gateway = $gateway; $this->auth = $auth; $this->clientFactory = $clientFactory; } /** * @param int $userId * * @throws GoogleAccountNotFoundException * @throws NoRefreshTokenException * * @return GoogleApiClient */ public function createClient(int $userId): GoogleApiClient { $account = $this->gateway->getAccountByUser($userId); try{ $token = $this->gateway->getAccessToken($account->getId()); } catch (NoAccessTokenException $e) { $token = null; } if ($token === null || $token->getTimeToLive() < 10) { $token = $this->auth->refreshAccessToken($account); } $options = new RequestOptions(); $options->setHeader( 'Authorization', sprintf('Bearer %s', $token->getToken()) ); $options->setHeader('Accept', 'application/json'); $httpClient = $this->clientFactory->createClient($options); $client = new GoogleApiClient($httpClient, $account); $client->setLogger($this->logger); return $client; } }