|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mb24dev\AmoCRM; |
|
4
|
|
|
|
|
5
|
|
|
use Dflydev\FigCookies\Cookie; |
|
6
|
|
|
use Dflydev\FigCookies\FigRequestCookies; |
|
7
|
|
|
use Dflydev\FigCookies\SetCookies; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use GuzzleHttp\Psr7\Request; |
|
10
|
|
|
use mb24dev\AmoCRM\Method\MethodInterface; |
|
11
|
|
|
use mb24dev\AmoCRM\ResponseTransformer\ResponseTransformerInterface; |
|
12
|
|
|
use mb24dev\AmoCRM\Session\Session; |
|
13
|
|
|
use mb24dev\AmoCRM\Session\SessionCookieDoNotPresented; |
|
14
|
|
|
use mb24dev\AmoCRM\Session\SessionDoesNotExistException; |
|
15
|
|
|
use mb24dev\AmoCRM\SessionStorage\SessionStorageInterface; |
|
16
|
|
|
use mb24dev\AmoCRM\User\UserInterface; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AmoCRM |
|
21
|
|
|
* |
|
22
|
|
|
* @package mb24dev\AmoCRM |
|
23
|
|
|
*/ |
|
24
|
|
|
class AmoCRMClient |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ClientInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $httpClient; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var SessionStorageInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $sessionStorage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var LoggerInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $logger; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ResponseTransformerInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $responseTransformer; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* AmoCRM constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param ClientInterface $httpClient |
|
50
|
|
|
* @param SessionStorageInterface $sessionStorage |
|
51
|
|
|
* @param ResponseTransformerInterface $responseTransformer |
|
52
|
|
|
* @param LoggerInterface $logger |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function __construct( |
|
55
|
|
|
ClientInterface $httpClient, |
|
56
|
|
|
SessionStorageInterface $sessionStorage, |
|
57
|
|
|
ResponseTransformerInterface $responseTransformer, |
|
58
|
|
|
LoggerInterface $logger = null |
|
59
|
|
|
) { |
|
60
|
4 |
|
$this->httpClient = $httpClient; |
|
61
|
4 |
|
$this->responseTransformer = $responseTransformer; |
|
62
|
4 |
|
$this->sessionStorage = $sessionStorage; |
|
63
|
4 |
|
$this->logger = $logger; |
|
64
|
4 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param ClientInterface $httpClient |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setHttpClient($httpClient) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->httpClient = $httpClient; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param SessionStorageInterface $sessionStorage |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setSessionStorage($sessionStorage) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->sessionStorage = $sessionStorage; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param ResponseTransformerInterface $responseTransformer |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setResponseTransformer($responseTransformer) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->responseTransformer = $responseTransformer; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param LoggerInterface $logger |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setLogger($logger) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->logger = $logger; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param MethodInterface $method |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
* @throws AmoCRMException |
|
102
|
|
|
*/ |
|
103
|
4 |
|
public function exec(MethodInterface $method) |
|
104
|
|
|
{ |
|
105
|
|
|
try { |
|
106
|
4 |
|
$request = $method->buildRequest(); |
|
107
|
|
|
|
|
108
|
4 |
|
if (!$session = $method->getUser()->getAmoCRMSession()) { |
|
109
|
4 |
|
$session = $this->sessionStorage->getActive($method->getUser()); |
|
110
|
2 |
|
} |
|
111
|
|
|
|
|
112
|
3 |
|
$cookie = new Cookie('session_id', $session->getId()); |
|
113
|
3 |
|
$request = FigRequestCookies::set($request, $cookie); |
|
114
|
|
|
|
|
115
|
3 |
|
$response = $this->httpClient->send($request); |
|
116
|
4 |
|
} catch (SessionDoesNotExistException $e) { |
|
117
|
2 |
|
$this->auth($e->getUser()); |
|
118
|
|
|
|
|
119
|
1 |
|
return $this->exec($method); |
|
120
|
|
|
} catch (\Exception $e) { |
|
121
|
|
|
if ($this->logger) { |
|
122
|
|
|
$this->logger->critical('Client error', ['exception' => $e]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
throw new AmoCRMException($e->getMessage()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
3 |
|
$methodResponseTransformer = $method->getResponseTransformer(); |
|
129
|
|
|
|
|
130
|
3 |
|
if ($methodResponseTransformer) { |
|
131
|
1 |
|
return $methodResponseTransformer->transform($response); |
|
132
|
2 |
|
} elseif ($this->responseTransformer) { |
|
133
|
2 |
|
return $this->responseTransformer->transform($response); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $response->getBody()->getContents(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param UserInterface $user |
|
141
|
|
|
* @throws AmoCRMException |
|
142
|
|
|
*/ |
|
143
|
2 |
|
private function auth(UserInterface $user) |
|
144
|
|
|
{ |
|
145
|
2 |
|
$request = new Request( |
|
146
|
2 |
|
'POST', $user->getAmoCRMDomain() . "private/api/auth.php?type=json", [], \GuzzleHttp\json_encode( |
|
147
|
|
|
[ |
|
148
|
2 |
|
'USER_LOGIN' => $user->getAmoCRMLogin(), |
|
149
|
2 |
|
'USER_HASH' => $user->getAmoCRMHash(), |
|
150
|
|
|
] |
|
151
|
2 |
|
) |
|
152
|
2 |
|
); |
|
153
|
|
|
|
|
154
|
2 |
|
$response = $this->httpClient->send($request); |
|
155
|
2 |
|
$cookies = SetCookies::fromResponse($response); |
|
156
|
2 |
|
$sessionCookie = $cookies->get('session_id'); |
|
157
|
|
|
|
|
158
|
2 |
|
if ($sessionCookie) { |
|
159
|
2 |
|
$session = new Session($sessionCookie->getValue()); |
|
160
|
2 |
|
$user->setAmoCRMSession($session); |
|
161
|
2 |
|
$this->sessionStorage->save($session, $user); |
|
162
|
1 |
|
} else { |
|
163
|
|
|
throw new SessionCookieDoNotPresented(); |
|
164
|
|
|
} |
|
165
|
1 |
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|