forked from remp2020/remp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrm.php
51 lines (45 loc) · 1.56 KB
/
Crm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace Remp\MailerModule\User;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
use Tracy\Debugger;
class Crm implements IUser
{
const ENDPOINT_LIST = 'api/v1/users/list';
private $client;
public function __construct($baseUrl, $token)
{
$this->client = new Client([
'base_uri' => $baseUrl,
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/x-www-form-urlencoded',
]
]);
}
public function list(array $userIds, $page)
{
try {
$response = $this->client->post(self::ENDPOINT_LIST, [
'form_params' => [
'user_ids' => Json::encode($userIds),
'page' => $page,
],
]);
$result = Json::decode($response->getBody(), Json::FORCE_ARRAY);
$response = null;
return $result['users'];
} catch (ConnectException $e) {
throw new UserException("could not connect CRM user base: {$e->getMessage()}");
} catch (ClientException $e) {
Debugger::log("unable to get list of CRM users: " . $e->getResponse()->getBody()->getContents(), Debugger::WARNING);
return [];
} catch (JsonException $e) {
Debugger::log("could not decode JSON response: {$response->getBody()->getContents()}", Debugger::WARNING);
return [];
}
}
}