/** * @return Token */ private function fetchNewToken() { $request = $this->requestFactory->createRequest('login', $this->consumer->exportToArray())->setMethod(Request::POST); try { $response = Json::decode($this->getCurlSender()->send($request)->getResponse()); return new Token($response->token, $this->consumer->getAppId(), new DateTime($response->expiration)); } catch (CurlException $e) { Debugger::log('Cannot fetch new token: ' . $e->getMessage(), 'logistics'); throw $e; } }
/** * @param Consumer $consumer * @return Token * @throws TokenNotFoundException */ public function findToken(Consumer $consumer) { $appId = $consumer->getAppId(); if (array_key_exists($appId, $this->tokens)) { foreach ($this->tokens[$appId] as $key => $token) { if ($token->isExpired()) { unset($this->tokens[$appId][$key]); } else { return $token; } } } throw new TokenNotFoundException("Missing valid token for consumer with ID {$appId}."); }