Skip to content

Commit

Permalink
Simplyfing Intercom Listeners and setup
Browse files Browse the repository at this point in the history
  • Loading branch information
willdoran committed Jun 5, 2017
1 parent 56a9f0b commit e84bacb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 65 deletions.
2 changes: 1 addition & 1 deletion application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function init()

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

// Roles config settings
Expand Down
69 changes: 25 additions & 44 deletions application/classes/Ushahidi/Listener/IntercomAdminListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,52 +23,33 @@ class Ushahidi_Listener_IntercomAdminListener extends AbstractListener

public function handle(EventInterface $event, $user = null)
{

$config = service('repository.config')->get('thirdparty');
$domain = Kohana::$config->load('site.client_url');

$intercomAppToken = $config->intercomAppToken;
$company = [
"id" => $config->intercomCompanyId
];

if ($user && $config->intercomAppToken) {
$client = new IntercomClient($config->intercomAppToken, null);

try {
// Get Company if it already exists, if not create it
if (!$config->intercomCompanyId) {

$site_name = Kohana::$config->load('site.name') ?: 'Ushahidi';
$created = date("Y-m-d H:i:s");

$company = $client->companies->create([
"company_id" => $domain,
"name" => $site_name,
"custom_attributes" => [
"created" => $created
if($user && $user->role === 'admin') {
$intercomAppToken = getenv('INTERCOM_APP_TOKEN');
$domain = service('site');
$company = [
"id" => $domain
];

if ($intercomAppToken) {
$client = new IntercomClient($intercomAppToken, null);

try {
$client->users->update([
"email" => $user->email,
"created_at" => $user->created,
"user_id" => $domain . '_' . $user->id,
"realname" => $user->realname,
"last_login" => $user->last_login,
"role" => $user->role,
"language" => $user->language,
"companies" => [
$company
]
]);

$config->intercomCompanyId = $company->id;
service('repository.config')->update($config);
} catch(ClientException $e) {
Kohana::$log->add(Log::ERROR, print_r($e,true));
}

$client->users->update([
"email" => $user->email,
"created_at" => $user->created,
"user_id" => $domain . '_' . $user->id,
"realname" => $user->realname,
"last_login" => $user->last_login,
"role" => $user->role,
"language" => $user->language,
"companies" => [
$company
]
]);
} catch(ClientException $e) {
Kohana::$log->add(Log::ERROR, print_r($e,true));
}
}
}
}
}
}
26 changes: 6 additions & 20 deletions application/classes/Ushahidi/Listener/IntercomCompanyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,15 @@ public function setConfigRepo(ConfigRepository $config_repo)

public function handle(EventInterface $event, $data = null)
{
$intercomAppToken = getenv('INTERCOM_APP_TOKEN');
if ($intercomAppToken) {

$config = service('repository.config')->get('thirdparty');
$company = [
"company_id" => $config->intercomCompanyId
];

Kohana::$log->add(Log::ERROR, print_r($config));

if ($config->intercomAppToken) {

$client = new IntercomClient($config->intercomAppToken, null);
$client = new IntercomClient($intercomAppToken, null);

try {
// Create company with current date if it does not already exist
if (!$config->intercomCompanyId) {
$company->name = Kohana::$config->load('site.name') ?: 'Ushahidi';
$config->intercomCompanyId = Kohana::$config->load('site.client_url');

service('repository.config')->update($config);
$company->company_id = $config->intercomCompanyId;

$data['created'] = date("Y-m-d H:i:s");
}
$company = [
"company_id" => service('site');
];

$company->custom_attributes = $data;
// Update company
Expand Down

0 comments on commit e84bacb

Please sign in to comment.