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 — directions-duration-in-traffic ( daa8b3 )
by Eric
03:25 queued 13s
created

DistanceMatrix::buildDistance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class DistanceMatrix extends AbstractService
24
{
25
    /**
26
     * @param HttpClient     $client
27
     * @param MessageFactory $messageFactory
28
     */
29
    public function __construct(HttpClient $client, MessageFactory $messageFactory)
30
    {
31
        parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/distancematrix');
32
    }
33
34
    /**
35
     * @param DistanceMatrixRequest $request
36
     *
37
     * @return DistanceMatrixResponse
38
     */
39 View Code Duplication
    public function process(DistanceMatrixRequest $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...
40
    {
41
        $response = $this->getClient()->sendRequest($this->createRequest($request->buildQuery()));
42
        $data = $this->parse((string) $response->getBody());
43
44
        return $this->buildResponse($data);
45
    }
46
47
    /**
48
     * @param string $data
49
     *
50
     * @return mixed[]
51
     */
52
    private function parse($data)
53
    {
54
        if ($this->getFormat() === self::FORMAT_JSON) {
55
            return json_decode($data, true);
56
        }
57
58
        return $this->getXmlParser()->parse($data, [
59
            'destination_address' => 'destination_addresses',
60
            'element'             => 'elements',
61
            'origin_address'      => 'origin_addresses',
62
            'row'                 => 'rows',
63
        ]);
64
    }
65
66
    /**
67
     * @param mixed[] $data
68
     *
69
     * @return DistanceMatrixResponse
70
     */
71
    private function buildResponse(array $data)
72
    {
73
        $response = new DistanceMatrixResponse();
74
        $response->setStatus($data['status']);
75
        $response->setDestinations($data['destination_addresses']);
76
        $response->setOrigins($data['origin_addresses']);
77
        $response->setRows($this->buildRows($data['rows']));
78
79
        return $response;
80
    }
81
82
    /**
83
     * @param mixed[] $data
84
     *
85
     * @return DistanceMatrixRow[]
86
     */
87
    private function buildRows($data)
88
    {
89
        $rows = [];
90
91
        foreach ($data as $item) {
92
            $rows[] = $this->buildRow($item);
93
        }
94
95
        return $rows;
96
    }
97
98
    /**
99
     * @param mixed[] $data
100
     *
101
     * @return DistanceMatrixRow
102
     */
103
    private function buildRow($data)
104
    {
105
        $elements = [];
106
        foreach ($data['elements'] as $element) {
107
            $elements[] = $this->buildElement($element);
108
        }
109
110
        $row = new DistanceMatrixRow();
111
        $row->setElements($elements);
112
113
        return $row;
114
    }
115
116
    /**
117
     * @param mixed[] $data
118
     *
119
     * @return DistanceMatrixElement
120
     */
121
    private function buildElement(array $data)
122
    {
123
        $element = new DistanceMatrixElement();
124
        $element->setStatus($data['status']);
125
        $element->setDistance(isset($data['distance']) ? $this->buildDistance($data['distance']) : null);
126
        $element->setDuration(isset($data['duration']) ? $this->buildDuration($data['duration']) : null);
127
128
        return $element;
129
    }
130
131
    /**
132
     * @param mixed[] $data
133
     *
134
     * @return Distance
135
     */
136
    private function buildDistance(array $data)
137
    {
138
        return new Distance($data['text'], $data['value']);
139
    }
140
141
    /**
142
     * @param mixed[] $data
143
     *
144
     * @return Duration
145
     */
146
    private function buildDuration(array $data)
147
    {
148
        return new Duration($data['text'], $data['value']);
149
    }
150
}
151