Completed
Push — master ( 5d8d53...9f2587 )
by Steven
11s
created

Client::getBearerToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Stevenmaguire\Yelp\v3;
4
5
use GuzzleHttp\Client as HttpClient;
6
use Stevenmaguire\Yelp\Contract\Http as HttpContract;
7
use Stevenmaguire\Yelp\Tool\ConfigurationTrait;
8
use Stevenmaguire\Yelp\Tool\HttpTrait;
9
10
class Client implements HttpContract
11
{
12
    use ConfigurationTrait,
13
        HttpTrait;
14
15
    /**
16
     * Access token
17
     *
18
     * @var string
19
     */
20
    protected $accessToken;
21
22
    /**
23
     * Api key
24
     *
25
     * @var string
26
     */
27
    protected $apiKey;
28
29
    /**
30
     * Creates new client
31
     *
32
     * @param array $options
33
     */
34 26
    public function __construct(array $options = array())
35
    {
36
        $defaults = [
37 26
            'accessToken' => null,
38 13
            'apiHost' => 'api.yelp.com',
39 13
            'apiKey' => null,
40 13
        ];
41
42 26
        $this->parseConfiguration($options, $defaults);
43
44 26
        if (!$this->getHttpClient()) {
45 26
            $this->setHttpClient($this->createDefaultHttpClient());
46 13
        }
47 26
    }
48
49
    /**
50
     * Creates default http client with appropriate authorization configuration.
51
     *
52
     * @return \GuzzleHttp\Client
53
     */
54 26
    public function createDefaultHttpClient()
55 6
    {
56 26
        return new HttpClient([
57
            'headers' => [
58 26
                'Authorization' => 'Bearer ' . $this->getBearerToken(),
59
            ]
60 13
        ]);
61
    }
62
63
    /**
64
     * Fetches results from the Autocomplete API.
65
     *
66
     * @param    array    $parameters
67
     *
68
     * @return   stdClass
69
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
70
     * @link     https://www.yelp.com/developers/documentation/v3/autocomplete
71
     */
72 2
    public function getAutocompleteResults($parameters = [])
73
    {
74 2
        $path = $this->appendParametersToUrl('/v3/autocomplete', $parameters);
75 2
        $request = $this->getRequest('GET', $path);
76
77 2
        return $this->processRequest($request);
78
    }
79
80
    /**
81
     * Returns the api key, if available, otherwise returns access token.
82
     *
83
     * @return string|null
84
     * @link https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going
85
     */
86 26
    private function getBearerToken()
87
    {
88 26
        if ($this->apiKey) {
89 24
            return $this->apiKey;
90
        }
91
92 4
        return $this->accessToken;
93
    }
94
95
    /**
96
     * Fetches a specific business by id.
97
     *
98
     * @param    string    $businessId
99
     * @param    array     $parameters
100
     *
101
     * @return   stdClass
102
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
103
     * @link     https://www.yelp.com/developers/documentation/v3/business
104
     */
105 4
    public function getBusiness($businessId, $parameters = [])
106
    {
107 4
        $path = $this->appendParametersToUrl('/v3/businesses/'.$businessId, $parameters);
108 4
        $request = $this->getRequest('GET', $path);
109
110 4
        return $this->processRequest($request);
111
    }
112
113
    /**
114
     * Fetches reviews for a specific business by id.
115
     *
116
     * @param    string    $businessId
117
     * @param    array     $parameters
118
     *
119
     * @return   stdClass
120
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
121
     * @link     https://www.yelp.com/developers/documentation/v3/business_reviews
122
     */
123 2
    public function getBusinessReviews($businessId, $parameters = [])
124
    {
125 2
        $path = $this->appendParametersToUrl('/v3/businesses/'.$businessId.'/reviews', $parameters);
126 2
        $request = $this->getRequest('GET', $path);
127
128 2
        return $this->processRequest($request);
129
    }
130
131
    /**
132
     * Fetches results from the Business Search API.
133
     *
134
     * @param    array    $parameters
135
     *
136
     * @return   stdClass
137
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
138
     * @link     https://www.yelp.com/developers/documentation/v3/business_search
139
     */
140 2
    public function getBusinessesSearchResults($parameters = [])
141
    {
142 2
        $csvParams = ['attributes', 'categories', 'price'];
143
144 2
        $path = $this->appendParametersToUrl('/v3/businesses/search', $parameters, $csvParams);
145 2
        $request = $this->getRequest('GET', $path);
146
147 2
        return $this->processRequest($request);
148
    }
149
150
    /**
151
     * Fetches results from the Business Search API by Phone.
152
     *
153
     * @param    string    $phoneNumber
154
     *
155
     * @return   stdClass
156
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
157
     * @link     https://www.yelp.com/developers/documentation/v3/business_search_phone
158
     */
159 2
    public function getBusinessesSearchResultsByPhone($phoneNumber)
160
    {
161
        $parameters = [
162 1
            'phone' => $phoneNumber
163 1
        ];
164
165 2
        $path = $this->appendParametersToUrl('/v3/businesses/search/phone', $parameters);
166 2
        $request = $this->getRequest('GET', $path);
167
168 2
        return $this->processRequest($request);
169
    }
170
171
    /**
172
     * Fetches results from the Business Search API by Type.
173
     *
174
     * @param    string    $type
175
     * @param    array     $parameters
176
     *
177
     * @return   stdClass
178
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
179
     * @link     https://www.yelp.com/developers/documentation/v3/transactions_search
180
     */
181 2
    public function getTransactionsSearchResultsByType($type, $parameters = [])
182
    {
183 2
        $path = $this->appendParametersToUrl('/v3/transactions/'.$type.'/search', $parameters);
184 2
        $request = $this->getRequest('GET', $path);
185
186 2
        return $this->processRequest($request);
187
    }
188
}
189