PlaceClient   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 120
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A details() 0 8 1
A photo() 0 13 3
A add() 0 24 2
A delete() 0 11 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/google-places-library project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\Google\Places\Client;
13
14
use GuzzleHttp\Exception\RequestException;
15
16
/**
17
 * Class Place handles place details requests and common actions.
18
 *
19
 * @author Antonio Ramirez <[email protected]>
20
 *
21
 * @link http://www.ramirezcobos.com/
22
 * @link http://www.2amigos.us/
23
 */
24
class PlaceClient extends AbstractClient
25
{
26
    /**
27
     * Returns the details of a place.
28
     *
29
     * @see https://developers.google.com/places/documentation/details
30
     * @see https://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1
31
     *
32
     * @param string $placeid  the place id
33
     * @param string $language the language to return the results. Defaults to 'en' (english).
34
     * @param array  $params   optional parameters
35
     *
36
     * @throws RequestException if the request fails
37
     *
38
     * @return mixed|null
39
     */
40
    public function details($placeid, $language = 'en', $params = [])
41
    {
42
        $params['placeid'] = $placeid;
43
        $params['language'] = $language;
44
        $params['sensor'] = $this->getParamValue($params, 'sensor', 'false');
45
46
        return $this->request('details', 'get', $params);
47
    }
48
49
    /**
50
     * Returns a photo content.
51
     *
52
     * @see https://developers.google.com/places/documentation/photos#place_photo_requests
53
     *
54
     * @param string $reference string identifier that uniquely identifies a photo. Photo references are returned from
55
     *                          either a [[Search::text]], [[Search::nearby]], [[Search::radar]] or [[Place::details]] request.
56
     * @param array  $params    optional parameters.
57
     *
58
     * @throws \InvalidArgumentException
59
     * @throws RequestException          if the request fails
60
     *
61
     * @return mixed|null
62
     */
63
    public function photo($reference, $params = [])
64
    {
65
        if (!isset($params['maxheight']) && !isset($params['maxwidth'])) {
66
            throw new \InvalidArgumentException('You must set "maxheight" or "maxwidth".');
67
        }
68
        $params['photoreference'] = $reference;
69
        $params['key'] = $this->key;
70
        $url = str_replace('/{format}', '', $this->api);
71
        $url = str_replace('{cmd}', 'photo', $url);
72
        $response = $this->getGuzzleClient()->get($url, ['query' => $params]);
73
74
        return $response->getBody();
75
    }
76
77
    /**
78
     * Adds a place on Google's places database for your application. This function only works with JSON formats, that
79
     * means that no matter what you set the [[$format]] to work with, it will be superseded by 'json' type.
80
     *
81
     * @see https://developers.google.com/places/documentation/actions#adding_a_place
82
     *
83
     * @param array  $location The textual latitude/longitude value from which you wish to add new place information.
84
     * @param string $name     The full text name of the place. Limited to 255 characters.
85
     * @param array  $types    The category in which this place belongs.
86
     * @param string $accuracy The accuracy of the location signal on which this request is based, expressed in meters.
87
     * @param string $language The language in which the place's name is being reported.
88
     * @param array  $params   The extra recommended but not required parameters (ie address, phone_number, and website)
89
     *
90
     * @throws \InvalidArgumentException
91
     * @throws RequestException          if the request fails
92
     *
93
     * @return array
94
     */
95
    public function add(array $location, $name, array $types, $accuracy, $language = 'en', array $params = [])
96
    {
97
        if (strlen($name) > 255) {
98
            throw new \InvalidArgumentException('"$name" cannot be larger than 255 chars');
99
        }
100
        $types = (array) $types;
101
        $data = $params;
102
        $data['location'] = $location;
103
        $data['name'] = $name;
104
        $data['types'] = $types;
105
        $data['accuracy'] = $accuracy;
106
        $data['language'] = $language;
107
108
        return $this->request(
109
            'add',
110
            'post',
111
            [
112
                'key' => $this->key,
113
            ],
114
            [
115
                'body' => json_encode($data),
116
            ]
117
        );
118
    }
119
120
    /**
121
     * Deletes a place. A place can only be deleted if:
122
     * - It was added by the same application as is requesting its deletion.
123
     * - It has not successfully passed through the Google Maps moderation process, and and is therefore not visible to
124
     * all applications.
125
     *
126
     * @param string $reference The textual identifier that uniquely identifies this place
127
     *
128
     * @throws RequestException if the request fails
129
     *
130
     * @return array
131
     */
132
    public function delete($reference)
133
    {
134
        return $this->request(
135
            'delete',
136
            'post',
137
            [
138
                'key' => $this->key,
139
            ],
140
            ['body' => json_encode(['reference' => $reference])]
141
        );
142
    }
143
}
144