Completed
Branch v3-fusion-support (d44c19)
by Steven
03:37
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace Stevenmaguire\Yelp\v2;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Subscriber\Oauth\Oauth1;
9
use Stevenmaguire\Yelp\Contract\Http as HttpContract;
10
use Stevenmaguire\Yelp\Exception\HttpException;
11
use Stevenmaguire\Yelp\Tool\ConfigurationTrait;
12
use Stevenmaguire\Yelp\Tool\HttpTrait;
13
14
class Client implements HttpContract
15
{
16
    use ConfigurationTrait,
17
        HttpTrait;
18
19
    /**
20
     * Consumer key
21
     *
22
     * @var string
23
     */
24
    protected $consumerKey;
25
26
    /**
27
     * Consumer secret
28
     *
29
     * @var string
30
     */
31
    protected $consumerSecret;
32
33
    /**
34
     * Access token
35
     *
36
     * @var string
37
     */
38
    protected $token;
39
40
    /**
41
     * Access token secret
42
     *
43
     * @var string
44
     */
45
    protected $tokenSecret;
46
47
    /**
48
     * Default search term
49
     *
50
     * @var string
51
     */
52
    protected $defaultTerm = 'bar';
53
54
    /**
55
     * Default location
56
     *
57
     * @var string
58
     */
59
    protected $defaultLocation = 'Chicago, IL';
60
61
    /**
62
     * Default search limit
63
     *
64
     * @var integer
65
     */
66
    protected $searchLimit = 3;
67
68
    /**
69
     * Search path
70
     *
71
     * @var string
72
     */
73
    protected $searchPath = '/v2/search/';
74
75
    /**
76
     * Business path
77
     *
78
     * @var string
79
     */
80
    protected $businessPath = '/v2/business/';
81
82
    /**
83
     * Phone search path
84
     *
85
     * @var string
86
     */
87
    protected $phoneSearchPath = '/v2/phone_search/';
88
89
    /**
90
     * Create new client
91
     *
92
     * @param array $options
93
     */
94
    public function __construct(array $options = array())
95
    {
96
        $defaults = array(
97
            'consumerKey' => null,
98
            'consumerSecret' => null,
99
            'token' => null,
100
            'tokenSecret' => null,
101
            'apiHost' => 'api.yelp.com'
102
        );
103
104
        $this->parseConfiguration($options, $defaults)
105
            ->createHttpClient();
106
    }
107
108
    /**
109
     * Build query string params using defaults
110
     *
111
     * @param  array $attributes
112
     *
113
     * @return string
114
     */
115
    public function buildQueryParams($attributes = [])
116
    {
117
        $defaults = array(
118
            'term' => $this->defaultTerm,
119
            'location' => $this->defaultLocation,
120
            'limit' => $this->searchLimit
121
        );
122
        $attributes = array_merge($defaults, $attributes);
123
124
        return $this->prepareQueryParams($attributes);
125
    }
126
127
    /**
128
     * Build unsigned url
129
     *
130
     * @param  string   $host
131
     * @param  string   $path
132
     *
133
     * @return string   Unsigned url
134
     */
135
    protected function buildUnsignedUrl($host, $path)
136
    {
137
        return "http://" . $host . $path;
138
    }
139
140
    /**
141
     * Builds and sets a preferred http client.
142
     *
143
     * @return Client
144
     */
145
    protected function createHttpClient()
146
    {
147
        $stack = HandlerStack::create();
148
149
        $middleware = new Oauth1([
150
            'consumer_key'    => $this->consumerKey,
151
            'consumer_secret' => $this->consumerSecret,
152
            'token'           => $this->token,
153
            'token_secret'    => $this->tokenSecret
154
        ]);
155
156
        $stack->push($middleware);
157
158
        $client = new HttpClient([
159
            'handler' => $stack
160
        ]);
161
162
        return $this->setHttpClient($client);
163
    }
164
165
    /**
166
     * Query the Business API by business id
167
     *
168
     * @param    string   $businessId      The ID of the business to query
169
     * @param    array    $attributes      Optional attributes to include in query string
170
     *
171
     * @return   stdClass                   The JSON response from the request
172
     */
173
    public function getBusiness($businessId, $attributes = [])
174
    {
175
        $businessPath = $this->businessPath . urlencode($businessId) . "?" . $this->prepareQueryParams($attributes);
176
177
        return $this->request($businessPath);
178
    }
179
180
    /**
181
     * Makes a request to the Yelp API and returns the response
182
     *
183
     * @param    string $path    The path of the APi after the domain
184
     *
185
     * @return   stdClass The JSON response from the request
186
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
187
     */
188
    protected function request($path)
189
    {
190
        $url = $this->buildUnsignedUrl($this->apiHost, $path);
191
192
        try {
193
            $response = $this->getHttpClient()->get($url, ['auth' => 'oauth']);
194
        } catch (ClientException $e) {
195
            $exception = new HttpException($e->getMessage());
196
197
            throw $exception->setResponseBody($e->getResponse()->getBody());
198
        }
199
200
        return json_decode($response->getBody());
201
    }
202
203
    /**
204
     * Query the Search API by a search term and location
205
     *
206
     * @param    array    $attributes   Query attributes
207
     *
208
     * @return   stdClass               The JSON response from the request
209
     */
210
    public function search($attributes = [])
211
    {
212
        $query_string = $this->buildQueryParams($attributes);
213
        $searchPath = $this->searchPath . "?" . $query_string;
214
215
        return $this->request($searchPath);
216
    }
217
218
    /**
219
     * Search for businesses by phone number
220
     *
221
     * @see https://www.yelp.com/developers/documentation/v2/phone_search
222
     *
223
     * @param    array    $attributes   Query attributes
224
     *
225
     * @return   stdClass               The JSON response from the request
226
     */
227
    public function searchByPhone($attributes = [])
228
    {
229
        $searchPath = $this->phoneSearchPath . "?" . $this->prepareQueryParams($attributes);
230
231
        return $this->request($searchPath);
232
    }
233
234
    /**
235
     * Set default location
236
     *
237
     * @param string $location
238
     *
239
     * @return Client
240
     */
241
    public function setDefaultLocation($location)
242
    {
243
        $this->defaultLocation = $location;
244
        return $this;
245
    }
246
247
    /**
248
     * Set default term
249
     *
250
     * @param string $term
251
     *
252
     * @return Client
253
     */
254
    public function setDefaultTerm($term)
255
    {
256
        $this->defaultTerm = $term;
257
        return $this;
258
    }
259
260
    /**
261
     * Set search limit
262
     *
263
     * @param integer $limit
264
     *
265
     * @return Client
266
     */
267
    public function setSearchLimit($limit)
268
    {
269
        if (is_int($limit)) {
270
            $this->searchLimit = $limit;
271
        }
272
        return $this;
273
    }
274
}
275