Completed
Pull Request — master (#20)
by Steven
12:12
created

Client::setSearchLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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