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

Commit b45e467

Browse files
committed
BridgeStack completed and Documentation started.
1 parent 178f658 commit b45e467

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,52 @@ require 'vendor/autoload.php'
2727

2828
use FindBrok\WatsonBridge\Bridge;
2929

30-
//Create a new bridge Object
30+
// Create a new bridge Object.
3131
$bridge = new Bridge('username', 'password', 'baseUrl');
3232

33-
//Simple get request
33+
// Simple get request.
3434
$queryParams = ['foo' => 'bar'];
3535
$response = $bridge->get('uri', $queryParams);
3636

37-
//Simple post request
37+
// Simple post request.
3838
$dataToPost = ['foo' => 'bar'];
3939
$response = $bridge->post('uri', $dataToPost, 'json');
4040
```
4141

4242
The Package uses [Guzzle](http://docs.guzzlephp.org/en/latest/testing.html) to perform requests,
4343
all your responses will be instances of ```GuzzleHttp\Psr7\Response```
4444

45+
---
46+
### Integration with Laravel 5
47+
48+
As of version 1.1.x, PHP Watson API bridge adds a new Service Provider which integrates easily with Laravel 5.
49+
50+
First add the ServiceProvider to your ```app.php``` file.
51+
52+
```php
53+
'providers' => [
54+
....
55+
FindBrok\WatsonBridge\WatsonBridgeServiceProvider::class,
56+
]
57+
```
58+
59+
Now publish the config file.
60+
61+
```php
62+
$ php artisan vendor:publish --tag=watson-api-bridge
63+
```
64+
65+
You will now have a config file ```watson-bridge.php``` in your config directory.
66+
You may define in this config file your credentials, auth method to use,
67+
Watson Services and so on.
68+
69+
### Services
70+
71+
The Laravel Integration gives you 3 service classes that are bound to the IoC.
72+
- Bridge (The actual Bridge class for making requests to Watson)
73+
- Carpenter (Which can construct Bridge instances using your credentials and service URL)
74+
- BridgeStack (Essentially a store where you can keep all Bridges you constructed and retrieve them back.)
75+
76+
## Bridge
77+
78+
Bridge class will help you make requests to Watson API using the ```get```, ```post```, ```put```, ```patch``` methods

src/Facades/Bridge.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace FindBrok\WatsonBridge\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use FindBrok\WatsonBridge\Bridge as Concrete;
7+
8+
class Bridge extends Facade
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
protected static function getFacadeAccessor()
14+
{
15+
return Concrete::class;
16+
}
17+
}

src/Facades/BridgeStack.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace FindBrok\WatsonBridge\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use FindBrok\WatsonBridge\Support\BridgeStack as Concrete;
7+
8+
class BridgeStack extends Facade
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
protected static function getFacadeAccessor()
14+
{
15+
return Concrete::class;
16+
}
17+
}

src/Facades/Carpenter.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace FindBrok\WatsonBridge\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use FindBrok\WatsonBridge\Support\Carpenter as Concrete;
7+
8+
class Carpenter extends Facade
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
protected static function getFacadeAccessor()
14+
{
15+
return Concrete::class;
16+
}
17+
}

src/Support/BridgeStack.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace FindBrok\WatsonBridge\Support;
44

5+
use FindBrok\WatsonBridge\Bridge;
56
use Illuminate\Support\Collection;
7+
use FindBrok\WatsonBridge\Exceptions\WatsonBridgeException;
68

79
class BridgeStack extends Collection
810
{
@@ -36,7 +38,33 @@ public function __construct(Carpenter $carpenter, $items = [])
3638
*
3739
* @return $this
3840
*/
39-
public function mountBridge($name, $credential = null, $service = null, $authMethod = null)
41+
public function mountBridge($name, $credential, $service = null, $authMethod = 'credentials')
4042
{
43+
// Creates the Bridge.
44+
$bridge = $this->carpenter->constructBridge($credential, $service, $authMethod);
45+
46+
// Save it under a name.
47+
$this->put($name, $bridge);
48+
49+
return $this;
50+
}
51+
52+
/**
53+
* Conjures a specific Bridge to use.
54+
*
55+
* @param string $name
56+
*
57+
* @throws WatsonBridgeException
58+
* @return Bridge
59+
*/
60+
public function conjure($name)
61+
{
62+
// We must check if the Bridge does
63+
// exists.
64+
if (! $this->has($name)) {
65+
throw new WatsonBridgeException('The Bridge with name "'.$name.'" does not exist.');
66+
}
67+
68+
return $this->get($name);
4169
}
4270
}

0 commit comments

Comments
 (0)