Passed
Push — master ( 78880b...ac7e2f )
by Jens
20:00 queued 13s
created

AnonymousFlowTokenProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 31
c 1
b 0
f 1
dl 0
loc 92
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getToken() 0 20 2
A refreshToken() 0 3 1
1
<?php
2
3
namespace Commercetools\Core\Client\OAuth;
4
5
use GuzzleHttp\Client;
6
7
class AnonymousFlowTokenProvider implements RefreshTokenProvider
8
{
9
    const GRANT_TYPE = 'grant_type';
10
    const GRANT_TYPE_CLIENT_CREDENTIALS = 'client_credentials';
11
    const SCOPE = 'scope';
12
    const REFRESH_TOKEN = 'refresh_token';
13
    const ACCESS_TOKEN = 'access_token';
14
    const EXPIRES_IN = 'expires_in';
15
16
    /**
17
     * @var Client
18
     */
19
    private $client;
20
21
    /**
22
     * @var ClientCredentials
23
     */
24
    private $credentials;
25
26
    /**
27
     * @var string
28
     */
29
    private $accessTokenUrl;
30
31
    /**
32
     * @var TokenStorage
33
     */
34
    private $tokenStorage;
0 ignored issues
show
introduced by
The private property $tokenStorage is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @var RefreshFlowTokenProvider
38
     */
39
    private $refreshTokenProvider;
40
41
    /**
42
     * @var AnonymousIdProvider
43
     */
44
    private $anonymousIdProvider;
45
46
47
    /**
48
     * @param Client $client
49
     * @param $accessTokenUrl
50
     * @param ClientCredentials $credentials
51
     * @param RefreshFlowTokenProvider $refreshTokenProvider
52
     * @param AnonymousIdProvider|null $anonymousIdProvider
53
     */
54 11
    public function __construct(
55
        Client $client,
56
        $accessTokenUrl,
57
        ClientCredentials $credentials,
58
        RefreshFlowTokenProvider $refreshTokenProvider,
59
        AnonymousIdProvider $anonymousIdProvider = null
60
    ) {
61 11
        $this->accessTokenUrl = $accessTokenUrl;
62 11
        $this->client = $client;
63 11
        $this->credentials = $credentials;
64 11
        $this->refreshTokenProvider = $refreshTokenProvider;
65 11
        $this->anonymousIdProvider = $anonymousIdProvider;
66 11
    }
67
68
    /**
69
     * @return Token
70
     */
71 4
    public function getToken()
72
    {
73
        $data = [
74 4
            self::GRANT_TYPE => self::GRANT_TYPE_CLIENT_CREDENTIALS,
75
        ];
76 4
        if ($this->anonymousIdProvider) {
77 1
            $data['anonymous_id'] = $this->anonymousIdProvider->getAnonymousId();
78
        }
79
        $options = [
80 4
            'form_params' => $data,
81 4
            'auth' => [$this->credentials->getClientId(), $this->credentials->getClientSecret()]
82
        ];
83
84 4
        $result = $this->client->post($this->accessTokenUrl, $options);
85
86 4
        $body = json_decode((string)$result->getBody(), true);
87 4
        $token = new Token((string)$body[self::ACCESS_TOKEN], (int)$body[self::EXPIRES_IN], $body[self::SCOPE]);
88 4
        $token->setRefreshToken((string)$body[self::REFRESH_TOKEN]);
89
90 4
        return $token;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96 1
    public function refreshToken()
97
    {
98 1
        return $this->refreshTokenProvider->refreshToken();
99
    }
100
}
101