Authenticator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 84
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createAccessToken() 0 21 2
A refreshAccessToken() 0 21 2
A getAccessToken() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FAPI\Sylius\Http;
6
7
use FAPI\Sylius\RequestBuilder;
8
use Http\Client\HttpClient;
9
10
/**
11
 * Class that gets access tokens.
12
 *
13
 * @author Tobias Nyholm <[email protected]>
14
 *
15
 * @internal
16
 */
17
final class Authenticator
18
{
19
    /**
20
     * @var RequestBuilder
21
     */
22
    private $requestBuilder;
23
24
    /**
25
     * @var HttpClient
26
     */
27
    private $httpClient;
28
29
    /**
30
     * @var string|null
31
     */
32
    private $accessToken;
33
34
    /**
35
     * @var string
36
     */
37
    private $clientId;
38
39
    /**
40
     * @var string
41
     */
42
    private $clientSecret;
43
44
    public function __construct(RequestBuilder $requestBuilder, HttpClient $httpClient, string $clientId, string $clientSecret)
45
    {
46
        $this->requestBuilder = $requestBuilder;
47
        $this->httpClient = $httpClient;
48
        $this->clientId = $clientId;
49
        $this->clientSecret = $clientSecret;
50
    }
51
52
    public function createAccessToken(string $username, string $password): ?string
53
    {
54
        $request = $this->requestBuilder->create('POST', '/api/oauth/v2/token', [
55
            'Content-type' => 'application/x-www-form-urlencoded',
56
        ], \http_build_query([
57
            'client_id' => $this->clientId,
58
            'client_secret' => $this->clientSecret,
59
            'grant_type' => 'password',
60
            'username' => $username,
61
            'password' => $password,
62
        ]));
63
64
        $response = $this->httpClient->sendRequest($request);
65
        if (200 !== $response->getStatusCode()) {
66
            return null;
67
        }
68
69
        $this->accessToken = $response->getBody()->__toString();
70
71
        return $this->accessToken;
72
    }
73
74
    public function refreshAccessToken(string $accessToken, string $refreshToken): ?string
75
    {
76
        $request = $this->requestBuilder->create('POST', '/api/oauth/v2/token', [
77
            'Authorization' => \sprintf('Bearer %s', $accessToken),
78
            'Content-type' => 'application/x-www-form-urlencoded',
79
        ], \http_build_query([
80
            'refresh_token' => $refreshToken,
81
            'grant_type' => 'refresh_token',
82
            'client_id' => $this->clientId,
83
            'client_secret' => $this->clientSecret,
84
        ]));
85
86
        $response = $this->httpClient->sendRequest($request);
87
        if (200 !== $response->getStatusCode()) {
88
            return null;
89
        }
90
91
        $this->accessToken = $response->getBody()->__toString();
92
93
        return $this->accessToken;
94
    }
95
96
    public function getAccessToken(): ?string
97
    {
98
        return $this->accessToken;
99
    }
100
}
101