-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCrm.php
63 lines (55 loc) · 1.9 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
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace Remp\MailerModule\Models\Users;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\StreamWrapper;
use JsonMachine\Items;
use Nette\Utils\Json;
use Tracy\Debugger;
class Crm implements IUser
{
const ENDPOINT_LIST = 'api/v1/users/list';
private $client;
public function __construct(string $baseUrl, string $token)
{
$this->client = new Client([
'base_uri' => $baseUrl,
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/x-www-form-urlencoded',
]
]);
}
public function list(array $userIds, int $page, bool $includeDeactivated = false): array
{
try {
$response = $this->client->post(self::ENDPOINT_LIST, [
'form_params' => [
'user_ids' => Json::encode($userIds),
'page' => $page,
'include_deactivated' => $includeDeactivated,
],
]);
$stream = StreamWrapper::getResource($response->getBody());
try {
$users = [];
foreach (Items::fromStream($stream, ['pointer' => '/users']) as $user) {
$users[$user->id] = [
'id' => $user->id,
'email' => $user->email,
];
}
} finally {
fclose($stream);
}
} 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 [];
}
return $users;
}
}