Completed
Push — master ( 5ed5e7...614fcd )
by Adriano
05:55
created

AbstractApi::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace FipeApi;
4
5
use Curl\Curl;
6
use FipeApi\Constants\FipeApiParameter;
7
8
/**
9
 * Class AbstractApi
10
 * @package FipeApi
11
 */
12
abstract class AbstractApi
13
{
14
15
    /**
16
     * @var Client
17
     */
18
    protected $client;
19
20
    /**
21
     * @var Curl
22
     */
23
    protected $_curl;
24
25
    /**
26
     * @var string
27
     */
28
    protected $tipo;
29
30
    /**
31
     * AbstractApi constructor.
32
     * @param string $tipo
33
     * @param Client|null $client
34
     */
35
    public function __construct($tipo, Client $client = null)
36
    {
37
38
        $this->_curl = new Curl();
39
        $this->tipo = $tipo;
40
41
        if(is_null($client))
42
        {
43
            $this->client = new Client();
44
        }else{
45
            $this->client = $client;
46
        }
47
48
    }
49
50
    /**
51
     * @return array
52
     * @throws \Exception
53
     */
54 View Code Duplication
    public function getMarcas()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        try{
57
            $this->_curl->get(sprintf("%s/%s/marcas.json",
58
                $this->client->getParameter(FipeApiParameter::FIPE_API_HOST),
59
                $this->tipo)
60
            );
61
            return $this->getResponse();
62
        }catch(\Exception $ex){
63
            throw new \Exception($ex->getMessage());
64
        }
65
    }
66
67
    /**
68
     * @param integer $fabricante
69
     *
70
     * @return array
71
     * @throws \Exception
72
     */
73 View Code Duplication
    public function getVeiculos($fabricante)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        try{
76
            $this->_curl->get(sprintf("%s/%s/veiculos/%s.json",
77
                    $this->client->getParameter(FipeApiParameter::FIPE_API_HOST),
78
                    $this->tipo,
79
                    $fabricante)
80
            );
81
            return $this->getResponse();
82
        }catch(\Exception $ex){
83
            throw new \Exception($ex->getMessage());
84
        }
85
    }
86
87
    /**
88
     * @param integer $fabricante
89
     * @param integer $veiculo
90
     *
91
     * @return array
92
     * @throws \Exception
93
     */
94 View Code Duplication
    public function getModelos($fabricante, $veiculo)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        try{
97
            $this->_curl->get(sprintf("%s/%s/veiculo/%s/%s.json",
98
                    $this->client->getParameter(FipeApiParameter::FIPE_API_HOST),
99
                    $this->tipo,
100
                    $fabricante,
101
                    $veiculo)
102
            );
103
            return $this->getResponse();
104
        }catch(\Exception $ex){
105
            throw new \Exception($ex->getMessage());
106
        }
107
    }
108
109 View Code Duplication
    public function getDetalhes($fabricante, $veiculo, $fipeCodigo)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        try{
112
            $this->_curl->get(sprintf("%s/%s/veiculo/%s/%s/%s.json",
113
                    $this->client->getParameter(FipeApiParameter::FIPE_API_HOST),
114
                    $this->tipo,
115
                    $fabricante,
116
                    $veiculo,
117
                    $fipeCodigo)
118
            );
119
            return $this->getResponse();
120
        }catch(\Exception $ex){
121
            throw new \Exception($ex->getMessage());
122
        }
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    protected function getResponse()
129
    {
130
        return [
131
            'http' => [
132
                'httpStatusCode' => $this->_curl->httpStatusCode,
133
                'httpErrorMessage' => $this->_curl->httpErrorMessage
134
            ],
135
            'data' => json_decode($this->_curl->rawResponse, true)
136
        ];
137
    }
138
139
}