Skip to content

Commit e84bacb

Browse files
committed
Simplyfing Intercom Listeners and setup
1 parent 56a9f0b commit e84bacb

File tree

3 files changed

+32
-65
lines changed

3 files changed

+32
-65
lines changed

application/classes/Ushahidi/Core.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static function init()
7575

7676
// Intercom config settings
7777
$di->set('thirdparty.intercomAppToken', function() use ($di) {
78-
return Kohana::$config->load('thirdparty.intercomAppToken');
78+
return getenv('INTERCOM_APP_TOKEN');
7979
});
8080

8181
// Roles config settings

application/classes/Ushahidi/Listener/IntercomAdminListener.php

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,33 @@ class Ushahidi_Listener_IntercomAdminListener extends AbstractListener
2323

2424
public function handle(EventInterface $event, $user = null)
2525
{
26-
27-
$config = service('repository.config')->get('thirdparty');
28-
$domain = Kohana::$config->load('site.client_url');
29-
30-
$intercomAppToken = $config->intercomAppToken;
31-
$company = [
32-
"id" => $config->intercomCompanyId
33-
];
34-
35-
if ($user && $config->intercomAppToken) {
36-
$client = new IntercomClient($config->intercomAppToken, null);
37-
38-
try {
39-
// Get Company if it already exists, if not create it
40-
if (!$config->intercomCompanyId) {
41-
42-
$site_name = Kohana::$config->load('site.name') ?: 'Ushahidi';
43-
$created = date("Y-m-d H:i:s");
44-
45-
$company = $client->companies->create([
46-
"company_id" => $domain,
47-
"name" => $site_name,
48-
"custom_attributes" => [
49-
"created" => $created
26+
if($user && $user->role === 'admin') {
27+
$intercomAppToken = getenv('INTERCOM_APP_TOKEN');
28+
$domain = service('site');
29+
$company = [
30+
"id" => $domain
31+
];
32+
33+
if ($intercomAppToken) {
34+
$client = new IntercomClient($intercomAppToken, null);
35+
36+
try {
37+
$client->users->update([
38+
"email" => $user->email,
39+
"created_at" => $user->created,
40+
"user_id" => $domain . '_' . $user->id,
41+
"realname" => $user->realname,
42+
"last_login" => $user->last_login,
43+
"role" => $user->role,
44+
"language" => $user->language,
45+
"companies" => [
46+
$company
5047
]
5148
]);
52-
53-
$config->intercomCompanyId = $company->id;
54-
service('repository.config')->update($config);
49+
} catch(ClientException $e) {
50+
Kohana::$log->add(Log::ERROR, print_r($e,true));
5551
}
56-
57-
$client->users->update([
58-
"email" => $user->email,
59-
"created_at" => $user->created,
60-
"user_id" => $domain . '_' . $user->id,
61-
"realname" => $user->realname,
62-
"last_login" => $user->last_login,
63-
"role" => $user->role,
64-
"language" => $user->language,
65-
"companies" => [
66-
$company
67-
]
68-
]);
69-
} catch(ClientException $e) {
70-
Kohana::$log->add(Log::ERROR, print_r($e,true));
71-
}
72-
}
52+
}
53+
}
7354
}
7455
}

application/classes/Ushahidi/Listener/IntercomCompanyListener.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,15 @@ public function setConfigRepo(ConfigRepository $config_repo)
2929

3030
public function handle(EventInterface $event, $data = null)
3131
{
32+
$intercomAppToken = getenv('INTERCOM_APP_TOKEN');
33+
if ($intercomAppToken) {
3234

33-
$config = service('repository.config')->get('thirdparty');
34-
$company = [
35-
"company_id" => $config->intercomCompanyId
36-
];
37-
38-
Kohana::$log->add(Log::ERROR, print_r($config));
39-
40-
if ($config->intercomAppToken) {
41-
42-
$client = new IntercomClient($config->intercomAppToken, null);
35+
$client = new IntercomClient($intercomAppToken, null);
4336

4437
try {
45-
// Create company with current date if it does not already exist
46-
if (!$config->intercomCompanyId) {
47-
$company->name = Kohana::$config->load('site.name') ?: 'Ushahidi';
48-
$config->intercomCompanyId = Kohana::$config->load('site.client_url');
49-
50-
service('repository.config')->update($config);
51-
$company->company_id = $config->intercomCompanyId;
52-
53-
$data['created'] = date("Y-m-d H:i:s");
54-
}
38+
$company = [
39+
"company_id" => service('site');
40+
];
5541

5642
$company->custom_attributes = $data;
5743
// Update company

0 commit comments

Comments
 (0)