Completed
Push — develop ( fe23c8...5eda7f )
by Jens
09:33
created

Manager::getToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 * @created: 22.01.15, 12:34
5
 */
6
7
namespace Commercetools\Core\Client\OAuth;
8
9
use Commercetools\Core\Config;
10
use Commercetools\Core\Error\ApiException;
11
use Psr\Http\Message\ResponseInterface;
12
use Commercetools\Core\AbstractHttpClient;
13
use Commercetools\Core\Cache\CacheAdapterFactory;
14
use Commercetools\Core\Cache\CacheAdapterInterface;
15
use Commercetools\Core\Error\InvalidClientCredentialsException;
16
17
/**
18
 * @package Commercetools\Core\OAuth
19
 * @internal
20
 */
21
class Manager extends AbstractHttpClient
22
{
23
    const TOKEN_CACHE_KEY = 'commercetools-io-access-token';
24
25
    const REFRESH_TOKEN = 'refresh_token';
26
    const ACCESS_TOKEN = 'access_token';
27
    const EXPIRES_IN = 'expires_in';
28
    const ERROR = 'error';
29
    const SCOPE = 'scope';
30
    const ERROR_DESCRIPTION = 'error_description';
31
32
    /**
33
     * @var array
34
     */
35
    protected $cacheKeys;
36
37
    /**
38
     * @var CacheAdapterInterface
39
     */
40
    protected $cacheAdapter;
41
42
    /**
43
     * @var CacheAdapterFactory
44
     */
45
    protected $cacheAdapterFactory;
46
47 35
    public function __construct($config, $cache = null)
48
    {
49 35
        parent::__construct($config);
50 35
        $this->cacheKeys = [];
51 35
        $this->setCacheAdapter($cache);
52 35
    }
53
54
    /**
55
     * @return CacheAdapterFactory
56
     */
57 35
    public function getCacheAdapterFactory()
58
    {
59 35
        if (is_null($this->cacheAdapterFactory)) {
60 35
            $this->cacheAdapterFactory = new CacheAdapterFactory();
61
        }
62 35
        return $this->cacheAdapterFactory;
63
    }
64
65
    /**
66
     * @param $cache
67
     * @return $this
68
     */
69 35
    public function setCacheAdapter($cache)
70
    {
71 35
        $this->cacheAdapter = $this->getCacheAdapterFactory()->get($cache);
72
73 35
        return $this;
74
    }
75
76
    /**
77
     * @return CacheAdapterInterface
78
     */
79 275
    public function getCacheAdapter()
80
    {
81 275
        return $this->cacheAdapter;
82
    }
83
84
    /**
85
     * @param string $scope
0 ignored issues
show
Bug introduced by
There is no parameter named $scope. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
86
     * @return Token
87
     * @throws InvalidClientCredentialsException
88
     */
89 275
    public function getToken()
90
    {
91 275
        $scope = $this->getConfig()->getScope();
92 275
        if ($token = $this->getCacheToken()) {
93 274
            return new Token($token, null, $scope);
94
        }
95
96 6
        return $this->refreshToken();
97
    }
98
99
    /**
100
     * @return Token
101
     * @throws InvalidClientCredentialsException
102
     */
103 8
    public function refreshToken()
104
    {
105 8
        $scope = $this->getConfig()->getScope();
106 8
        $grantType = $this->getConfig()->getGrantType();
107 8
        $data = [Config::SCOPE => $scope, Config::GRANT_TYPE => $grantType];
108
109 8
        if ($grantType === Config::GRANT_TYPE_PASSWORD) {
110 1
            $user = $this->getConfig()->getUsername();
111 1
            $password = $this->getConfig()->getPassword();
112 1
            $data[Config::USER_NAME] = $user;
113 1
            $data[Config::PASSWORD] = $password;
114 8
        } elseif ($grantType === Config::GRANT_TYPE_REFRESH) {
115 1
            $refreshToken = $this->getConfig()->getRefreshToken();
116 1
            $data[Config::REFRESH_TOKEN] = $refreshToken;
117
        }
118
        
119 8
        $token = $this->getBearerToken($data);
120
121 7
        if ($grantType === Config::GRANT_TYPE_PASSWORD) {
122 1
            $this->getConfig()->setGrantType(Config::GRANT_TYPE_REFRESH);
123 1
            $this->getConfig()->setRefreshToken($token->getRefreshToken());
124
        }
125
126
        // ensure token to be invalidated in cache before TTL
127 7
        $ttl = max(1, floor($token->getTtl()/2));
128 7
        $this->getCacheAdapter()->store($this->getCacheKey(), $token->getToken(), $ttl);
129
130 7
        return $token;
131
    }
132
133 275
    protected function getCacheToken()
134
    {
135 275
        return $this->getCacheAdapter()->fetch($this->getCacheKey());
136
    }
137
138
    /**
139
     * @return string
140
     */
141 275
    protected function getCacheKey()
142
    {
143 275
        $scope = $this->getConfig()->getScope();
144 275
        $grantType = $this->getConfig()->getGrantType();
145 275
        $cacheScope = $scope . '-' . $grantType;
146
147 275
        if ($grantType === Config::GRANT_TYPE_PASSWORD) {
148
            $user = $this->getConfig()->getUsername();
149
            $cacheScope .= '-' . $user;
150 275
        } elseif ($grantType === Config::GRANT_TYPE_REFRESH) {
151 1
            $token = $this->getConfig()->getRefreshToken();
152 1
            $cacheScope .= '-' . $token;
153
        }
154
155 275
        if (!isset($this->cacheKeys[$cacheScope])) {
156 8
            $this->cacheKeys[$cacheScope] = static::TOKEN_CACHE_KEY . '-' .
157 8
                sha1($cacheScope);
158
        }
159
160 275
        return $this->cacheKeys[$cacheScope];
161
    }
162
163
    /**
164
     * @param array $data
165
     * @return Token
166
     * @throws ApiException
167
     * @throws \Commercetools\Core\Error\BadGatewayException
168
     * @throws \Commercetools\Core\Error\GatewayTimeoutException
169
     * @throws \Commercetools\Core\Error\ServiceUnavailableException
170
     */
171 8
    protected function getBearerToken(array $data)
172
    {
173
        try {
174 8
            $response = $this->execute($data);
175 1
        } catch (ApiException $exception) {
176 1
            throw ApiException::create($exception->getRequest(), $exception->getResponse());
177
        }
178
179 7
        $result = json_decode($response->getBody(), true);
180
181 7
        $token = new Token($result[static::ACCESS_TOKEN], $result[static::EXPIRES_IN], $result[static::SCOPE]);
182 7
        $token->setValidTo(new \DateTime('now +' . $result[static::EXPIRES_IN] . ' seconds'));
183 7
        if (isset($result[static::REFRESH_TOKEN])) {
184 1
            $token->setRefreshToken($result[static::REFRESH_TOKEN]);
185
        }
186
187 7
        return $token;
188
    }
189
190
    /**
191
     * @param $data
192
     * @return ResponseInterface
193
     */
194 8
    public function execute($data)
195
    {
196 8
        return $this->getHttpClient()->authenticate(
197 8
            $this->getConfig()->getOauthUrl(),
198 8
            $this->getConfig()->getClientId(),
199 8
            $this->getConfig()->getClientSecret(),
200
            $data
201
        );
202
    }
203
204
    /**
205
     * @return string
206
     */
207 9
    protected function getBaseUrl()
208
    {
209 9
        return $this->getConfig()->getOauthUrl();
210
    }
211
}
212