commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
RefreshFlowTokenProvider.php
1 <?php
2 
3 declare(strict_types=1);
4 
5 namespace Commercetools\Client;
6 
7 use GuzzleHttp\Client;
8 
10 {
11  public const GRANT_TYPE = 'grant_type';
12  public const GRANT_TYPE_REFRESH_TOKEN = 'refresh_token';
13  public const REFRESH_TOKEN = 'refresh_token';
14  public const ACCESS_TOKEN = 'access_token';
15  public const EXPIRES_IN = 'expires_in';
16 
20  private $client;
21 
25  private $credentials;
26 
30  private $accessTokenUrl;
31 
35  private $tokenStorage;
36 
37  public function __construct(
38  Client $client,
39  string $accessTokenUrl,
40  ClientCredentials $credentials,
41  TokenStorage $tokenStorage
42  ) {
43  $this->accessTokenUrl = $accessTokenUrl;
44  $this->client = $client;
45  $this->credentials = $credentials;
46  $this->tokenStorage = $tokenStorage;
47  }
48 
49  public function getToken(): Token
50  {
51  return $this->refreshToken();
52  }
53 
54  public function refreshToken(): Token
55  {
56  $refreshToken = $this->tokenStorage->getRefreshToken();
57  $data = [
58  self::GRANT_TYPE => self::GRANT_TYPE_REFRESH_TOKEN,
59  self::REFRESH_TOKEN => $refreshToken,
60  ];
61  $options = [
62  'form_params' => $data,
63  'auth' => [$this->credentials->getClientId(), $this->credentials->getClientSecret()],
64  ];
65 
66  $result = $this->client->post($this->accessTokenUrl, $options);
67 
69  $body = json_decode((string) $result->getBody(), true);
70 
71  return new RefreshableTokenModel(
72  (string) $body[self::ACCESS_TOKEN],
73  (int) $body[self::EXPIRES_IN],
74  $refreshToken
75  );
76  }
77 }
__construct(Client $client, string $accessTokenUrl, ClientCredentials $credentials, TokenStorage $tokenStorage)