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.

Curl::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
ccs 6
cts 8
cp 0.75
crap 2.0625
rs 10
1
<?php
2
3
namespace RattfieldNz\SafeUrls\Libraries\Curl;
4
5
use Curl\Curl as PhpCurl;
6
use RattfieldNz\SafeUrls\Libraries\Data\Data;
7
8
/**
9
 * Class Curl.
10
 *
11
 * @category PHP
12
 *
13
 * @author  Rob Attfield <[email protected]>
14
 * @license https://github.com/rattfieldnz/safe-urls/blob/master/license.md MIT
15
 *
16
 * @link https://github.com/rattfieldnz/safe-urls/
17
 */
18
class Curl
19
{
20
    private $_payload;
21
    private $_defaultHeaders;
22
    private $_timeout;
23
    private $_curl;
24
25
    /**
26
     * Curl constructor.
27
     *
28
     * Set the needed properties to do a CURL request.
29
     *
30
     * @param array $payload Data to be submitted with CURL request.
31
     * @param int   $timeout Timeout in seconds to complete a CURL request. Default is 10.
32
     *
33
     * @throws \ErrorException Will throw an exception if PHP ext-curl is not installed.
34
     */
35 8
    public function __construct(array $payload, int $timeout = 10)
36
    {
37 8
        if (!extension_loaded('curl')) {
38
            throw new \ErrorException(
39
                'The cURL extensions is not loaded, make sure you have installed the cURL extension: https://php.net/manual/curl.setup.php'
40
            );
41
        }
42
43 8
        $this->_curl = new PhpCurl();
44 8
        $this->_payload = $payload;
45 8
        $this->_timeout = $timeout;
46 8
        $this->_setDefaultHeaders();
47 8
    }
48
49
    /**
50
     * Execute a CURL request, and return current object for further processing.
51
     *
52
     * @return PhpCurl
53
     */
54 8
    public function execute(): PhpCurl
55
    {
56 8
        $this->_curl->setOpt(CURLOPT_RETURNTRANSFER, true);
57 8
        $this->_curl->setOpt(CURLOPT_CONNECTTIMEOUT, $this->_timeout);
58 8
        $this->_curl->setOpt(CURLOPT_HTTPHEADER, $this->_defaultHeaders);
59 8
        $this->_curl->setOpt(CURLOPT_POSTFIELDS, json_encode($this->_payload));
60 8
        $this->_curl->post(Data::googleApiUrl());
61
62 8
        return $this->_curl;
63
    }
64
65
    /**
66
     * Get the data retrieved from executing CURL request, in JSON format.
67
     *
68
     * @return string
69
     *
70
     * @see \RattfieldNz\SafeUrls\Libraries\Curl\Curl->execute().
71
     */
72 8
    public function getData()
73
    {
74 8
        $dataObject = $this->execute();
75
        $data = [
76 8
            'status'   => $dataObject->getHttpStatus(),
77 8
            'response' => json_decode($dataObject->response, true),
78
        ];
79
80 8
        return json_encode($data);
81
    }
82
83
    /**
84
     * Sets the default headers to use for CURL request.
85
     *
86
     * @return void
87
     */
88 8
    private function _setDefaultHeaders(): void
89
    {
90 8
        $this->_defaultHeaders = [
91
            'Content-Type: application/json',
92
            'Connection: Keep-Alive',
93
        ];
94 8
    }
95
}
96