Completed
Push — master ( bdc52c...616bc8 )
by Konstantin
03:41
created

RequestHandler::encodeResponse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 0
crap 4
1
<?php
2
3
namespace linkprofit\AmoCRM;
4
use Exception;
5
6
/**
7
 * Class RequestHandler
8
 * @package linkprofit\AmoCRM
9
 */
10
class RequestHandler
11
{
12
    /**
13
     * @var
14
     */
15
    protected $response;
16
17
    /**
18
     * @var integer
19
     */
20
    protected $httpCode;
21
22
    /**
23
     * @var array
24
     */
25
    protected $httpErrors = [
26
        301 => 'Moved permanently',
27
        400 => 'Bad request',
28
        401 => 'Unauthorized',
29
        403 => 'Forbidden',
30
        404 => 'Not found',
31
        500 => 'Internal server error',
32
        502 => 'Bad gateway',
33
        503 => 'Service unavailable'
34
    ];
35
36
    /**
37
     * @var array
38
     */
39
    protected $successHttpCodes = [
40
        200, 204
41
    ];
42
43
    protected $subdomain;
44
45
    /**
46
     * @param string $link
47
     * @param array $fields
48
     */
49 3
    public function performRequest($link, array $fields)
50
    {
51 3
        $curl = curl_init();
52
53 3
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
54 3
        curl_setopt($curl, CURLOPT_USERAGENT, 'amoCRM-API-client/1.0');
55 3
        curl_setopt($curl, CURLOPT_URL, $link);
56 3
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
57 3
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
58 3
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
59 3
        curl_setopt($curl, CURLOPT_HEADER, false);
60 3
        curl_setopt($curl, CURLOPT_COOKIEFILE, $this->getCookiePath());
61 3
        curl_setopt($curl, CURLOPT_COOKIEJAR, $this->getCookiePath());
62 3
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
63 3
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
64
65 3
        $this->response = curl_exec($curl);
66 3
        $this->httpCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
67
68 3
        curl_close($curl);
69 3
    }
70
71
    /**
72
     * @return bool
73
     */
74 4
    public function getResponse()
75
    {
76 4
        if (!$this->response) {
77 1
            return false;
78
        }
79
80 3
        $this->encodeResponse();
81
82 3
        return $this->response;
83
    }
84
85
    /**
86
     * @param $subdomain string
87
     */
88 4
    public function setSubdomain($subdomain)
89
    {
90 4
        $this->subdomain = $subdomain;
91 4
    }
92
93
    /**
94
     * @return mixed
95
     */
96 1
    public function getSubdomain()
97
    {
98 1
        return $this->subdomain;
99
    }
100
101
    /**
102
     * Encoding response from json, throw exception in case of wrong http code
103
     */
104 3
    protected function encodeResponse()
105
    {
106
        try {
107 3
            if (!in_array($this->httpCode, $this->successHttpCodes)) {
108 2
                throw new Exception(isset($this->httpErrors[$this->httpCode]) ? $this->httpErrors[$this->httpCode] : 'Undescribed error', $this->httpCode);
109
            }
110 3
        } catch (Exception $e) {
111 2
            echo 'Error: ' . $e->getMessage() . PHP_EOL . 'Error code: ' . $e->getCode();
112
        }
113
114 3
        $this->response = json_decode($this->response, true);
115 3
    }
116
117
    /**
118
     * @return string
119
     */
120 3
    protected function getCookiePath()
121
    {
122 3
        return dirname(dirname(__FILE__)) . '/cookie.txt';
123
    }
124
}