ZipFromPlaceIdAction::getEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Sfneal\GooglePlaces\Actions;
4
5
use Sfneal\GooglePlaces\Actions\Abstracts\PlacesSearchAction;
6
7
class ZipFromPlaceIdAction extends PlacesSearchAction
8
{
9
    /**
10
     * Google Places details endpoint.
11
     *
12
     * @return string
13
     */
14
    public function getEndpoint(): string
15
    {
16
        // Retrieve a city's zip code by inputting it's place ID
17
        $query = $this->place_id ?? $this->query;
18
19
        $endpoint = 'https://maps.googleapis.com/maps/api/place/details/json?placeid='.$query;
20
        $endpoint .= '&language=en_EN';
21
        $endpoint .= '&region=us';
22
        $endpoint .= '&fields=address_component';
23
        $endpoint .= '&key='.config('google-places.api_key');
24
25
        return $endpoint;
26
    }
27
28
    /**
29
     * Parse the Google Places API response and return an array.
30
     *
31
     * @param $response
32
     * @return string
33
     */
34
    public function parseResponse($response): string
35
    {
36
        // Parse predictions
37
        foreach ($response['result']['address_components'] as $res) {
38
            if (in_array('postal_code', $res['types'])) {
39
                return $res['short_name'];
40
            }
41
        }
42
    }
43
}
44