Skip to content

Commit cf5cadc

Browse files
committed
ref gnello#50 decouple from pimple
1 parent 9273649 commit cf5cadc

File tree

4 files changed

+53
-104
lines changed

4 files changed

+53
-104
lines changed

README.md

+27-58
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Latest Stable Version][7]][8] [![Scrutinizer Code Quality][5]][6] [![Total Downloads][11]][12]
44

5-
The PHP Driver to interact with the [Mattermost Web Service API][4].
5+
The PHP Driver to interact with the [Mattermost Web Service API][4].
66

77
Please read [the api documentation][1] for further information on using this application.
88

@@ -31,64 +31,33 @@ composer require gnello/php-mattermost-driver:1.*
3131

3232
#### Login id and password
3333
```php
34-
use \Gnello\Mattermost\Driver;
35-
36-
$container = new \Pimple\Container([
37-
'driver' => [
38-
'url' => 'your_chat_url',
39-
'login_id' => 'your_login_id',
40-
'password' => 'your_password',
41-
]
42-
]);
43-
44-
$driver = new Driver($container);
45-
$result = $driver->authenticate();
46-
```
34+
use \Gnello\Mattermost\Driver;
35+
36+
// construct your own GuzzleHttp client instance
37+
$guzzle = new GuzzleHttp/Client(...);
38+
39+
$driver = new Driver($guzzle, [
40+
'url' => 'your_chat_url',
41+
'login_id' => 'your_login_id',
42+
'password' => 'your_password',
43+
]);
44+
$result = $driver->authenticate();
45+
```
4746

4847
#### Token
4948
```php
5049
use \Gnello\Mattermost\Driver;
51-
52-
$container = new \Pimple\Container([
53-
'driver' => [
54-
'url' => 'your_chat_url',
55-
'token' => 'your_token',
56-
]
57-
]);
58-
59-
$driver = new Driver($container);
60-
$result = $driver->authenticate();
61-
```
62-
63-
### Options
64-
Below a list of all the Driver available options, for the Guzzle options
65-
please refer to its [official documentation][13].
66-
67-
| Option | Default value | Description |
68-
|:---------|:--------------|:-------------------------------------------------------------------------------------------|
69-
| scheme | "https" | The URI scheme. |
70-
| basePath | "/api/v4" | The base path of the API endpoint. |
71-
| url | "localhost" | The URL of the Mattermost server, without the scheme (es. "www.mydomain.com"). |
72-
| login_id | null | The account username to use with the API. |
73-
| password | null | The account password to use with the API. |
74-
| token | null | The account token to use with the API, if specified it override the login_id and password. |
75-
76-
You can specify the options as shown in the following example:
77-
```php
78-
use \Gnello\Mattermost\Driver;
79-
80-
$container = new \Pimple\Container([
81-
'driver' => [
82-
//put here any options for the driver
83-
],
84-
'guzzle' => [
85-
//put here any options for Guzzle
86-
]
87-
]);
88-
89-
$driver = new Driver($container);
90-
$result = $driver->authenticate();
91-
```
50+
51+
// construct your own GuzzleHttp client instance
52+
$guzzle = new GuzzleHttp/Client(...);
53+
54+
$driver = new Driver($guzzle, [
55+
'url' => 'your_chat_url',
56+
'token' => 'your_token',
57+
]);
58+
59+
$result = $driver->authenticate();
60+
```
9261

9362
### Check results
9463
This Driver follows the [PSR-7][2] document therefore any response is a ResponseInterface type:
@@ -106,8 +75,8 @@ if ($result->getStatusCode() == 200) {
10675
```php
10776
//Add a new user
10877
$result = $driver->getUserModel()->createUser([
109-
'email' => '[email protected]',
110-
'username' => 'test',
78+
'email' => '[email protected]',
79+
'username' => 'test',
11180
'password' => 'testpsw'
11281
]);
11382

@@ -180,7 +149,7 @@ $result = $driver->getPreferenceModel('user_id')->getUserPreference();
180149
//Please read the PreferenceModel class or refer to the api documentation for a complete list of available methods.
181150
```
182151

183-
## Endpoints supported
152+
## Endpoints supported
184153

185154
- [Bleve](https://api.mattermost.com/#tag/bleve)
186155
- [Bots](https://api.mattermost.com/#tag/bots)

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"php": ">=5.5.0",
1919
"ext-json": "*",
2020
"psr/http-message" : "^1.0",
21-
"pimple/pimple" : "~3.0",
2221
"guzzlehttp/guzzle": "^6.2|^7.4"
2322
},
2423
"require-dev": {

src/Client.php

+3-14
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use GuzzleHttp\Psr7\Response;
2121
use GuzzleHttp\RequestOptions;
2222
use Psr\Http\Message\ResponseInterface;
23-
use Pimple\Container;
2423

2524
/**
2625
* Class Client
@@ -44,20 +43,10 @@ class Client
4443
*/
4544
private $client;
4645

47-
/**
48-
* Client constructor.
49-
*
50-
* @param Container $container
51-
*/
52-
public function __construct(Container $container)
46+
/** @param array<string, ?string> $options */
47+
public function __construct(GuzzleClient $guzzle, array $options)
5348
{
54-
$guzzleOptions = [];
55-
if (isset($container['guzzle'])) {
56-
$guzzleOptions = $container['guzzle'];
57-
}
58-
$this->client = new GuzzleClient($guzzleOptions);
59-
60-
$options = $container['driver'];
49+
$this->client = $guzzle;
6150
$this->baseUri = $options['scheme'] . '://' . $options['url'] . $options['basePath'];
6251
}
6352

src/Driver.php

+23-31
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
use Gnello\Mattermost\Models\ThreadModel;
3939
use Gnello\Mattermost\Models\UserModel;
4040
use Gnello\Mattermost\Models\WebhookModel;
41+
use GuzzleHttp\Client as Guzzle;
4142
use GuzzleHttp\Psr7\Response;
42-
use Pimple\Container;
4343
use Psr\Http\Message\ResponseInterface;
4444

4545
/**
@@ -54,7 +54,7 @@ class Driver
5454
*
5555
* @var array
5656
*/
57-
private $defaultOptions = [
57+
private const DEFAULT_OPTIONS = [
5858
'scheme' => 'https',
5959
'basePath' => '/api/v4',
6060
'url' => 'localhost',
@@ -63,57 +63,49 @@ class Driver
6363
'token' => null,
6464
];
6565

66-
/**
67-
* @var Container
68-
*/
69-
private $container;
66+
/** @var array<string, ?string> */
67+
private $driverOptions;
68+
69+
/** @var Client */
70+
private $client;
7071

7172
/**
7273
* @var array
7374
*/
7475
private $models = [];
7576

76-
/**
77-
* Driver constructor.
78-
*
79-
* @param Container $container
80-
*/
81-
public function __construct(Container $container)
77+
public function __construct(
78+
Guzzle $guzzle,
79+
array $driverOptions
80+
)
8281
{
83-
$driverOptions = $this->defaultOptions;
84-
85-
if (isset($container['driver'])) {
86-
$driverOptions = array_merge($driverOptions, $container['driver']);
87-
}
88-
89-
$container['driver'] = $driverOptions;
90-
$container['client'] = new Client($container);
91-
92-
$this->container = $container;
82+
$this->driverOptions = array_merge(self::DEFAULT_OPTIONS, $driverOptions);
83+
$this->client = new Client(
84+
$guzzle,
85+
$driverOptions
86+
);
9387
}
9488

9589
/**
9690
* @return ResponseInterface
9791
*/
9892
public function authenticate()
9993
{
100-
$driverOptions = $this->container['driver'];
101-
102-
if (isset($driverOptions['token'])) {
94+
if (isset($this->driverOptions['token'])) {
10395

104-
$this->container['client']->setToken($driverOptions['token']);
96+
$this->client->setToken($this->driverOptions['token']);
10597
$response = $this->getUserModel()->getAuthenticatedUser();
10698

107-
} else if (isset($driverOptions['login_id']) && isset($driverOptions['password'])) {
99+
} else if (isset($this->driverOptions['login_id']) && isset($this->driverOptions['password'])) {
108100

109101
$response = $this->getUserModel()->loginToUserAccount([
110-
'login_id' => $driverOptions['login_id'],
111-
'password' => $driverOptions['password']
102+
'login_id' => $this->driverOptions['login_id'],
103+
'password' => $this->driverOptions['password']
112104
]);
113105

114106
if ($response->getStatusCode() == 200) {
115107
$token = $response->getHeader('Token')[0];
116-
$this->container['client']->setToken($token);
108+
$this->client->setToken($token);
117109
}
118110

119111
} else {
@@ -138,7 +130,7 @@ public function authenticate()
138130
private function getModel($className)
139131
{
140132
if (!isset($this->models[$className])) {
141-
$this->models[$className] = new $className($this->container['client']);
133+
$this->models[$className] = new $className($this->client);
142134
}
143135

144136
return $this->models[$className];

0 commit comments

Comments
 (0)