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

RefreshFlowTokenProvider::refreshToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
c 1
b 0
f 1
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.8666
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Commercetools\Core\Client\OAuth;
4
5
use GuzzleHttp\Client;
6
7
class RefreshFlowTokenProvider implements RefreshTokenProvider
8
{
9
    const GRANT_TYPE = 'grant_type';
10
    const GRANT_TYPE_REFRESH_TOKEN = 'refresh_token';
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 AnonymousFlowTokenProvider
33
     */
34
    private $anonymousFlowTokenProvider;
0 ignored issues
show
introduced by
The private property $anonymousFlowTokenProvider is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @var TokenStorage
38
     */
39
    private $tokenStorage;
40
41
    /**
42
     * @param Client $client
43
     * @param $accessTokenUrl
44
     * @param ClientCredentials $credentials
45
     * @param TokenStorage $tokenStorage
46
     */
47 11
    public function __construct(
48
        Client $client,
49
        $accessTokenUrl,
50
        ClientCredentials $credentials,
51
        TokenStorage $tokenStorage
52
    ) {
53 11
        $this->accessTokenUrl = $accessTokenUrl;
54 11
        $this->client = $client;
55 11
        $this->credentials = $credentials;
56 11
        $this->tokenStorage = $tokenStorage;
57 11
    }
58
59
    /**
60
     * @return Token
61
     */
62 1
    public function getToken()
63
    {
64 1
        return $this->refreshToken();
65
    }
66
67
    /**
68
     * @return Token
69
     */
70 2
    public function refreshToken()
71
    {
72 2
        $refreshToken = $this->tokenStorage->getRefreshToken();
73
        $data = [
74 2
            self::GRANT_TYPE => self::GRANT_TYPE_REFRESH_TOKEN,
75 2
            self::REFRESH_TOKEN => $refreshToken
76
        ];
77
        $options = [
78 2
            'form_params' => $data,
79 2
            'auth' => [$this->credentials->getClientId(), $this->credentials->getClientSecret()]
80
        ];
81
82 2
        $result = $this->client->post($this->accessTokenUrl, $options);
83
84 2
        $body = json_decode((string)$result->getBody(), true);
85 2
        $token = new Token((string)$body[self::ACCESS_TOKEN], (int)$body[self::EXPIRES_IN], $body[self::SCOPE]);
86 2
        $token->setRefreshToken($refreshToken);
87
88 2
        return $token;
89
    }
90
}
91