1 | <?php |
||
2 | |||
3 | namespace ddlzz\AmoAPI\Request; |
||
4 | |||
5 | use ddlzz\AmoAPI\Exception\ErrorCodeException; |
||
6 | use ddlzz\AmoAPI\Exception\FailedAuthException; |
||
7 | use ddlzz\AmoAPI\SettingsStorage; |
||
8 | |||
9 | /** |
||
10 | * Class DataSender. It's responsible for interacting with amoCRM. |
||
11 | * |
||
12 | * @author ddlzz |
||
13 | */ |
||
14 | class DataSender |
||
15 | { |
||
16 | /** @var SettingsStorage */ |
||
17 | private $settings; |
||
18 | |||
19 | /** @var Curl */ |
||
20 | private $curl; |
||
21 | |||
22 | /** DataSender constructor. |
||
23 | * @param Curl $curl |
||
24 | * @param SettingsStorage $settings |
||
25 | */ |
||
26 | 4 | public function __construct(Curl $curl, SettingsStorage $settings) |
|
27 | { |
||
28 | 4 | $this->curl = $curl; |
|
29 | 4 | $this->settings = $settings; |
|
30 | 4 | } |
|
31 | |||
32 | /** |
||
33 | * @param string $url |
||
34 | * @param array $data |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | 3 | public function send($url, array $data = []) |
|
39 | { |
||
40 | 3 | $this->curl->init(); |
|
41 | |||
42 | 3 | $this->curl->setReturnTransfer(true); |
|
43 | 3 | $this->curl->setUserAgent($this->settings->getUserAgent()); |
|
44 | 3 | $this->curl->setUrl($url); |
|
45 | 3 | $this->curl->setHttpHeader([SettingsStorage::SENDER_HTTP_HEADER]); |
|
46 | 3 | $this->curl->setHeader(false); |
|
47 | 3 | $this->curl->setCookieFile($this->settings->getCookiePath()); |
|
48 | 3 | $this->curl->setCookieJar($this->settings->getCookiePath()); |
|
49 | 3 | $this->curl->setSSLVerifyPeer(false); |
|
50 | 3 | $this->curl->setSSLVerifyHost(2); |
|
51 | |||
52 | 3 | if (!empty($data)) { |
|
53 | 3 | $this->curl->setCustomRequest('POST'); |
|
54 | 3 | $this->curl->setPostFields(json_encode($data)); |
|
55 | } |
||
56 | |||
57 | 3 | $response = $this->curl->exec(); |
|
58 | 3 | $code = $this->curl->getHttpCode(); |
|
59 | |||
60 | 3 | $this->curl->close(); |
|
61 | 3 | $this->validateCode($code, $url, $response); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
62 | |||
63 | 1 | return (string) $response; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param int $code |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | 2 | private static function getErrorByHttpCode($code) |
|
72 | { |
||
73 | $errors = [ |
||
74 | 2 | 301 => '301 Moved permanently', |
|
75 | 400 => '400 Bad request', |
||
76 | 401 => '401 Unauthorized or Blocked', |
||
77 | 403 => '403 Forbidden', |
||
78 | 404 => '404 Not found', |
||
79 | 429 => '429 Too Many Requests', |
||
80 | 500 => '500 Internal server error', |
||
81 | 502 => '502 Bad gateway', |
||
82 | 503 => '503 Service unavailable', |
||
83 | 504 => '504 Gateway Timeout', |
||
84 | ]; |
||
85 | |||
86 | 2 | return isset($errors[$code]) ? $errors[$code] : $code.' Unknown error'; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param int $code |
||
91 | * @param string $url |
||
92 | * @param string $response |
||
93 | * |
||
94 | * @throws ErrorCodeException |
||
95 | * @throws FailedAuthException |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | 3 | private function validateCode($code, $url, $response) |
|
100 | { |
||
101 | switch ($code) { |
||
102 | 3 | case 401: |
|
103 | 2 | case 403: |
|
104 | 1 | throw new FailedAuthException('Auth failed! '.self::getErrorByHttpCode($code), $response); |
|
105 | break; |
||
106 | 2 | case 200 !== $code && 204 !== $code: |
|
107 | 1 | throw new ErrorCodeException(self::getErrorByHttpCode($code), $response, $url); |
|
108 | break; |
||
109 | } |
||
110 | |||
111 | 1 | return true; |
|
112 | } |
||
113 | } |
||
114 |