GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 61aacd...05f06e )
by Alexey
18:53
created

Client::makeRequest()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
rs 8.7155
cc 6
eloc 37
nc 9
nop 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace RetailCrm\Http;
4
5
use RetailCrm\Exception\CurlException;
6
use RetailCrm\Response\ApiResponse;
7
8
/**
9
 * HTTP client
10
 */
11
class Client
12
{
13
    const METHOD_GET = 'GET';
14
    const METHOD_POST = 'POST';
15
16
    protected $url;
17
    protected $defaultParameters;
18
    protected $retry;
19
20
    public function __construct($url, array $defaultParameters = array())
21
    {
22
        if (false === stripos($url, 'https://')) {
23
            throw new \InvalidArgumentException('API schema requires HTTPS protocol');
24
        }
25
26
        $this->url = $url;
27
        $this->defaultParameters = $defaultParameters;
28
        $this->retry = 0;
29
    }
30
31
    /**
32
     * Make HTTP request
33
     *
34
     * @param string $path
35
     * @param string $method (default: 'GET')
36
     * @param array $parameters (default: array())
37
     * @param int $timeout
38
     * @param bool $verify
39
     * @param bool $debug
40
     * @return ApiResponse
41
     */
42
    public function makeRequest(
43
        $path,
44
        $method,
45
        array $parameters = array(),
46
        $timeout = 30,
47
        $verify = false,
48
        $debug = false
0 ignored issues
show
Unused Code introduced by
The parameter $debug is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    ) {
50
        $allowedMethods = array(self::METHOD_GET, self::METHOD_POST);
51
        if (!in_array($method, $allowedMethods)) {
52
            throw new \InvalidArgumentException(sprintf(
53
                'Method "%s" is not valid. Allowed methods are %s',
54
                $method,
55
                implode(', ', $allowedMethods)
56
            ));
57
        }
58
59
        $parameters = array_merge($this->defaultParameters, $parameters);
60
61
        $path = $this->url . $path;
62
63
        if (self::METHOD_GET === $method && sizeof($parameters)) {
64
            $path .= '?' . http_build_query($parameters, '', '&');
65
        }
66
67
        $ch = curl_init();
68
        curl_setopt($ch, CURLOPT_URL, $path);
69
        curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout);
70
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
71
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
72
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
73
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
74
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify);
75
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify);
76
77
        if (self::METHOD_POST === $method) {
78
            curl_setopt($ch, CURLOPT_POST, true);
79
            curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
80
        }
81
82
        $responseBody = curl_exec($ch);
83
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
84
        $errno = curl_errno($ch);
85
        $error = curl_error($ch);
86
87
        curl_close($ch);
88
89
        if ($errno) {
90
            throw new CurlException($error, $errno);
91
        }
92
93
        return new ApiResponse($statusCode, $responseBody);
94
    }
95
}
96