1 | <?php |
||
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 | 38 | public function __construct($config, $cache = null) |
|
48 | { |
||
49 | 38 | parent::__construct($config); |
|
50 | 38 | $this->cacheKeys = []; |
|
51 | 38 | $this->setCacheAdapter($cache); |
|
52 | 38 | } |
|
53 | |||
54 | /** |
||
55 | * @return CacheAdapterFactory |
||
56 | */ |
||
57 | 38 | public function getCacheAdapterFactory() |
|
64 | |||
65 | /** |
||
66 | * @param $cache |
||
67 | * @return $this |
||
68 | */ |
||
69 | 38 | public function setCacheAdapter($cache) |
|
75 | |||
76 | /** |
||
77 | * @return CacheAdapterInterface |
||
78 | */ |
||
79 | 282 | public function getCacheAdapter() |
|
83 | |||
84 | /** |
||
85 | * @param string $scope |
||
|
|||
86 | * @return Token |
||
87 | * @throws InvalidClientCredentialsException |
||
88 | */ |
||
89 | 282 | public function getToken() |
|
98 | |||
99 | /** |
||
100 | * @return Token |
||
101 | * @throws InvalidClientCredentialsException |
||
102 | */ |
||
103 | 12 | public function refreshToken() |
|
104 | { |
||
105 | 12 | $scope = $this->getConfig()->getScope(); |
|
106 | 12 | $grantType = $this->getConfig()->getGrantType(); |
|
107 | 12 | $data = [Config::SCOPE => $scope, Config::GRANT_TYPE => $grantType]; |
|
108 | |||
109 | 12 | if ($grantType === Config::GRANT_TYPE_PASSWORD) { |
|
110 | 4 | $user = $this->getConfig()->getUsername(); |
|
111 | 4 | $password = $this->getConfig()->getPassword(); |
|
112 | 4 | $data[Config::USER_NAME] = $user; |
|
113 | 4 | $data[Config::PASSWORD] = $password; |
|
114 | 9 | } elseif ($grantType === Config::GRANT_TYPE_REFRESH) { |
|
115 | 1 | $refreshToken = $this->getConfig()->getRefreshToken(); |
|
116 | 1 | $data[Config::REFRESH_TOKEN] = $refreshToken; |
|
117 | } |
||
118 | |||
119 | 12 | $token = $this->getBearerToken($data); |
|
120 | |||
121 | 11 | if ($grantType === Config::GRANT_TYPE_PASSWORD) { |
|
122 | 4 | $this->getConfig()->setGrantType(Config::GRANT_TYPE_REFRESH); |
|
123 | 4 | $this->getConfig()->setRefreshToken($token->getRefreshToken()); |
|
124 | } |
||
125 | |||
126 | // ensure token to be invalidated in cache before TTL |
||
127 | 11 | $ttl = max(1, floor($token->getTtl()/2)); |
|
128 | 11 | $this->getCacheAdapter()->store($this->getCacheKey(), $token->getToken(), $ttl); |
|
129 | |||
130 | 11 | return $token; |
|
131 | } |
||
132 | |||
133 | 282 | protected function getCacheToken() |
|
137 | |||
138 | /** |
||
139 | * @return string |
||
140 | */ |
||
141 | 282 | protected function getCacheKey() |
|
142 | { |
||
143 | 282 | $scope = $this->getConfig()->getScope(); |
|
144 | 282 | $grantType = $this->getConfig()->getGrantType(); |
|
145 | 282 | $cacheScope = $scope . '-' . $grantType; |
|
146 | |||
147 | 282 | if ($grantType === Config::GRANT_TYPE_PASSWORD) { |
|
148 | 3 | $user = $this->getConfig()->getUsername(); |
|
149 | 3 | $cacheScope .= '-' . $user; |
|
150 | 282 | } elseif ($grantType === Config::GRANT_TYPE_REFRESH) { |
|
151 | 4 | $token = $this->getConfig()->getRefreshToken(); |
|
152 | 4 | $cacheScope .= '-' . $token; |
|
153 | } |
||
154 | |||
155 | 282 | if (!isset($this->cacheKeys[$cacheScope])) { |
|
156 | 11 | $this->cacheKeys[$cacheScope] = static::TOKEN_CACHE_KEY . '-' . |
|
157 | 11 | sha1($cacheScope); |
|
158 | } |
||
159 | |||
160 | 282 | 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 | 12 | protected function getBearerToken(array $data) |
|
189 | |||
190 | /** |
||
191 | * @param $data |
||
192 | * @return ResponseInterface |
||
193 | */ |
||
194 | 12 | public function execute($data) |
|
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | 12 | protected function getBaseUrl() |
|
211 | } |
||
212 |
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 methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.