Request   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 86
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A consultShipping() 0 13 1
B doRequest() 0 27 2
1
<?php
2
namespace Axado;
3
4
class Request
5
{
6
    /**
7
     * The API url.
8
     *
9
     * @var string
10
     */
11
    protected static $consultURL = 'http://api.axado.com.br/v2/consulta/?token=';
12
13
    /**
14
     * Token for consult quotations.
15
     *
16
     * @var string
17
     */
18
    protected $token;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param string $token
24
     */
25
    public function __construct(string $token)
26
    {
27
        $this->token = $token;
28
    }
29
30
    /**
31
     * Runs the request to Axado API and return a Response Object.
32
     *
33
     * @param string $jsonString
34
     *
35
     * @return Response
36
     */
37
    public function consultShipping($jsonString): Response
38
    {
39
        $raw = $this->doRequest(
40
            'POST',
41
            static::$consultURL.$this->token,
42
            $jsonString
43
        );
44
45
        $response = new Response();
46
        $response->parse($raw);
47
48
        return $response;
49
    }
50
51
    /**
52
     * Request to Axado API.
53
     *
54
     * @codeCoverageIgnore
55
     *
56
     * @param string $method
57
     * @param string $path
58
     * @param string $data
59
     *
60
     * @return array
61
     */
62
    protected function doRequest(string $method, string $path, string $data): array
63
    {
64
        $conn = curl_init();
65
66
        curl_setopt_array(
67
            $conn,
68
            [
69
                CURLOPT_URL => $path,
70
                CURLOPT_TIMEOUT => 15,
71
                CURLOPT_RETURNTRANSFER => 1,
72
                CURLOPT_CUSTOMREQUEST => $method,
73
                CURLOPT_FORBID_REUSE => 1,
74
                CURLOPT_POSTFIELDS => $data,
75
            ]
76
        );
77
78
        $response = curl_exec($conn);
79
        $data = null;
80
81
        if (false !== $response) {
82
            $data = json_decode($response, true);
83
        }
84
85
        curl_close($conn);
86
87
        return $data ?? [];
88
    }
89
}
90