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

Commit 9c21130

Browse files
committed
New Carpenter class and Rework Service provider for support of laravel 5.4
1 parent 76135b7 commit 9c21130

File tree

7 files changed

+302
-67
lines changed

7 files changed

+302
-67
lines changed

src/Bridge.php

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,77 @@ class Bridge
7676
* @param string $password
7777
* @param string $endpoint
7878
*/
79-
public function __construct($username, $password, $endpoint)
79+
public function __construct($username = null, $password = null, $endpoint = null)
8080
{
8181
// Set Username, Password and Endpoint.
82-
$this->username = $username;
83-
$this->password = $password;
84-
$this->endpoint = $endpoint;
82+
$this->setUsername($username);
83+
$this->setPassword($password);
84+
$this->setEndPoint($endpoint);
8585

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

8989
// Set Token.
90-
$this->token = new Token($this->username);
90+
$this->setToken($username);
91+
}
92+
93+
/**
94+
* Sets the Username.
95+
*
96+
* @param string $username
97+
*
98+
* @return $this
99+
*/
100+
public function setUsername($username = null)
101+
{
102+
$this->username = $username;
103+
104+
return $this;
105+
}
106+
107+
/**
108+
* Sets the Password.
109+
*
110+
* @param string $password
111+
*
112+
* @return $this
113+
*/
114+
public function setPassword($password = null)
115+
{
116+
$this->password = $password;
117+
118+
return $this;
119+
}
120+
121+
/**
122+
* Sets the Endpoint.
123+
*
124+
* @param string $endpoint
125+
*
126+
* @return $this
127+
*/
128+
public function setEndPoint($endpoint = null)
129+
{
130+
$this->endpoint = $endpoint;
131+
132+
return $this;
133+
}
134+
135+
/**
136+
* Sets the Token.
137+
*
138+
* @param string $username
139+
*
140+
* @return $this
141+
*/
142+
public function setToken($username = null)
143+
{
144+
// Only set Token if Username is supplied.
145+
if (! is_null($username)) {
146+
$this->token = new Token($username);
147+
}
148+
149+
return $this;
91150
}
92151

93152
/**

src/Support/BridgeStack.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace FindBrok\WatsonBridge\Support;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class BridgeStack extends Collection
8+
{
9+
/**
10+
* The Carpenter instance.
11+
*
12+
* @var Carpenter
13+
*/
14+
protected $carpenter;
15+
16+
/**
17+
* BridgeStack constructor.
18+
*
19+
* @param Carpenter $carpenter
20+
* @param array $items
21+
*/
22+
public function __construct(Carpenter $carpenter, $items = [])
23+
{
24+
$this->carpenter = $carpenter;
25+
26+
parent::__construct($items);
27+
}
28+
29+
30+
public function mountBridge($name, $credential = null, $service = null, $authMethod = null)
31+
{
32+
}
33+
}

src/Support/Carpenter.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace FindBrok\WatsonBridge\Support;
4+
5+
use FindBrok\WatsonBridge\Bridge;
6+
use FindBrok\WatsonBridge\Exceptions\WatsonBridgeException;
7+
8+
class Carpenter
9+
{
10+
/**
11+
* Constructs a new Bridge.
12+
*
13+
* @param string $credential
14+
* @param string $service
15+
* @param string $authMethod
16+
*
17+
* @throws WatsonBridgeException
18+
* @return Bridge
19+
*/
20+
public function constructBridge($credential, $service = null, $authMethod = 'credentials')
21+
{
22+
// Get credentials array.
23+
$credentials = $this->getCredentials($credential);
24+
25+
// Make sure credentials information is available.
26+
if (! isset($credentials['username']) || ! isset($credentials['password']) || ! isset($credentials['gateway'])) {
27+
throw new WatsonBridgeException('Could not construct Bridge, missing some information in credentials.',
28+
500);
29+
}
30+
31+
// Make bridge.
32+
$bridge = $this->makeRawBridge()
33+
->setUsername($credentials['username'])
34+
->setPassword($credentials['password'])
35+
->setEndPoint($credentials['gateway'])
36+
->setClient($credentials['gateway'])
37+
->appendHeaders(['X-Watson-Learning-Opt-Out' => config('watson-bridge.x_watson_learning_opt_out')]);
38+
39+
// Add service.
40+
$bridge = $this->addServiceToBridge($bridge, $service);
41+
42+
// Choose an auth method.
43+
$bridge = $this->chooseAuthMethodForBridge($bridge, $authMethod);
44+
45+
return $bridge;
46+
}
47+
48+
/**
49+
* Creates and return a raw bridge.
50+
*
51+
* @return Bridge
52+
*/
53+
protected function makeRawBridge()
54+
{
55+
return new Bridge();
56+
}
57+
58+
/**
59+
* Adds a Service to the Bridge.
60+
*
61+
* @param Bridge $bridge
62+
* @param string $service
63+
*
64+
* @return Bridge
65+
*/
66+
protected function addServiceToBridge(Bridge $bridge, $service = null)
67+
{
68+
// Add a service if necessary.
69+
if (! is_null($service)) {
70+
$bridge->usingService($service);
71+
}
72+
73+
return $bridge;
74+
}
75+
76+
/**
77+
* Choose an Auth Method for the Bridge.
78+
*
79+
* @param Bridge $bridge
80+
* @param string $username
81+
* @param string $authMethod
82+
*
83+
* @return Bridge
84+
*/
85+
protected function chooseAuthMethodForBridge(Bridge $bridge, $username, $authMethod = null)
86+
{
87+
// Check if an auth method is passed explicitly.
88+
if (! is_null($authMethod) && collect(config('watson-bridge.auth_methods'))->contains($authMethod)) {
89+
$bridge->useAuthMethodAs($authMethod);
90+
91+
// Auth method is token so we need to set Token.
92+
if ($authMethod == 'token') {
93+
$bridge->setToken($username);
94+
}
95+
}
96+
97+
return $bridge;
98+
}
99+
100+
/**
101+
* Get credentials to use.
102+
*
103+
* @param string $name
104+
*
105+
* @throws WatsonBridgeException
106+
* @return array
107+
*/
108+
protected function getCredentials($name = 'default')
109+
{
110+
// We must make sure that credentials exists.
111+
if (! config()->has('watson-bridge.credentials.'.$name)) {
112+
throw new WatsonBridgeException('Credentials "'.$name.'" does not exist, try specifying it in the watson-bridge config.',
113+
500);
114+
}
115+
116+
return config('watson-bridge.credentials.'.$name);
117+
}
118+
}

src/Token.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public function exists()
6666
*/
6767
public function isExpired()
6868
{
69-
return $this->hasPayLoad() && ($this->payLoad['created'] + $this->payLoad['expires_in']) < Carbon::now()->format('U');
69+
return $this->hasPayLoad() && ($this->payLoad['created'] + $this->payLoad['expires_in']) < Carbon::now()
70+
->format('U');
7071
}
7172

7273
/**
@@ -96,11 +97,12 @@ public function isValid()
9697
*/
9798
public function save()
9899
{
99-
//No payload to save
100+
// No payload to save.
100101
if (! $this->hasPayLoad()) {
101102
return false;
102103
}
103-
//Save the token
104+
105+
// Save the token.
104106
return (bool) file_put_contents($this->getFilePath(), collect($this->payLoad)->toJson(), LOCK_EX);
105107
}
106108

@@ -121,12 +123,13 @@ public function getFilePath()
121123
*/
122124
public function loadPayLoadFromFile()
123125
{
124-
//Not found
126+
// Not found.
125127
if (! $this->exists()) {
126-
//We return empty array
128+
// We return empty array.
127129
return [];
128130
}
129-
//Load content from file
131+
132+
// Load content from file.
130133
return json_decode(file_get_contents($this->getFilePath()), true);
131134
}
132135

@@ -159,13 +162,14 @@ public function getToken()
159162
*/
160163
public function updateToken($token)
161164
{
162-
//Update Payload
165+
// Update Payload.
163166
$this->payLoad = [
164167
'token' => $token,
165168
'expires_in' => 3600,
166169
'created' => Carbon::now()->format('U'),
167170
];
168-
//Save token
171+
172+
// Save token.
169173
return $this->save();
170174
}
171175
}

src/WatsonBridgeServiceProvider.php

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace FindBrok\WatsonBridge;
44

55
use Illuminate\Support\ServiceProvider;
6-
use FindBrok\WatsonBridge\Exceptions\WatsonBridgeException;
6+
use Illuminate\Contracts\Config\Repository;
7+
use FindBrok\WatsonBridge\Support\Carpenter;
8+
use FindBrok\WatsonBridge\Support\BridgeStack;
9+
use Illuminate\Contracts\Foundation\Application;
710

811
class WatsonBridgeServiceProvider extends ServiceProvider
912
{
@@ -30,59 +33,33 @@ public function register()
3033
// Merge configs.
3134
$this->mergeConfigFrom(__DIR__.'/config/watson-bridge.php', 'watson-bridge');
3235

33-
// Bind Bridge in the IOC.
34-
$this->app->bind(Bridge::class, function ($app, array $args = ['use' => 'default']) {
35-
return $this->constructBridge($args);
36+
// The Carpenter must be an Instance because we need only one.
37+
$this->app->singleton(Carpenter::class, function () {
38+
return new Carpenter();
3639
});
37-
}
38-
39-
/**
40-
* Construct Watson Bridge.
41-
*
42-
* @param array $args
43-
*
44-
* @throws WatsonBridgeException
45-
* @return Bridge
46-
*/
47-
protected function constructBridge(array $args)
48-
{
49-
// A credential name is necessary.
50-
if (! isset($args['use'])) {
51-
$args['use'] = 'default';
52-
}
5340

54-
// Get credentials array.
55-
$credentials = $this->getCredentials($args['use']);
56-
57-
// Make sure credentials information is available.
58-
if (! isset($credentials['username']) || ! isset($credentials['password']) || ! isset($credentials['gateway'])) {
59-
throw new WatsonBridgeException('Could not construct Bridge, missing some information in credentials.',
60-
500);
61-
}
62-
63-
// Make bridge.
64-
$bridge = new Bridge($credentials['username'], $credentials['password'], $credentials['gateway']);
65-
$bridge->appendHeaders(['X-Watson-Learning-Opt-Out' => config('watson-bridge.x_watson_learning_opt_out')]);
41+
// The Bridge Stack also must be the same across the entire app.
42+
$this->app->singleton(BridgeStack::class, function (Application $app) {
43+
return new BridgeStack($app->make(Carpenter::class), []);
44+
});
6645

67-
return $bridge;
46+
// Registers a Default Bridge.
47+
$this->registerDefaultBridge();
6848
}
6949

7050
/**
71-
* Get credentials to use.
72-
*
73-
* @param string $name
74-
*
75-
* @throws WatsonBridgeException
76-
* @return array
51+
* Registers the Default Bridge.
7752
*/
78-
protected function getCredentials($name = 'default')
53+
protected function registerDefaultBridge()
7954
{
80-
// We must make sure that credentials exists.
81-
if (! config()->has('watson-bridge.credentials.'.$name)) {
82-
throw new WatsonBridgeException('Credentials "'.$name.'" does not exist, try specifying it in the watson-bridge config.',
83-
500);
84-
}
55+
$this->app->bind(Bridge::class, function (Application $app) {
56+
/** @var Carpenter $carpenter */
57+
$carpenter = $app->make(Carpenter::class);
58+
/** @var Repository $config */
59+
$config = $app->make(Repository::class);
8560

86-
return config('watson-bridge.credentials.'.$name);
61+
return $carpenter->constructBridge($config->get('watson-bridge.default_credentials'), null,
62+
$config->get('watson-bridge.default_auth_method'));
63+
});
8764
}
8865
}

0 commit comments

Comments
 (0)