commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
TokenStorageProvider.php
1 <?php
2 
3 declare(strict_types=1);
4 
5 namespace Commercetools\Client;
6 
8 {
12  private $tokenStorage;
13 
17  private $anonymousProvider;
18 
22  public function __construct(TokenStorage $tokenStorage, AnonymousFlowTokenProvider $anonymousProvider)
23  {
24  $this->tokenStorage = $tokenStorage;
25  $this->anonymousProvider = $anonymousProvider;
26  }
27 
31  public function refreshToken(): Token
32  {
33  $token = $this->anonymousProvider->refreshToken();
34  $this->storeToken($token);
35 
36  return $token;
37  }
38 
42  public function getToken(): Token
43  {
44  $token = $this->tokenStorage->getAccessToken();
45  if (!is_null($token)) {
46  return new TokenModel($token);
47  }
48 
49  if ($this->tokenStorage->getRefreshToken()) {
50  return $this->refreshToken();
51  }
52 
53  $token = $this->anonymousProvider->getToken();
54  $this->storeToken($token);
55 
56  return $token;
57  }
58 
59  private function storeToken(Token $token): void
60  {
61  $this->tokenStorage->setAccessToken($token->getValue());
62  if ($token instanceof RefreshableTokenModel) {
63  $this->tokenStorage->setRefreshToken($token->getRefreshToken());
64  }
65  }
66 }
__construct(TokenStorage $tokenStorage, AnonymousFlowTokenProvider $anonymousProvider)