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

Commit d3616ff

Browse files
author
Mateusz Gostański
committed
Refactored GetTokenAction
Renamed GetTokenAction to GetToken and added method reset() containing logic from ResetTokenAction + updated test
1 parent 81c0772 commit d3616ff

File tree

3 files changed

+89
-47
lines changed

3 files changed

+89
-47
lines changed

src/GetToken.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Grixu\ApiClient;
4+
5+
use Grixu\ApiClient\Exceptions\TokenIssueException;
6+
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Support\Facades\Http;
8+
9+
class GetToken
10+
{
11+
protected string $oAuthUrl;
12+
protected string $clientKey;
13+
protected string $clientId;
14+
protected string $cacheKey;
15+
16+
public function __construct(string $oAuthUrl, string $clientId, string $clientKey, string $cacheKey)
17+
{
18+
$this->oAuthUrl = $oAuthUrl;
19+
$this->clientId = $clientId;
20+
$this->clientKey = $clientKey;
21+
$this->cacheKey = $cacheKey;
22+
}
23+
24+
public function get(): ?string
25+
{
26+
$token = Cache::get($this->cacheKey);
27+
28+
if (!$token) {
29+
$tokenRequest = Http::withHeaders(
30+
[
31+
'Accept' => 'application/json'
32+
]
33+
)
34+
->post(
35+
$this->oAuthUrl,
36+
[
37+
'grant_type' => 'client_credentials',
38+
'client_id' => $this->clientId,
39+
'client_secret' => $this->clientKey,
40+
'scope' => '*',
41+
]
42+
);
43+
44+
if (!$tokenRequest->successful()) {
45+
throw new TokenIssueException($tokenRequest->body());
46+
}
47+
48+
$token = $tokenRequest->json('access_token');
49+
Cache::put($this->cacheKey, $token);
50+
}
51+
52+
return $token;
53+
}
54+
55+
public function reset(): ?string
56+
{
57+
Cache::forget($this->cacheKey);
58+
return $this->get();
59+
}
60+
}

src/GetTokenAction.php

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
namespace Grixu\ApiClient\Tests;
44

5-
use Grixu\ApiClient\GetTokenAction;
5+
use Grixu\ApiClient\GetToken;
66
use Grixu\ApiClient\Exceptions\TokenIssueException;
77
use Grixu\ApiClient\ApiClientServiceProvider;
88
use Illuminate\Support\Facades\Cache;
99
use Illuminate\Support\Facades\Http;
1010
use Orchestra\Testbench\TestCase;
1111

12-
class GetTokenActionTest extends TestCase
12+
class GetTokenTest extends TestCase
1313
{
14-
private GetTokenAction $action;
14+
private GetToken $action;
1515

1616
protected function setUp(): void
1717
{
1818
parent::setUp();
1919

20-
$this->action = new GetTokenAction();
20+
$this->action = new GetToken('t', 't', 't', 't');
2121
}
2222

2323
protected function getPackageProviders($app)
@@ -41,7 +41,7 @@ public function normal_pass()
4141
]
4242
);
4343

44-
$result = $this->action->execute();
44+
$result = $this->action->get();
4545

4646
$this->assertNotEmpty($result);
4747
$this->assertIsString($result);
@@ -58,9 +58,32 @@ public function with_http_error()
5858
);
5959

6060
try {
61-
$this->action->execute();
61+
$this->action->get();
6262
} catch (TokenIssueException $e) {
6363
$this->assertTrue(true);
6464
}
6565
}
66+
67+
/** @test */
68+
public function reset_token(): void
69+
{
70+
Cache::shouldReceive('forget')->once()->andReturnNull();
71+
Cache::shouldReceive('get')->once()->andReturnNull();
72+
Cache::shouldReceive('put')->once()->andReturnNull();
73+
Http::fake(
74+
[
75+
'*' => Http::response(
76+
[
77+
'access_token' => 'blebleble'
78+
],
79+
200
80+
)
81+
]
82+
);
83+
84+
$result = $this->action->reset();
85+
86+
$this->assertNotEmpty($result);
87+
$this->assertIsString($result);
88+
}
6689
}

0 commit comments

Comments
 (0)