Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 49c9421

Browse files
committed
Update Dependencise to start moving to optional Laravel interagtion
1 parent 87f9ff6 commit 49c9421

File tree

4 files changed

+66
-76
lines changed

4 files changed

+66
-76
lines changed

.styleci.yml

-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
11
preset: laravel
2-
3-
disabled:
4-
- concat_without_spaces
5-
6-
enabled:
7-
- concat_with_spaces

composer.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
"require": {
1313
"php": ">=5.5.0",
1414
"guzzlehttp/guzzle": "6.2.*",
15-
"illuminate/support": "5.0 - 5.3",
16-
"nesbot/carbon": "^1.21"
15+
"illuminate/support": "~5.0",
16+
"nesbot/carbon": "~1.20"
1717
},
1818
"require-dev": {
19-
"fzaninotto/faker": "~1.4",
20-
"phpunit/phpunit": "~4.0",
21-
"mockery/mockery": "0.9.*",
19+
"orchestra/testbench": "~3.0",
20+
"phpunit/phpunit": "~5.5",
2221
"symfony/var-dumper": "^3.1"
2322
},
2423
"autoload": {
@@ -28,7 +27,7 @@
2827
},
2928
"extra": {
3029
"branch-alias": {
31-
"dev-master": "1.0.x-dev"
30+
"dev-master": "1.1.x-dev"
3231
}
3332
},
3433
"minimum-stability": "dev",

src/Bridge.php

+61-61
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ class Bridge
7878
*/
7979
public function __construct($username, $password, $endpoint)
8080
{
81-
//Set Username, Password and Endpoint
81+
// Set Username, Password and Endpoint.
8282
$this->username = $username;
8383
$this->password = $password;
8484
$this->endpoint = $endpoint;
8585

86-
//Set HttpClient
86+
// Set HttpClient.
8787
$this->setClient($endpoint);
8888

89-
//Set Token
89+
// Set Token.
9090
$this->token = new Token($this->username);
9191
}
9292

@@ -99,12 +99,13 @@ public function __construct($username, $password, $endpoint)
9999
*/
100100
public function appendHeaders($headers = [])
101101
{
102-
//We have some headers to append
102+
// We have some headers to append.
103103
if (! empty($headers)) {
104-
//Append headers
104+
// Append headers.
105105
$this->headers = collect($this->headers)->merge($headers)->all();
106106
}
107-
//Return calling object
107+
108+
// Return calling object.
108109
return $this;
109110
}
110111

@@ -117,7 +118,7 @@ public function appendHeaders($headers = [])
117118
*/
118119
public function cleanOptions($options = [])
119120
{
120-
//If item is null or empty we will remove them
121+
// If item is null or empty we will remove them.
121122
return collect($options)->reject(function ($item) {
122123
return empty($item) || is_null($item);
123124
})->all();
@@ -144,7 +145,7 @@ public function clearThrottle()
144145
*/
145146
public function delete($uri, $data, $type = 'json')
146147
{
147-
//Make a Delete and return response
148+
// Make a Delete and return response.
148149
return $this->send('DELETE', $uri, $data, $type);
149150
}
150151

@@ -157,13 +158,11 @@ public function delete($uri, $data, $type = 'json')
157158
*/
158159
public function failedRequest(Response $response)
159160
{
160-
//Decode Response
161+
// Decode Response.
161162
$decodedResponse = json_decode($response->getBody()->getContents(), true);
162-
//Get error message
163-
$errorMessage = (isset($decodedResponse['error_message']) && ! is_null($decodedResponse['error_message'])) ?
164-
$decodedResponse['error_message'] :
165-
$response->getReasonPhrase();
166-
//ClientException
163+
// Get error message.
164+
$errorMessage = (isset($decodedResponse['error_message']) && ! is_null($decodedResponse['error_message'])) ? $decodedResponse['error_message'] : $response->getReasonPhrase();
165+
// ClientException.
167166
throw new WatsonBridgeException($errorMessage, $response->getStatusCode());
168167
}
169168

@@ -176,21 +175,21 @@ public function failedRequest(Response $response)
176175
*/
177176
public function fetchToken($incrementThrottle = false)
178177
{
179-
//Increment throttle if needed
178+
// Increment throttle if needed.
180179
if ($incrementThrottle) {
181180
$this->incrementThrottle();
182181
}
183-
//Reset Client
182+
// Reset Client.
184183
$this->setClient($this->getAuthorizationEndpoint());
185-
//Get the token response
184+
// Get the token response.
186185
$response = $this->get('v1/token', [
187186
'url' => $this->endpoint,
188187
]);
189-
//Extract
188+
// Extract.
190189
$token = json_decode($response->getBody()->getContents(), true);
191-
//Reset client
190+
// Reset client.
192191
$this->setClient($this->endpoint);
193-
//Update token
192+
// Update token.
194193
$this->token->updateToken($token['token']);
195194
}
196195

@@ -204,7 +203,7 @@ public function fetchToken($incrementThrottle = false)
204203
*/
205204
public function get($uri = '', $query = [])
206205
{
207-
//Make a Post and return response
206+
// Make a Post and return response.
208207
return $this->send('GET', $uri, $query, 'query');
209208
}
210209

@@ -215,7 +214,7 @@ public function get($uri = '', $query = [])
215214
*/
216215
public function getAuth()
217216
{
218-
//Return access authorization
217+
// Return access authorization.
219218
return [$this->username, $this->password];
220219
}
221220

@@ -226,10 +225,11 @@ public function getAuth()
226225
*/
227226
public function getAuthorizationEndpoint()
228227
{
229-
//Parse the endpoint
228+
// Parse the endpoint.
230229
$parsedEndpoint = collect(parse_url($this->endpoint));
231-
//Return auth url
232-
return $parsedEndpoint->get('scheme') . '://' . $parsedEndpoint->get('host') . '/authorization/api/';
230+
231+
// Return auth url.
232+
return $parsedEndpoint->get('scheme').'://'.$parsedEndpoint->get('host').'/authorization/api/';
233233
}
234234

235235
/**
@@ -239,7 +239,7 @@ public function getAuthorizationEndpoint()
239239
*/
240240
public function getClient()
241241
{
242-
//Return client
242+
// Return client.
243243
return $this->client;
244244
}
245245

@@ -250,7 +250,7 @@ public function getClient()
250250
*/
251251
public function getHeaders()
252252
{
253-
//Return headers
253+
// Return headers.
254254
return $this->headers;
255255
}
256256

@@ -263,23 +263,20 @@ public function getHeaders()
263263
*/
264264
public function getRequestOptions($initial = [])
265265
{
266-
//Define options
266+
// Define options.
267267
$options = collect($initial);
268-
//Define an auth option
268+
269+
// Define an auth option.
269270
if ($this->authMethod == 'credentials') {
270-
$options = $options->merge([
271-
'auth' => $this->getAuth(),
272-
]);
271+
$options = $options->merge(['auth' => $this->getAuth()]);
273272
} elseif ($this->authMethod == 'token') {
274-
$this->appendHeaders([
275-
'X-Watson-Authorization-Token' => $this->getToken(),
276-
]);
273+
$this->appendHeaders(['X-Watson-Authorization-Token' => $this->getToken()]);
277274
}
278-
//Put Headers in options
279-
$options = $options->merge([
280-
'headers' => $this->getHeaders(),
281-
]);
282-
//Clean and return
275+
276+
// Put Headers in options.
277+
$options = $options->merge(['headers' => $this->getHeaders(),]);
278+
279+
// Clean and return.
283280
return $this->cleanOptions($options->all());
284281
}
285282

@@ -290,13 +287,13 @@ public function getRequestOptions($initial = [])
290287
*/
291288
public function getToken()
292289
{
293-
//Token is not valid
290+
// Token is not valid.
294291
if (! $this->token->isValid()) {
295-
//Fetch from Watson
292+
// Fetch from Watson.
296293
$this->fetchToken();
297294
}
298295

299-
//Return token
296+
// Return token.
300297
return $this->token->getToken();
301298
}
302299

@@ -331,7 +328,7 @@ public function isThrottledReached()
331328
*/
332329
public function patch($uri, $data, $type = 'json')
333330
{
334-
//Make a Patch and return response
331+
// Make a Patch and return response.
335332
return $this->send('PATCH', $uri, $data, $type);
336333
}
337334

@@ -346,7 +343,7 @@ public function patch($uri, $data, $type = 'json')
346343
*/
347344
public function post($uri, $data, $type = 'json')
348345
{
349-
//Make a Post and return response
346+
// Make a Post and return response.
350347
return $this->send('POST', $uri, $data, $type);
351348
}
352349

@@ -361,7 +358,7 @@ public function post($uri, $data, $type = 'json')
361358
*/
362359
public function put($uri, $data, $type = 'json')
363360
{
364-
//Make a Put and return response
361+
// Make a Put and return response.
365362
return $this->send('PUT', $uri, $data, $type);
366363
}
367364

@@ -377,19 +374,20 @@ public function put($uri, $data, $type = 'json')
377374
public function request($method = 'GET', $uri = '', $options = [])
378375
{
379376
try {
380-
//Make the request
377+
// Make the request.
381378
return $this->getClient()->request($method, $uri, $this->getRequestOptions($options));
382379
} catch (ClientException $e) {
383-
//We are using token auth and probably token expired
380+
// We are using token auth and probably token expired.
384381
if ($this->authMethod == 'token' && $e->getCode() == 401 && ! $this->isThrottledReached()) {
385-
//Try refresh token
382+
// Try refresh token.
386383
$this->fetchToken(true);
387-
//Try requesting again
384+
385+
// Try requesting again.
388386
return $this->request($method, $uri, $options);
389387
}
390-
//Clear throttle for this request
388+
// Clear throttle for this request.
391389
$this->clearThrottle();
392-
//Call Failed Request
390+
// Call Failed Request.
393391
$this->failedRequest($e->getResponse());
394392
}
395393
}
@@ -406,14 +404,15 @@ public function request($method = 'GET', $uri = '', $options = [])
406404
*/
407405
private function send($method, $uri, $data, $type = 'json')
408406
{
409-
//Make the Request to Watson
407+
// Make the Request to Watson.
410408
$response = $this->request($method, $uri, [$type => $data]);
411-
//Request Failed
409+
// Request Failed.
412410
if ($response->getStatusCode() != 200) {
413-
//Throw Watson Bridge Exception
411+
// Throw Watson Bridge Exception.
414412
$this->failedRequest($response);
415413
}
416-
//We return response
414+
415+
// We return response.
417416
return $response;
418417
}
419418

@@ -426,10 +425,10 @@ private function send($method, $uri, $data, $type = 'json')
426425
*/
427426
public function setClient($endpoint = null)
428427
{
429-
//Create client using API endpoint
428+
// Create client using API endpoint.
430429
$this->client = new Client([
431-
'base_uri' => ! is_null($endpoint) ? $endpoint : $this->endpoint,
432-
]);
430+
'base_uri' => ! is_null($endpoint) ? $endpoint : $this->endpoint
431+
]);
433432
}
434433

435434
/**
@@ -441,9 +440,10 @@ public function setClient($endpoint = null)
441440
*/
442441
public function useAuthMethodAs($method = 'credentials')
443442
{
444-
//Change auth method
443+
// Change auth method.
445444
$this->authMethod = $method;
446-
//Return object
445+
446+
// Return object.
447447
return $this;
448448
}
449449
}

src/Token.php

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
use Carbon\Carbon;
66

7-
/**
8-
* Class Token.
9-
*/
107
class Token
118
{
129
/**

0 commit comments

Comments
 (0)