35 $this->validateCache($cache);
36 $this->cache = $cache;
37 $this->provider = $provider;
38 $this->cacheKey = self::TOKEN_CACHE_KEY .
"_" . ($cacheKey ?? sha1(self::TOKEN_CACHE_KEY));
45 private function validateCache($cache): void
47 if (!$cache instanceof CacheInterface && !$cache instanceof CacheItemPoolInterface) {
57 $token = $this->getCacheToken();
58 if (!is_null($token)) {
70 $token = $this->provider->refreshToken();
72 $ttl = max(1, ($token->getExpiresIn() - 300));
74 $this->cache($token, $ttl);
79 private function getCacheToken(): ?string
81 $cache = $this->cache;
82 if ($cache instanceof CacheInterface) {
84 return $cache->get($this->cacheKey,
null);
87 $item = $cache->getItem($this->cacheKey);
89 return (
string)$item->get();
95 private function cache(Token $token,
int $ttl): void
97 $cache = $this->cache;
98 if ($cache instanceof CacheInterface) {
99 $cache->set($this->cacheKey, $token->getValue(), $ttl);
101 $item = $cache->getItem($this->cacheKey)->set($token->getValue())->expiresAfter($ttl);