Completed
Push — v3-fusion-support ( e0700e...c04d70 )
by Steven
03:26
created

Client::createHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

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