Completed
Pull Request — master (#10)
by Tobias
07:59
created

Authenticator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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
 * @internal
15
 */
16
final class Authenticator
17
{
18
    /**
19
     * @var RequestBuilder
20
     */
21
    private $requestBuilder;
22
23
    /**
24
     * @var HttpClient
25
     */
26
    private $httpClient;
27
28
    /**
29
     * @var string|null
30
     */
31
    private $accessToken;
32
33
    /**
34
     * @var string
35
     */
36
    private $clientId;
37
38
    /**
39
     * @var string
40
     */
41
    private $clientSecret;
42
43
    public function __construct(RequestBuilder $requestBuilder, HttpClient $httpClient, string $clientId, string $clientSecret)
44
    {
45
        $this->requestBuilder = $requestBuilder;
46
        $this->httpClient = $httpClient;
47
        $this->clientId = $clientId;
48
        $this->clientSecret = $clientSecret;
49
    }
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 ($response->getStatusCode() !== 200) {
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 ($response->getStatusCode() !== 200) {
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