Skip to content

Commit 4192830

Browse files
authored
Merge pull request #5 from KOBASSoftware/multi-token-fix
Resolves an issue where using the library for multiple company's woul…
2 parents 6971ca0 + b51a3d6 commit 4192830

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/Kobas/APIClient/Auth/Provider.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
class Provider
1717
{
1818
/**
19-
* @var AccessToken|null
19+
* @var AccessToken[]
2020
*/
21-
protected static $token;
21+
protected static $tokens;
2222
/**
2323
* @var int
2424
*/
@@ -89,17 +89,24 @@ public function getAccessToken()
8989
{
9090
$provider = $this->getProvider();
9191

92-
if (!self::$token instanceof AccessToken || self::$token->hasExpired()) {
92+
if (!is_array(self::$tokens) ||
93+
!array_key_exists($provider->companyId, self::$tokens) ||
94+
!self::$tokens[$provider->companyId] instanceof AccessToken ||
95+
self::$tokens[$provider->companyId]->hasExpired()
96+
) {
9397
try {
94-
self::$token = $provider->getAccessToken('client_credentials', ['scope' => $this->scopes]);
98+
self::$tokens[$provider->companyId] = $provider->getAccessToken(
99+
'client_credentials',
100+
['scope' => $this->scopes]
101+
);
95102
} catch (IdentityProviderException $e) {
96103
throw new AuthenticationException($e->getMessage(), $e->getCode());
97104
} catch (ConnectException $e) {
98105
throw new CurlException($e->getMessage(), $e->getCode());
99106
}
100107
}
101108

102-
return self::$token->getToken();
109+
return self::$tokens[$provider->companyId]->getToken();
103110
}
104111

105112
/**

src/Kobas/APIClient/Client.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,25 @@ public function setAPIVersion($version)
110110
* @param array $params
111111
* @param array $headers
112112
* @return mixed
113-
* @throws HttpException
114113
* @throws CurlException
114+
* @throws Exception\AuthenticationException
115+
* @throws HttpException
115116
*/
116117
public function get($route, array $params = array(), array $headers = array())
117118
{
118119
return $this->call('GET', $route, $params, $headers);
119120
}
120121

122+
121123
/**
122124
* @param $http_method
123125
* @param $route
124126
* @param array $params
125127
* @param array $headers
126128
* @return mixed
127-
* @throws HttpException
128129
* @throws CurlException
130+
* @throws Exception\AuthenticationException
131+
* @throws HttpException
129132
*/
130133
protected function call($http_method, $route, array $params = array(), array $headers = array())
131134
{
@@ -193,39 +196,45 @@ protected function call($http_method, $route, array $params = array(), array $he
193196
return json_decode($result, true);
194197
}
195198

199+
196200
/**
197201
* @param $route
198202
* @param array $params
199203
* @param array $headers
200204
* @return mixed
201-
* @throws HttpException
202205
* @throws CurlException
206+
* @throws Exception\AuthenticationException
207+
* @throws HttpException
203208
*/
204209
public function post($route, array $params = array(), array $headers = array())
205210
{
206211
return $this->call('POST', $route, $params, $headers);
207212
}
208213

214+
209215
/**
210216
* @param $route
211217
* @param array $params
212218
* @param array $headers
213219
* @return mixed
214-
* @throws HttpException
215220
* @throws CurlException
221+
* @throws Exception\AuthenticationException
222+
* @throws HttpException
216223
*/
217224
public function put($route, array $params = array(), array $headers = array())
218225
{
219226
return $this->call('PUT', $route, $params, $headers);
220227
}
221228

229+
222230
/**
223231
* @param $route
224232
* @param array $params
225233
* @param array $headers
226234
* @return mixed
227-
* @throws HttpException
228235
* @throws CurlException
236+
* @throws Exception\AuthenticationException
237+
* @throws HttpException
229238
*/
230239
public function delete($route, array $params = array(), array $headers = array())
231240
{

0 commit comments

Comments
 (0)