1 | <?php |
||
23 | class TwitterClient |
||
24 | { |
||
25 | /** |
||
26 | * HTTP Client capable of making HTTP Requests |
||
27 | * |
||
28 | * @var \GuzzleHttp\Client $client |
||
29 | */ |
||
30 | private $client; |
||
31 | |||
32 | /** |
||
33 | * Header object that is used to generate the OAuth 1.0 header |
||
34 | * |
||
35 | * @var \Snaggle\Client\Header\Header $header |
||
36 | */ |
||
37 | private $header; |
||
38 | |||
39 | /** |
||
40 | * A signature type used to generate the OAuth 1.0 signature |
||
41 | * |
||
42 | * @var \Snaggle\Client\Signatures\SignatureInterface $signature |
||
43 | */ |
||
44 | private $signature; |
||
45 | |||
46 | /** |
||
47 | * A Snaggle\AccessCredentials instance with the appropriate key/secret |
||
48 | * |
||
49 | * @var \Snaggle\Client\Credentials\AccessCredentials |
||
50 | */ |
||
51 | private $accessCredentials; |
||
52 | |||
53 | /** |
||
54 | * A Snaggle\ConsumerCredentials instance with the appropriate key/secret |
||
55 | * |
||
56 | * @var \Snaggle\Client\Credentials\ConsumerCredentials |
||
57 | */ |
||
58 | private $consumerCredentials; |
||
59 | |||
60 | /** |
||
61 | * String representing the location of the resource |
||
62 | * |
||
63 | * @var string $resourceUrl |
||
64 | */ |
||
65 | private $resourceUrl; |
||
66 | |||
67 | /** |
||
68 | * String representing the HTTP method with which to use the request |
||
69 | * |
||
70 | * @var string $httpMethod |
||
71 | */ |
||
72 | private $httpMethod; |
||
73 | |||
74 | /** |
||
75 | * A timestamp for the request |
||
76 | * |
||
77 | * @var int $timestamp |
||
78 | */ |
||
79 | private $timestamp = 0; |
||
80 | |||
81 | /** |
||
82 | * A nonce for the request |
||
83 | * |
||
84 | * @var string $nonce |
||
85 | */ |
||
86 | private $nonce = null; |
||
87 | |||
88 | /** |
||
89 | * Verifier that is part of the temporary token exchange |
||
90 | * |
||
91 | * @var string $verifier |
||
92 | */ |
||
93 | private $verifier = null; |
||
94 | |||
95 | /** |
||
96 | * Post requests require any form fields to be included for the signature, you can set them here |
||
97 | * |
||
98 | * @var Array $postFields |
||
99 | */ |
||
100 | private $postFields = []; |
||
101 | |||
102 | /** |
||
103 | * Base Twitter API Endpoint |
||
104 | * |
||
105 | * @var const string $baseEndpoint |
||
106 | */ |
||
107 | const TWITTER_BASE_ENDPOINT = 'https://api.twitter.com/1.1/'; |
||
108 | |||
109 | /** |
||
110 | * @param AccessCredentials $accessCredentials |
||
111 | * @param ConsumerCredentials $consumerCredentials |
||
112 | */ |
||
113 | public function __construct(AccessCredentials $accessCredentials, ConsumerCredentials $consumerCredentials) |
||
119 | |||
120 | /** |
||
121 | * Method set an instance of Guzzle HTTP client |
||
122 | * |
||
123 | * @param Client $client |
||
124 | */ |
||
125 | public function setClient(Client $client) |
||
129 | |||
130 | /** |
||
131 | * Accessor method to retrieve a set header or create a new instance of header |
||
132 | * |
||
133 | * @return Header |
||
134 | */ |
||
135 | public function getHeader() |
||
142 | |||
143 | /** |
||
144 | * Access method to set an instance of header |
||
145 | * |
||
146 | * @param Header $header |
||
147 | */ |
||
148 | public function setHeader(Header $header) |
||
152 | |||
153 | /** |
||
154 | * Accessor method to retrieve a set Signature or create a new instance |
||
155 | * |
||
156 | */ |
||
157 | public function getSignature() |
||
164 | |||
165 | /** |
||
166 | * Accessor method for setting a preconfigured signature which will set the other |
||
167 | * properties from the data contained in the signature |
||
168 | * |
||
169 | * @param HmacSha1 $signature |
||
170 | */ |
||
171 | public function setSignature(HmacSha1 $signature) |
||
181 | |||
182 | /** |
||
183 | * Method to set the resource url |
||
184 | * |
||
185 | * @param string $url |
||
186 | */ |
||
187 | public function setResourceUrl($url) |
||
191 | |||
192 | /** |
||
193 | * Method to set the Http Method |
||
194 | * |
||
195 | * @param string $httpMethod |
||
196 | */ |
||
197 | public function setHttpMethod($httpMethod) |
||
201 | |||
202 | /** |
||
203 | * Method to set a timestamp |
||
204 | * |
||
205 | * @param int $timestamp |
||
206 | */ |
||
207 | public function setTimestamp($timestamp) |
||
211 | |||
212 | /** |
||
213 | * Method to set a nonce |
||
214 | * |
||
215 | * @param string $nonce |
||
216 | */ |
||
217 | public function setNonce($nonce) |
||
221 | |||
222 | /** |
||
223 | * Method to set the verifier for token requests |
||
224 | * |
||
225 | * @param string $verifier |
||
226 | */ |
||
227 | public function setVerifier($verifier) |
||
231 | |||
232 | /** |
||
233 | * Method for setting the post fields |
||
234 | * |
||
235 | * @param Array $postFields |
||
236 | */ |
||
237 | public function setPostFields(Array $postFields) |
||
244 | |||
245 | /** |
||
246 | * Method to build the Authorization Header |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getAuthorizationHeader() |
||
257 | |||
258 | /** |
||
259 | * Prepare the signature for use in the Authorization header |
||
260 | * |
||
261 | * @return Signature |
||
262 | */ |
||
263 | private function prepareSignature() |
||
284 | |||
285 | /** |
||
286 | * Method to execute a GET request |
||
287 | * |
||
288 | * @param string $endpoint - endpoint to hit |
||
289 | * @param Array $options parameters for the query string |
||
290 | * @return string response from the Twitter endpoint |
||
291 | * @throws UnauthorizedRequestException |
||
292 | * @throws RateLimitExceededException |
||
293 | * @throws RuntimeException |
||
294 | */ |
||
295 | public function makeGetRequest($endpoint, $options) |
||
315 | |||
316 | /** |
||
317 | * Method to execute a POST request |
||
318 | * |
||
319 | * @param string $endpoint - end point to hit |
||
320 | * @param Array $options - parameters/post body |
||
321 | * @return string response from Twitter Endpoint |
||
322 | * @throws UnauthorizedRequestException |
||
323 | * @throws RateLimitExceededException |
||
324 | * @throws RuntimeException |
||
325 | */ |
||
326 | public function makePostRequest($endpoint, $options) |
||
345 | |||
346 | /** |
||
347 | * Method to handle the selection of the correct exception to throw |
||
348 | * |
||
349 | * @param \GuzzleHttp\Exception\ClientException $e - Guzzle Client Exception |
||
350 | * @return mixed - Exception to throw |
||
351 | */ |
||
352 | protected function handleException(ClientException $e) |
||
368 | |||
369 | /** |
||
370 | * Method to prepare parameters for the base string by rawurlencoding them |
||
371 | * |
||
372 | * @param Array $options parameters or post body |
||
373 | * @return Array array of options rawurlencoded |
||
374 | */ |
||
375 | protected function preparePostOptions(Array $options) |
||
382 | |||
383 | /** |
||
384 | * Wrapper method for makeGetRequest |
||
385 | * |
||
386 | * @param string $endpoint end point to hit |
||
387 | * @param Array $options parameters |
||
388 | * @return string response from Twitter Endpoint |
||
389 | */ |
||
390 | public function get($endpoint, $options = []) |
||
394 | |||
395 | /** |
||
396 | * Wrapper method for makePostRequest |
||
397 | * |
||
398 | * @param string $endpoint endpoint to hit |
||
399 | * @param Array $options parameters/post body |
||
400 | * @return string response from endpoint |
||
401 | */ |
||
402 | public function post($endpoint, $options = []) |
||
406 | } |
||
407 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.