Completed
Push — develop ( 783309...516d96 )
by Ravan
01:22
created

Request::flagAsContracted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
namespace Axado;
3
4
class Request
5
{
6
    /**
7
     * The de API url.
8
     *
9
     * @var string
10
     */
11
    protected static $consultURL = 'http://api.axado.com.br/v2/consulta/?token=';
12
13
    /**
14
     * The de API url.
15
     *
16
     * @var string
17
     */
18
    protected static $quotationURL = 'http://api.axado.com.br/v2/cotacao/';
19
20
    /**
21
     * Token for consult quotations.
22
     *
23
     * @var string
24
     */
25
    protected $token;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param string $token
31
     */
32
    public function __construct(string $token)
33
    {
34
        $this->token = $token;
35
    }
36
37
    /**
38
     * Runs the request to Axado API and return a Response Object.
39
     *
40
     * @param string $jsonString
41
     *
42
     * @return Response
43
     */
44
    public function consultShipping($jsonString): Response
45
    {
46
        $raw = $this->doRequest(
47
            'POST',
48
            static::$consultURL . $this->token,
49
            $jsonString
50
        );
51
52
        return $this->createResponse($raw);
0 ignored issues
show
Documentation introduced by
$raw is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
    }
54
55
    /**
56
     * Request to Axado API.
57
     *
58
     * @codeCoverageIgnore
59
     *
60
     * @param string $method
61
     * @param string $path
62
     * @param string $data
63
     *
64
     * @return array
65
     */
66
    protected function doRequest(string $method, string $path, string $data): array
67
    {
68
        $conn = curl_init();
69
70
        curl_setopt_array(
71
            $conn,
72
            [
73
                CURLOPT_URL => $path,
74
                CURLOPT_TIMEOUT => 15,
75
                CURLOPT_RETURNTRANSFER => 1,
76
                CURLOPT_CUSTOMREQUEST => $method,
77
                CURLOPT_FORBID_REUSE => 1,
78
                CURLOPT_POSTFIELDS => $data,
79
            ]
80
        );
81
82
        $response = curl_exec($conn);
83
        $data = null;
84
85
        if (false !== $response) {
86
            $data = json_decode($response, true);
87
        }
88
89
        curl_close($conn);
90
91
        return $data ?? [];
92
    }
93
94
    /**
95
     * Return the Response instance.
96
     *
97
     * @param  string $raw
98
     *
99
     * @return Response
100
     */
101
    protected function createResponse($raw): Response
102
    {
103
        $response = new Response();
104
        $response->parse($raw);
0 ignored issues
show
Documentation introduced by
$raw is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
106
        return $response;
107
    }
108
}
109