Completed
Pull Request — master (#20)
by Steven
07:01 queued 05:18
created

Client::setDefaultTerm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Stevenmaguire\Yelp\v2;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\Exception\BadResponseException;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Subscriber\Oauth\Oauth1;
9
use Psr\Http\Message\RequestInterface;
10
use Stevenmaguire\Yelp\Contract\Http as HttpContract;
11
use Stevenmaguire\Yelp\Exception\HttpException;
12
use Stevenmaguire\Yelp\Tool\ConfigurationTrait;
13
use Stevenmaguire\Yelp\Tool\HttpTrait;
14
15
class Client implements HttpContract
16
{
17
    use ConfigurationTrait,
18
        HttpTrait;
19
20
    /**
21
     * Consumer key
22
     *
23
     * @var string
24
     */
25
    protected $consumerKey;
26
27
    /**
28
     * Consumer secret
29
     *
30
     * @var string
31
     */
32
    protected $consumerSecret;
33
34
    /**
35
     * Access token
36
     *
37
     * @var string
38
     */
39
    protected $token;
40
41
    /**
42
     * Access token secret
43
     *
44
     * @var string
45
     */
46
    protected $tokenSecret;
47
48
    /**
49
     * Default search term
50
     *
51
     * @var string
52
     */
53
    protected $defaultTerm = 'bar';
54
55
    /**
56
     * Default location
57
     *
58
     * @var string
59
     */
60
    protected $defaultLocation = 'Chicago, IL';
61
62
    /**
63
     * Default search limit
64
     *
65
     * @var integer
66
     */
67
    protected $searchLimit = 3;
68
69
    /**
70
     * Create new client
71
     *
72
     * @param array $options
73
     */
74
    public function __construct(array $options = array())
75
    {
76
        $defaults = array(
77
            'consumerKey' => null,
78
            'consumerSecret' => null,
79
            'token' => null,
80
            'tokenSecret' => null,
81
            'apiHost' => 'api.yelp.com',
82
            // 'scheme' => 'http'
83
        );
84
85
        $this->parseConfiguration($options, $defaults);
86
87
        if (!$this->getHttpClient()) {
88
            $this->setHttpClient($this->createDefaultHttpClient());
89
        }
90
    }
91
92
    /**
93
     * Creates default http client with appropriate authorization configuration.
94
     *
95
     * @return HttpClient
96
     */
97
    public function createDefaultHttpClient()
98
    {
99
        $stack = HandlerStack::create();
100
101
        $middleware = new Oauth1([
102
            'consumer_key'    => $this->consumerKey,
103
            'consumer_secret' => $this->consumerSecret,
104
            'token'           => $this->token,
105
            'token_secret'    => $this->tokenSecret
106
        ]);
107
108
        $stack->push($middleware);
109
110
        return new HttpClient([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \GuzzleHttp\C...('handler' => $stack)); (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...
111
            'handler' => $stack
112
        ]);
113
    }
114
115
    /**
116
     * Fetches a specific business by id.
117
     *
118
     * @param    string    $businessId
119
     * @param    array     $parameters
120
     *
121
     * @return   stdClass
122
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
123
     */
124
    public function getBusiness($businessId, $parameters = [])
125
    {
126
        $path = $this->appendParametersToUrl('/v2/business/'.$businessId, $parameters);
127
        $request = $this->getRequest('GET', $path);
128
129
        return $this->processRequest($request);
130
    }
131
132
    /**
133
     * Sends a request instance and returns a response instance.
134
     *
135
     * WARNING: This method does not attempt to catch exceptions caused by HTTP
136
     * errors! It is recommended to wrap this method in a try/catch block.
137
     *
138
     * @param  RequestInterface $request
139
     * @return ResponseInterface
140
     * @throws Stevenmaguire\Yelp\Exception\HttpException
141
     */
142
    public function getResponse(RequestInterface $request)
143
    {
144
        try {
145
            return $this->getHttpClient()->get((string) $request->getUri(), ['auth' => 'oauth']);
146
        } catch (BadResponseException $e) {
147
            $exception = new HttpException($e->getMessage());
148
149
            throw $exception->setResponseBody((string) $e->getResponse()->getBody());
150
        }
151
    }
152
153
    /**
154
     * Fetches results from the Business Search API.
155
     *
156
     * @param    array    $parameters
157
     *
158
     * @return   stdClass
159
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
160
     */
161
    public function search($parameters = [])
162
    {
163
        $parameters = array_merge([
164
            'term' => $this->defaultTerm,
165
            'location' => $this->defaultLocation,
166
            'limit' => $this->searchLimit
167
        ], $parameters);
168
169
        $path = $this->appendParametersToUrl('/v2/search', $parameters);
170
        $request = $this->getRequest('GET', $path);
171
172
        return $this->processRequest($request);
173
    }
174
175
    /**
176
     * Fetches results from the Business Search API by Phone.
177
     *
178
     * @param    array    $parameters
179
     *
180
     * @return   stdClass
181
     * @throws   Stevenmaguire\Yelp\Exception\HttpException
182
     */
183
    public function searchByPhone($parameters = [])
184
    {
185
        $path = $this->appendParametersToUrl('/v2/phone_search', $parameters);
186
        $request = $this->getRequest('GET', $path);
187
188
        return $this->processRequest($request);
189
    }
190
191
    /**
192
     * Set default location
193
     *
194
     * @param string $location
195
     *
196
     * @return Client
197
     */
198
    public function setDefaultLocation($location)
199
    {
200
        $this->defaultLocation = $location;
201
202
        return $this;
203
    }
204
205
    /**
206
     * Set default term
207
     *
208
     * @param string $term
209
     *
210
     * @return Client
211
     */
212
    public function setDefaultTerm($term)
213
    {
214
        $this->defaultTerm = $term;
215
216
        return $this;
217
    }
218
219
    /**
220
     * Set search limit
221
     *
222
     * @param integer $limit
223
     *
224
     * @return Client
225
     */
226
    public function setSearchLimit($limit)
227
    {
228
        if (is_int($limit)) {
229
            $this->searchLimit = $limit;
230
        }
231
232
        return $this;
233
    }
234
}
235