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
Pull Request — master (#177)
by Eric
08:46 queued 06:24
created

PlaceAutocompleteService::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
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\Place\Autocomplete;
13
14
use Ivory\GoogleMap\Service\Place\AbstractPlaceParsableService;
15
use Ivory\GoogleMap\Service\Place\Autocomplete\Request\PlaceAutocompleteRequestInterface;
16
use Ivory\GoogleMap\Service\Place\Autocomplete\Response\PlaceAutocompleteMatch;
17
use Ivory\GoogleMap\Service\Place\Autocomplete\Response\PlaceAutocompletePrediction;
18
use Ivory\GoogleMap\Service\Place\Autocomplete\Response\PlaceAutocompleteResponse;
19
use Ivory\GoogleMap\Service\Place\Autocomplete\Response\PlaceAutocompleteTerm;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class PlaceAutocompleteService extends AbstractPlaceParsableService
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function setHttps($https)
30
    {
31
        if (!$https) {
32
            throw new \InvalidArgumentException('The http scheme is not supported.');
33
        }
34
35
        parent::setHttps($https);
36
    }
37
38
    /**
39
     * @param PlaceAutocompleteRequestInterface $request
40
     *
41
     * @return PlaceAutocompleteResponse
42
     */
43 View Code Duplication
    public function process(PlaceAutocompleteRequestInterface $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...
44
    {
45
        $httpRequest = $this->createRequest($request);
46
        $httpResponse = $this->getClient()->sendRequest($httpRequest);
47
48
        $data = $this->parse((string) $httpResponse->getBody(), [
49
            'pluralization_rules' => [
50
                'matched_substring' => 'matched_substrings',
51
                'prediction'        => 'predictions',
52
                'term'              => 'terms',
53
                'type'              => 'types',
54
            ],
55
        ]);
56
57
        $response = $this->buildResponse($data);
58
        $response->setRequest($request);
59
60
        return $response;
61
    }
62
63
    /**
64
     * @param mixed[] $data
65
     *
66
     * @return PlaceAutocompleteResponse
67
     */
68
    private function buildResponse(array $data)
69
    {
70
        $response = new PlaceAutocompleteResponse();
71
        $response->setStatus($data['status']);
72
        $response->setPredictions($this->buildPredictions($data['predictions']));
73
74
        return $response;
75
    }
76
77
    /**
78
     * @param mixed[] $data
79
     *
80
     * @return PlaceAutocompletePrediction[]
81
     */
82
    private function buildPredictions(array $data)
83
    {
84
        $predictions = [];
85
86
        foreach ($data as $prediction) {
87
            $predictions[] = $this->buildPrediction($prediction);
88
        }
89
90
        return $predictions;
91
    }
92
93
    /**
94
     * @param mixed[] $data
95
     *
96
     * @return PlaceAutocompletePrediction
97
     */
98
    private function buildPrediction(array $data)
99
    {
100
        $prediction = new PlaceAutocompletePrediction();
101
        $prediction->setDescription($data['description']);
102
        $prediction->setTerms($this->buildTerms($data['terms']));
103
        $prediction->setMatches($this->buildMatches($data['matched_substrings']));
104
105
        if (isset($data['place_id'])) {
106
            $prediction->setPlaceId($data['place_id']);
107
        }
108
109
        if (isset($data['types'])) {
110
            $prediction->setTypes($data['types']);
111
        }
112
113
        return $prediction;
114
    }
115
116
    /**
117
     * @param mixed[] $data
118
     *
119
     * @return PlaceAutocompleteTerm[]
120
     */
121
    private function buildTerms(array $data)
122
    {
123
        $terms = [];
124
125
        foreach ($data as $term) {
126
            $terms[] = $this->buildTerm($term);
127
        }
128
129
        return $terms;
130
    }
131
132
    /**
133
     * @param mixed[] $data
134
     *
135
     * @return PlaceAutocompleteTerm
136
     */
137
    private function buildTerm(array $data)
138
    {
139
        $term = new PlaceAutocompleteTerm();
140
        $term->setValue($data['value']);
141
        $term->setOffset($data['offset']);
142
143
        return $term;
144
    }
145
146
    /**
147
     * @param mixed[] $data
148
     *
149
     * @return PlaceAutocompleteMatch[]
150
     */
151
    private function buildMatches(array $data)
152
    {
153
        $matches = [];
154
155
        foreach ($data as $match) {
156
            $matches[] = $this->buildMatch($match);
157
        }
158
159
        return $matches;
160
    }
161
162
    /**
163
     * @param mixed[] $data
164
     *
165
     * @return PlaceAutocompleteMatch
166
     */
167
    private function buildMatch(array $data)
168
    {
169
        $match = new PlaceAutocompleteMatch();
170
        $match->setLength($data['length']);
171
        $match->setOffset($data['offset']);
172
173
        return $match;
174
    }
175
}
176