Completed
Pull Request — master (#25)
by Steven
02:25
created

Client::getBearerToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 1
cts 1
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 22
    protected $apiKey;
28
29
    /**
30 22
     * Creates new client
31
     *
32 11
     * @param array $options
33
     */
34 22
    public function __construct(array $options = array())
35
    {
36 22
        $defaults = [
37 22
            'accessToken' => null,
38 11
            'apiHost' => 'api.yelp.com',
39 22
            'apiKey' => null,
40
        ];
41
42
        $this->parseConfiguration($options, $defaults);
43
44
        if (!$this->getHttpClient()) {
45
            $this->setHttpClient($this->createDefaultHttpClient());
46 22
        }
47
    }
48 22
49
    /**
50 22
     * Creates default http client with appropriate authorization configuration.
51
     *
52 11
     * @return GuzzleHttp\Client
53
     */
54
    public function createDefaultHttpClient()
55
    {
56
        return new HttpClient([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \GuzzleHttp\C...s->getBearerToken()))); (GuzzleHttp\Client) is incompatible with the return type declared by the interface Stevenmaguire\Yelp\Contr...createDefaultHttpClient of type Stevenmaguire\Yelp\Contract\GuzzleHttp\Client.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
57
            'headers' => [
58
                'Authorization' => 'Bearer ' . $this->getBearerToken(),
59
            ]
60
        ]);
61
    }
62
63
    /**
64 2
     * Fetches results from the Autocomplete API.
65
     *
66 2
     * @param    array    $parameters
67 2
     *
68
     * @return   stdClass
69 2
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
70
     * @link     https://www.yelp.com/developers/documentation/v3/autocomplete
71
     */
72
    public function getAutocompleteResults($parameters = [])
73
    {
74
        $path = $this->appendParametersToUrl('/v3/autocomplete', $parameters);
75
        $request = $this->getRequest('GET', $path);
76
77
        return $this->processRequest($request);
78
    }
79
80
    /**
81
     * Returns the api key, if available, otherwise returns access token.
82 4
     *
83
     * @return string|null
84 4
     * @link https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going
85 4
     */
86
    private function getBearerToken()
87 4
    {
88
        if ($this->apiKey) {
89
            return $this->apiKey;
90
        }
91
92
        return $this->accessToken;
93
    }
94
95
    /**
96
     * Fetches a specific business by id.
97
     *
98
     * @param    string    $businessId
99
     * @param    array     $parameters
100 2
     *
101
     * @return   stdClass
102 2
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
103 2
     * @link     https://www.yelp.com/developers/documentation/v3/business
104
     */
105 2
    public function getBusiness($businessId, $parameters = [])
106
    {
107
        $path = $this->appendParametersToUrl('/v3/businesses/'.$businessId, $parameters);
108
        $request = $this->getRequest('GET', $path);
109
110
        return $this->processRequest($request);
111
    }
112
113
    /**
114
     * Fetches reviews for a specific business by id.
115
     *
116
     * @param    string    $businessId
117 2
     * @param    array     $parameters
118
     *
119 2
     * @return   stdClass
120
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
121 2
     * @link     https://www.yelp.com/developers/documentation/v3/business_reviews
122 2
     */
123
    public function getBusinessReviews($businessId, $parameters = [])
124 2
    {
125
        $path = $this->appendParametersToUrl('/v3/businesses/'.$businessId.'/reviews', $parameters);
126
        $request = $this->getRequest('GET', $path);
127
128
        return $this->processRequest($request);
129
    }
130
131
    /**
132
     * Fetches results from the Business Search API.
133
     *
134
     * @param    array    $parameters
135
     *
136 2
     * @return   stdClass
137
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
138
     * @link     https://www.yelp.com/developers/documentation/v3/business_search
139 1
     */
140 1
    public function getBusinessesSearchResults($parameters = [])
141
    {
142 2
        $csvParams = ['attributes', 'categories', 'price'];
143 2
144
        $path = $this->appendParametersToUrl('/v3/businesses/search', $parameters, $csvParams);
145 2
        $request = $this->getRequest('GET', $path);
146
147
        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 2
     */
159
    public function getBusinessesSearchResultsByPhone($phoneNumber)
160 2
    {
161 2
        $parameters = [
162
            'phone' => $phoneNumber
163 2
        ];
164
165
        $path = $this->appendParametersToUrl('/v3/businesses/search/phone', $parameters);
166
        $request = $this->getRequest('GET', $path);
167
168
        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
    public function getTransactionsSearchResultsByType($type, $parameters = [])
182
    {
183
        $path = $this->appendParametersToUrl('/v3/transactions/'.$type.'/search', $parameters);
184
        $request = $this->getRequest('GET', $path);
185
186
        return $this->processRequest($request);
187
    }
188
}
189