Passed
Push — master ( 25ef95...88bd2b )
by Stephen
02:06
created

PlacesSearchAction::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace Sfneal\GooglePlaces\Actions\Abstracts;
5
6
7
use Sfneal\Actions\AbstractAction;
8
use Sfneal\GooglePlaces\Actions\CurlRequestAction;
9
10
abstract class PlacesSearchAction extends AbstractAction
11
{
12
    /**
13
     * @var array Longitude & Latitude of the location biases (Milford, MA)
14
     */
15
    private $location_bias_coords;
16
17
    /**
18
     * @var int A radius in miles from location bias coordinates
19
     */
20
    private $radius_bias;
21
22
    /**
23
     * @var string Google places country code (defaults to 'us')
24
     */
25
    private $country;
26
27
    /**
28
     * @var string Query parameters
29
     */
30
    public $query;
31
32
    /**
33
     * @var array Places predictions results
34
     */
35
    public $results;
36
37
    /**
38
     * @var null Place ID for retrieving a zip or other information about a place
39
     */
40
    public $place_id;
41
42
    /**
43
     * PlacesSearchAction constructor.
44
     *
45
     * @param string $query
46
     * @param null $place_id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $place_id is correct as it would always require null to be passed?
Loading history...
47
     */
48
    public function __construct(string $query, $place_id = null)
49
    {
50
        // Set params
51
        $this->query = self::sanitize($query);
52
        $this->place_id = $place_id;
53
54
        // Set optional env values
55
        $this->location_bias_coords = env('GOOGLE_PLACES_LOCATION_BIAS_COORD');
0 ignored issues
show
Documentation Bug introduced by
It seems like env('GOOGLE_PLACES_LOCATION_BIAS_COORD') of type PhpOption\S or PhpOption\T is incompatible with the declared type array of property $location_bias_coords.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
        $this->radius_bias = env('GOOGLE_PLACES_RADIUS');
0 ignored issues
show
Documentation Bug introduced by
It seems like env('GOOGLE_PLACES_RADIUS') of type PhpOption\S or PhpOption\T is incompatible with the declared type integer of property $radius_bias.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->country = env('GOOGLE_PLACES_COUNTRY', 'us');
58
    }
59
60
    /**
61
     * Retrieve the API endpoint for a google places AutocompleteCity search
62
     *
63
     * @return string
64
     */
65
    public function getEndpoint(): string {
66
        $endpoint = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=' . $this->query;
67
        $endpoint .= '&types=(regions)';
68
        $endpoint .= '&region=us';
69
70
        // Add location bias if set
71
        if (isset($this->location_bias_coords)) {
72
            $endpoint .= '&location=' . str_replace(' ', '', $this->location_bias_coords);
0 ignored issues
show
Bug introduced by
Are you sure str_replace(' ', '', $this->location_bias_coords) of type string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            $endpoint .= '&location=' . /** @scrutinizer ignore-type */ str_replace(' ', '', $this->location_bias_coords);
Loading history...
73
        }
74
75
        // Add radius bias if set
76
        if (isset($this->radius_bias)) {
77
            $endpoint .= '&radius=' . $this->radius_bias;
78
        }
79
80
        // Add language
81
        $endpoint .= '&language=en_EN';
82
        $endpoint .= '&components=country:' . $this->country;
83
        $endpoint .= '&key=' . env('GOOGLE_API_KEY');
84
        return $endpoint;
85
    }
86
87
    /**
88
     * Parse the Google Places API response to retrieve results set
89
     *
90
     * @param $response
91
     * @return array|string
92
     */
93
    abstract public function parseResponse($response);
94
95
    /**
96
     * Remove spaces and forbidden characters from query string
97
     *
98
     * @param $query
99
     * @return string|string[]
100
     */
101
    private static function sanitize($query) {
102
        return str_replace(' ', '-', $query);
103
    }
104
105
    /**
106
     * Retrieve the API URL endpoint, cURL execute the request and return response
107
     *
108
     * @return mixed
109
     */
110
    public function execute() {
111
        return $this->parseResponse(CurlRequestAction::execute($this->getEndpoint()));
112
    }
113
}
114