Completed
Push — master ( e81d9a...17c9bd )
by Steven
03:15
created

Client::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php namespace Stevenmaguire\Yelp;
2
3
use GuzzleHttp\Client as HttpClient;
4
use GuzzleHttp\HandlerStack;
5
use GuzzleHttp\Subscriber\Oauth\Oauth1;
6
use GuzzleHttp\Exception\ClientException;
7
8
class Client
9
{
10
    /**
11
     * API host url
12
     *
13
     * @var string
14
     */
15
    protected $apiHost;
16
17
    /**
18
     * Consumer key
19
     *
20
     * @var string
21
     */
22
    protected $consumerKey;
23
24
    /**
25
     * Consumer secret
26
     *
27
     * @var string
28
     */
29
    protected $consumerSecret;
30
31
    /**
32
     * Access token
33
     *
34
     * @var string
35
     */
36
    protected $token;
37
38
    /**
39
     * Access token secret
40
     *
41
     * @var string
42
     */
43
    protected $tokenSecret;
44
45
    /**
46
     * Default search term
47
     *
48
     * @var string
49
     */
50
    protected $defaultTerm = 'bar';
51
52
    /**
53
     * Default location
54
     *
55
     * @var string
56
     */
57
    protected $defaultLocation = 'Chicago, IL';
58
59
    /**
60
     * Default search limit
61
     *
62
     * @var integer
63
     */
64
    protected $searchLimit = 3;
65
66
    /**
67
     * Search path
68
     *
69
     * @var string
70
     */
71
    protected $searchPath = '/v2/search/';
72
73
    /**
74
     * Business path
75
     *
76
     * @var string
77
     */
78
    protected $businessPath = '/v2/business/';
79
80
    /**
81
     * Phone search path
82
     *
83
     * @var string
84
     */
85
    protected $phoneSearchPath = '/v2/phone_search/';
86
87
    /**
88
     * [$httpClient description]
89
     *
90
     * @var [type]
91
     */
92
    protected $httpClient;
93
94
    /**
95
     * Create new client
96
     *
97
     * @param array $configuration
98
     */
99 16
    public function __construct($configuration = [])
100
    {
101 16
        $this->parseConfiguration($configuration);
102
103
        /*
104
        $this->consumerKey = $configuration['consumerKey'];
105
        $this->consumerSecret = $configuration['consumerSecret'];
106
        $this->token = $configuration['token'];
107
        $this->tokenSecret = $configuration['tokenSecret'];
108
        $this->apiHost = $configuration['apiHost'];
109
        */
110 16
        $this->createHttpClient();
111 16
    }
112
113
    /**
114
     * Build query string params using defaults
115
     *
116
     * @param  array $attributes
117
     *
118
     * @return string
119
     */
120 4
    public function buildQueryParams($attributes = [])
121
    {
122
        $defaults = array(
123 4
            'term' => $this->defaultTerm,
124 4
            'location' => $this->defaultLocation,
125 4
            'limit' => $this->searchLimit
126 4
        );
127 4
        $attributes = array_merge($defaults, $attributes);
128
129 4
        return http_build_query($attributes);
130
    }
131
132
    /**
133
     * Build unsigned url
134
     *
135
     * @param  string   $host
136
     * @param  string   $path
137
     *
138
     * @return string   Unsigned url
139
     */
140 14
    protected function buildUnsignedUrl($host, $path)
141
    {
142 14
        return "http://" . $host . $path;
143
    }
144
145
    /**
146
     * Builds and sets a preferred http client.
147
     *
148
     * @return Client
149
     */
150 16
    protected function createHttpClient()
151
    {
152 16
        $stack = HandlerStack::create();
153
154 16
        $middleware = new Oauth1([
155 16
            'consumer_key'    => $this->consumerKey,
156 16
            'consumer_secret' => $this->consumerSecret,
157 16
            'token'           => $this->token,
158 16
            'token_secret'    => $this->tokenSecret
159 16
        ]);
160
161 16
        $stack->push($middleware);
162
163 16
        $client = new HttpClient([
164
            'handler' => $stack
165 16
        ]);
166
167 16
        return $this->setHttpClient($client);
168
    }
169
170
    /**
171
     * Query the Business API by business id
172
     *
173
     * @param    string   $businessId      The ID of the business to query
174
     *
175
     * @return   stdClass                   The JSON response from the request
176
     */
177 8
    public function getBusiness($businessId)
178
    {
179 8
        $businessPath = $this->businessPath . urlencode($businessId);
180
181 8
        return $this->request($businessPath);
182
    }
183
184
    /**
185
     * Maps legacy configuration keys to updated keys.
186
     *
187
     * @param  array   $configuration
188
     *
189
     * @return array
190
     */
191 16
    protected function mapConfiguration(array $configuration)
192
    {
193
        $map = [
194 16
            'consumer_key' => 'consumerKey',
195 16
            'consumer_secret' => 'consumerSecret',
196 16
            'token_secret' => 'tokenSecret',
197 16
            'api_host' => 'apiHost',
198 16
        ];
199
200 16
        array_walk($map, function ($value, $key) use (&$configuration) {
201 16
            if (isset($configuration[$key])) {
202 2
                $configuration[$value] = $configuration[$key];
203 2
            }
204 16
        });
205
206 16
        return $configuration;
207
    }
208
209
    /**
210
     * Parse configuration using defaults
211
     *
212
     * @param  array $configuration
213
     *
214
     * @return array $configuration
215
     */
216 16
    protected function parseConfiguration(&$configuration = [])
217
    {
218
        $defaults = array(
219 16
            'consumerKey' => null,
220 16
            'consumerSecret' => null,
221 16
            'token' => null,
222 16
            'tokenSecret' => null,
223
            'apiHost' => 'api.yelp.com'
224 16
        );
225
226 16
        $configuration = array_merge(
227 16
            $defaults,
228 16
            $this->mapConfiguration($configuration)
229 16
        );
230
231 16
        array_walk($configuration, [$this, 'setConfig']);
232 16
    }
233
234
    /**
235
     * Makes a request to the Yelp API and returns the response
236
     *
237
     * @param    string $path    The path of the APi after the domain
238
     *
239
     * @return   stdClass The JSON response from the request
240
     * @throws   Exception
241
     */
242 14
    protected function request($path)
243
    {
244 14
        $url = $this->buildUnsignedUrl($this->apiHost, $path);
245
246
        try {
247 14
            $response = $this->httpClient->get($url, ['auth' => 'oauth']);
248 14
        } catch (ClientException $e) {
249 4
            $exception = new Exception($e->getMessage());
250
251 4
            throw $exception->setResponseBody($e->getResponse()->getBody());
252
        }
253
254 10
        return json_decode($response->getBody());
255
    }
256
257
    /**
258
     * Query the Search API by a search term and location
259
     *
260
     * @param    array    $attributes   Query attributes
261
     *
262
     * @return   stdClass               The JSON response from the request
263
     */
264 4
    public function search($attributes = [])
265
    {
266 4
        $query_string = $this->buildQueryParams($attributes);
267 4
        $searchPath = $this->searchPath . "?" . $query_string;
268
269 4
        return $this->request($searchPath);
270
    }
271
272
    /**
273
     * Search for businesses by phone number
274
     *
275
     * @see https://www.yelp.com/developers/documentation/v2/phone_search
276
     *
277
     * @param    array    $attributes   Query attributes
278
     *
279
     * @return   stdClass               The JSON response from the request
280
     */
281 2
    public function searchByPhone($attributes = [])
282
    {
283 2
        $searchPath = $this->phoneSearchPath . "?" . http_build_query($attributes);
284
285 2
        return $this->request($searchPath);
286
    }
287
288
    /**
289
     * Attempts to set a given value.
290
     *
291
     * @param mixed   $value
292
     * @param string  $key
293
     *
294
     * @return Client
295
     */
296 16
    protected function setConfig($value, $key)
297
    {
298 16
        if (property_exists($this, $key)) {
299 16
            $this->$key = $value;
300 16
        }
301
302 16
        return $this;
303
    }
304
305
    /**
306
     * Set default location
307
     *
308
     * @param string $location
309
     *
310
     * @return Client
311
     */
312 2
    public function setDefaultLocation($location)
313
    {
314 2
        $this->defaultLocation = $location;
315 2
        return $this;
316
    }
317
318
    /**
319
     * Set default term
320
     *
321
     * @param string $term
322
     *
323
     * @return Client
324
     */
325 2
    public function setDefaultTerm($term)
326
    {
327 2
        $this->defaultTerm = $term;
328 2
        return $this;
329
    }
330
331
    /**
332
     * Updates the yelp client's http client to the given http client. Client.
333
     *
334
     * @param HttpClient  $client
335
     *
336
     * @return  Client
337
     */
338 16
    public function setHttpClient(HttpClient $client)
339
    {
340 16
        $this->httpClient = $client;
341
342 16
        return $this;
343
    }
344
345
    /**
346
     * Set search limit
347
     *
348
     * @param integer $limit
349
     *
350
     * @return Client
351
     */
352 2
    public function setSearchLimit($limit)
353
    {
354 2
        if (is_int($limit)) {
355 2
            $this->searchLimit = $limit;
356 2
        }
357 2
        return $this;
358
    }
359
360
    /**
361
     * Retrives the value of a given property from the client.
362
     *
363
     * @param  string  $property
364
     *
365
     * @return mixed|null
366
     */
367 2
    public function __get($property)
368
    {
369 2
        if (property_exists($this, $property)) {
370 2
            return $this->$property;
371
        }
372
373
        return null;
374
    }
375
}
376