| Conditions | 12 |
| Paths | 12 |
| Total Lines | 53 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 174 | public function getToken($videoManagerId = null) |
||
| 175 | { |
||
| 176 | $logger = $this->getLogger(); |
||
| 177 | $this->logTokenData(); |
||
| 178 | |||
| 179 | $cacheKey = sha1(sprintf('%s.%s', __METHOD__, json_encode(func_get_args()))); |
||
| 180 | $cacheItem = $this->cacheItemPool->getItem($cacheKey); |
||
| 181 | if (!$this->accessToken && $cacheItem->isHit()) { |
||
| 182 | $this->accessToken = $cacheItem->get(); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Access token has expired, but expiration token has not expired. |
||
| 186 | // Issue ourselves a new access token for the same video manager. |
||
| 187 | if (!is_null($this->accessToken) |
||
| 188 | && $this->accessToken->expired() |
||
| 189 | && !$this->refreshToken->expired()) { |
||
| 190 | $logger->info('Access token has expired - getting new one for same video manager with refresh token'); |
||
| 191 | $tokenData = $this->createAccessTokenFromRefreshToken( |
||
| 192 | $this->refreshToken, |
||
| 193 | $this->accessToken->getVideoManagerId() |
||
| 194 | ); |
||
| 195 | |||
| 196 | $this->accessToken = $tokenData['accessToken']; |
||
| 197 | } elseif (is_null($this->accessToken) |
||
| 198 | || (!is_null($this->refreshToken) && $this->refreshToken->expired())) { |
||
| 199 | // Either we have no token, or the refresh token has expired |
||
| 200 | // so we will need to generate completely new tokens |
||
| 201 | $logger->info('No access token, or refresh token has expired - generate completely new ones'); |
||
| 202 | $tokenData = $this->createNewTokens(); |
||
| 203 | |||
| 204 | $this->accessToken = $tokenData['accessToken']; |
||
| 205 | $this->refreshToken = $tokenData['refreshToken']; |
||
| 206 | } |
||
| 207 | |||
| 208 | // Video manager is not matching with the one that our token |
||
| 209 | // was generated with - issue ourselves a token for the video manager |
||
| 210 | // we need. |
||
| 211 | if (!is_null($videoManagerId) |
||
| 212 | && isset($this->accessToken) |
||
| 213 | && $this->accessToken->getVideoManagerId() != $videoManagerId) { |
||
| 214 | $logger->info('Attempting to use token for different video manager - generate valid access token'); |
||
| 215 | $this->accessToken = $this->createAccessTokenFromRefreshToken($this->refreshToken, $videoManagerId); |
||
| 216 | } |
||
| 217 | |||
| 218 | $cacheItem->set($this->accessToken); |
||
| 219 | $cacheItem->expiresAt((new \DateTime()) |
||
| 220 | ->setTimestamp($this->accessToken->getTokenData()['exp']) |
||
| 221 | ->sub(new \DateInterval('PT30S')) |
||
| 222 | ); |
||
| 223 | $this->cacheItemPool->save($cacheItem); |
||
| 224 | |||
| 225 | return $this->accessToken->getTokenString(); |
||
| 226 | } |
||
| 227 | } |
||
| 228 |