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

AbstractApi   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 42.19 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 54
loc 128
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A getMarcas() 12 12 2
A getVeiculos() 13 13 2
A getModelos() 14 14 2
A getDetalhes() 15 15 2
A getResponse() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}