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.
Completed
Push — master ( bcefff...98364d )
by Eric
03:01
created

DistanceMatrixService::buildElement()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 8.5906
cc 5
eloc 12
nc 16
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Google Map package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\GoogleMap\Service\DistanceMatrix;
13
14
use Http\Client\HttpClient;
15
use Http\Message\MessageFactory;
16
use Ivory\GoogleMap\Service\AbstractService;
17
use Ivory\GoogleMap\Service\Base\Distance;
18
use Ivory\GoogleMap\Service\Base\Duration;
19
use Ivory\GoogleMap\Service\Base\Fare;
20
use Ivory\GoogleMap\Service\DistanceMatrix\Request\DistanceMatrixRequestInterface;
21
use Ivory\GoogleMap\Service\DistanceMatrix\Response\DistanceMatrixElement;
22
use Ivory\GoogleMap\Service\DistanceMatrix\Response\DistanceMatrixResponse;
23
use Ivory\GoogleMap\Service\DistanceMatrix\Response\DistanceMatrixRow;
24
25
/**
26
 * @author GeLo <[email protected]>
27
 */
28
class DistanceMatrixService extends AbstractService
29
{
30
    /**
31
     * @param HttpClient     $client
32
     * @param MessageFactory $messageFactory
33
     */
34
    public function __construct(HttpClient $client, MessageFactory $messageFactory)
35
    {
36
        parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/distancematrix');
37
    }
38
39
    /**
40
     * @param DistanceMatrixRequestInterface $request
41
     *
42
     * @return DistanceMatrixResponse
43
     */
44 View Code Duplication
    public function process(DistanceMatrixRequestInterface $request)
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...
45
    {
46
        $response = $this->getClient()->sendRequest($this->createRequest($request->build()));
47
        $data = $this->parse((string) $response->getBody());
48
49
        return $this->buildResponse($data);
50
    }
51
52
    /**
53
     * @param string $data
54
     *
55
     * @return mixed[]
56
     */
57
    private function parse($data)
58
    {
59
        if ($this->getFormat() === self::FORMAT_JSON) {
60
            return json_decode($data, true);
61
        }
62
63
        return $this->getXmlParser()->parse($data, [
64
            'destination_address' => 'destination_addresses',
65
            'element'             => 'elements',
66
            'origin_address'      => 'origin_addresses',
67
            'row'                 => 'rows',
68
        ]);
69
    }
70
71
    /**
72
     * @param mixed[] $data
73
     *
74
     * @return DistanceMatrixResponse
75
     */
76
    private function buildResponse(array $data)
77
    {
78
        $response = new DistanceMatrixResponse();
79
        $response->setStatus($data['status']);
80
        $response->setDestinations($data['destination_addresses']);
81
        $response->setOrigins($data['origin_addresses']);
82
        $response->setRows($this->buildRows($data['rows']));
83
84
        return $response;
85
    }
86
87
    /**
88
     * @param mixed[] $data
89
     *
90
     * @return DistanceMatrixRow[]
91
     */
92
    private function buildRows($data)
93
    {
94
        $rows = [];
95
96
        foreach ($data as $row) {
97
            $rows[] = $this->buildRow($row);
98
        }
99
100
        return $rows;
101
    }
102
103
    /**
104
     * @param mixed[] $data
105
     *
106
     * @return DistanceMatrixRow
107
     */
108
    private function buildRow($data)
109
    {
110
        $row = new DistanceMatrixRow();
111
        $row->setElements($this->buildElements($data['elements']));
112
113
        return $row;
114
    }
115
116
    /**
117
     * @param mixed[] $data
118
     *
119
     * @return DistanceMatrixElement[]
120
     */
121
    private function buildElements(array $data)
122
    {
123
        $elements = [];
124
125
        foreach ($data as $element) {
126
            $elements[] = $this->buildElement($element);
127
        }
128
129
        return $elements;
130
    }
131
132
    /**
133
     * @param mixed[] $data
134
     *
135
     * @return DistanceMatrixElement
136
     */
137
    private function buildElement(array $data)
138
    {
139
        $element = new DistanceMatrixElement();
140
        $element->setStatus($data['status']);
141
142
        if (isset($data['distance'])) {
143
            $element->setDistance($this->buildDistance($data['distance']));
144
        }
145
146
        if (isset($data['duration'])) {
147
            $element->setDuration($this->buildDuration($data['duration']));
148
        }
149
150
        if (isset($data['duration_in_traffic'])) {
151
            $element->setDurationInTraffic($this->buildDuration($data['duration_in_traffic']));
152
        }
153
154
        if (isset($data['fare'])) {
155
            $element->setFare($this->buildFare($data['fare']));
156
        }
157
158
        return $element;
159
    }
160
161
    /**
162
     * @param mixed[] $data
163
     *
164
     * @return Distance
165
     */
166
    private function buildDistance(array $data)
167
    {
168
        return new Distance($data['value'], $data['text']);
169
    }
170
171
    /**
172
     * @param mixed[] $data
173
     *
174
     * @return Duration
175
     */
176
    private function buildDuration(array $data)
177
    {
178
        return new Duration($data['value'], $data['text']);
179
    }
180
181
    /**
182
     * @param mixed[] $data
183
     *
184
     * @return Fare
185
     */
186
    private function buildFare(array $data)
187
    {
188
        return new Fare($data['value'], $data['currency'], $data['text']);
189
    }
190
}
191