Passed
Push — develop ( 72d62a...1226dd )
by Andrew
02:08
created

DataSender::validateCode()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 5
nc 3
nop 3
crap 5
1
<?php
2
3
4
namespace ddlzz\AmoAPI\Request;
5
use ddlzz\AmoAPI\Exceptions\ErrorCodeException;
6
use ddlzz\AmoAPI\Exceptions\FailedAuthException;
7
use ddlzz\AmoAPI\SettingsStorage;
8
9
10
/**
11
 * Class DataSender. It's responsible for interacting with amoCRM.
12
 * @package ddlzz\AmoAPI
13
 * @author ddlzz
14
 */
15
class DataSender
16
{
17
    /** @var SettingsStorage */
18
    private $settings;
19
20
    /** @var Curl */
21
    private $curl;
22
23
    /** DataSender constructor.
24
     * @param Curl $curl
25
     * @param SettingsStorage $settings
26
     */
27 4
    public function __construct(Curl $curl, SettingsStorage $settings)
28
    {
29 4
        $this->curl = $curl;
30 4
        $this->settings = $settings;
31 4
    }
32
33
    /**
34
     * @param string $url
35
     * @param array $data
36
     * @return string
37
     * @throws ErrorCodeException
38
     * @throws FailedAuthException
39
     * @throws \ddlzz\AmoAPI\Exceptions\CurlException
40
     */
41 3
    public function send($url, array $data = [])
42
    {
43 3
        $this->curl->init();
44
45 3
        $this->curl->setReturnTransfer(true);
46 3
        $this->curl->setUserAgent($this->settings->getUserAgent());
47 3
        $this->curl->setUrl($url);
48 3
        $this->curl->setHttpHeader([$this->settings::SENDER_HTTP_HEADER]);
49 3
        $this->curl->setHeader(false);
50 3
        $this->curl->setCookieFile($this->settings->getCookiePath());
51 3
        $this->curl->setCookieJar($this->settings->getCookiePath());
52 3
        $this->curl->setSSLVerifyPeer(false);
53 3
        $this->curl->setSSLVerifyHost(2);
54
55 3
        if (!empty($data)) {
56 3
            $this->curl->setCustomRequest('POST');
57 3
            $this->curl->setPostFields(json_encode($data));
58
        }
59
60 3
        $response = $this->curl->exec();
61 3
        $code = $this->curl->getHttpCode();
62
63 3
        $this->curl->close();
64 3
        $this->validateCode($code, $url, $response);
0 ignored issues
show
Bug introduced by
$response of type false|array is incompatible with the type string expected by parameter $response of ddlzz\AmoAPI\Request\DataSender::validateCode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $this->validateCode($code, $url, /** @scrutinizer ignore-type */ $response);
Loading history...
65
66 1
        return (string)$response;
67
    }
68
69
    /**
70
     * @param int $code
71
     * @return string
72
     */
73 2
    private static function getErrorByHttpCode($code)
74
    {
75
        $errors = [
76 2
            301 => '301 Moved permanently',
77
            400 => '400 Bad request',
78
            401 => '401 Unauthorized or Blocked',
79
            403 => '403 Forbidden',
80
            404 => '404 Not found',
81
            429 => '429 Too Many Requests',
82
            500 => '500 Internal server error',
83
            502 => '502 Bad gateway',
84
            503 => '503 Service unavailable',
85
            504 => '504 Gateway Timeout',
86
        ];
87
88 2
        return isset($errors[$code]) ? $errors[$code] : $code . ' Unknown error';
89
    }
90
91
    /**
92
     * @param int $code
93
     * @param string $url
94
     * @param string $response
95
     * @return bool
96
     * @throws ErrorCodeException
97
     * @throws FailedAuthException
98
     */
99 3
    private function validateCode($code, $url, $response)
100
    {
101 3
        if ((401 === $code) || (403 === $code)) {
102 1
            throw new FailedAuthException('Auth failed! ' . self::getErrorByHttpCode($code), $response);
103 2
        } elseif ((200 !== $code) && (204 !== $code)) {
104 1
            throw new ErrorCodeException(self::getErrorByHttpCode($code), $response, $url);
105
        }
106
107 1
        return true;
108
    }
109
}