Completed
Push — v3-fusion-support ( d44c19...cd5b17 )
by Steven
03:40
created

Client::createDefaultHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

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 1
eloc 4
nc 1
nop 0
crap 1
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
     * Creates new client
24
     *
25
     * @param array $options
26
     */
27 22
    public function __construct(array $options = array())
28
    {
29
        $defaults = [
30 22
            'accessToken' => null,
31
            'apiHost' => 'api.yelp.com'
32 11
        ];
33
34 22
        $this->parseConfiguration($options, $defaults);
35
36 22
        if (!$this->getHttpClient()) {
37 22
            $this->setHttpClient($this->createDefaultHttpClient());
38 11
        }
39 22
    }
40
41
    /**
42
     * Creates default http client with appropriate authorization header.
43
     *
44
     * @return GuzzleHttp\Client
45
     */
46 22
    protected function createDefaultHttpClient()
47
    {
48 22
        return new HttpClient([
49
            'headers' => [
50 22
                'Authorization' => 'Bearer ' . $this->accessToken,
51
            ]
52 11
        ]);
53
    }
54
55
    /**
56
     * Fetches results from the Autocomplete API.
57
     *
58
     * @param    array    $parameters
59
     *
60
     * @return   stdClass
61
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
62
     * @link     https://www.yelp.com/developers/documentation/v3/autocomplete
63
     */
64 2
    public function getAutocompleteResults($parameters = [])
65
    {
66 2
        $path = $this->appendParametersToUrl('/v3/autocomplete', $parameters);
67 2
        $request = $this->getRequest('GET', $path);
68
69 2
        return $this->processRequest($request);
70
    }
71
72
    /**
73
     * Fetches a specific business by id.
74
     *
75
     * @param    string    $businessId
76
     * @param    array     $parameters
77
     *
78
     * @return   stdClass
79
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
80
     * @link     https://www.yelp.com/developers/documentation/v3/business
81
     */
82 4
    public function getBusiness($businessId, $parameters = [])
83
    {
84 4
        $path = $this->appendParametersToUrl('/v3/businesses/'.$businessId, $parameters);
85 4
        $request = $this->getRequest('GET', $path);
86
87 4
        return $this->processRequest($request);
88
    }
89
90
    /**
91
     * Fetches reviews for a specific business by id.
92
     *
93
     * @param    string    $businessId
94
     * @param    array     $parameters
95
     *
96
     * @return   stdClass
97
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
98
     * @link     https://www.yelp.com/developers/documentation/v3/business_reviews
99
     */
100 2
    public function getBusinessReviews($businessId, $parameters = [])
101
    {
102 2
        $path = $this->appendParametersToUrl('/v3/businesses/'.$businessId.'/reviews', $parameters);
103 2
        $request = $this->getRequest('GET', $path);
104
105 2
        return $this->processRequest($request);
106
    }
107
108
    /**
109
     * Fetches results from the Business Search API.
110
     *
111
     * @param    array    $parameters
112
     *
113
     * @return   stdClass
114
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
115
     * @link     https://www.yelp.com/developers/documentation/v3/business_search
116
     */
117 2
    public function getBusinessesSearchResults($parameters = [])
118
    {
119
        $options = [
120 2
            'to_csv' => ['attributes', 'categories', 'price']
121 1
        ];
122
123 2
        $path = $this->appendParametersToUrl('/v3/businesses/search', $parameters, $options);
124 2
        $request = $this->getRequest('GET', $path);
125
126 2
        return $this->processRequest($request);
127
    }
128
129
    /**
130
     * Fetches results from the Business Search API by Phone.
131
     *
132
     * @param    string    $phoneNumber
133
     *
134
     * @return   stdClass
135
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
136
     * @link     https://www.yelp.com/developers/documentation/v3/business_search_phone
137
     */
138 2
    public function getBusinessesSearchResultsByPhone($phoneNumber)
139
    {
140
        $parameters = [
141 1
            'phone' => $phoneNumber
142 1
        ];
143
144 2
        $path = $this->appendParametersToUrl('/v3/businesses/search/phone', $parameters);
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 Type.
152
     *
153
     * @param    string    $type
154
     * @param    array     $parameters
155
     *
156
     * @return   stdClass
157
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
158
     * @link     https://www.yelp.com/developers/documentation/v3/transactions_search
159
     */
160 2
    public function getTransactionsSearchResultsByType($type, $parameters = [])
161
    {
162 2
        $path = $this->appendParametersToUrl('/v3/transactions/'.$type.'/search', $parameters);
163 2
        $request = $this->getRequest('GET', $path);
164
165 2
        return $this->processRequest($request);
166
    }
167
}
168