commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
MiddlewareFactory.php
1 <?php
2 
3 declare(strict_types=1);
10 namespace Commercetools\Client;
11 
12 use GuzzleHttp\Exception\ServerException;
13 use GuzzleHttp\MessageFormatter;
14 use GuzzleHttp\Middleware;
15 use GuzzleHttp\Promise\PromiseInterface;
16 use Psr\Http\Message\RequestInterface;
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Log\LoggerInterface;
19 use Psr\Log\LogLevel;
20 
22 {
26  public static function createDefaultMiddlewares(
27  ?OAuth2Handler $handler = null,
28  ?LoggerInterface $logger = null,
29  int $maxRetries = 0,
30  ?CorrelationIdProvider $correlationIdProvider = null
31  ) {
32  $middlewares = [];
33  if (!is_null($handler)) {
34  $middlewares['oauth'] = self::createMiddlewareForOAuthHandler($handler);
35  $middlewares['reauth'] = self::createReauthenticateMiddleware($handler);
36  }
37  if (!is_null($logger)) {
38  $middlewares['logger'] = self::createLoggerMiddleware($logger);
39  }
40  if ($maxRetries > 0) {
41  $middlewares['retryNA'] = self::createRetryNAMiddleware($maxRetries);
42  }
43  $middlewares['correlation_id'] = self::createCorrelationIdMiddleware($correlationIdProvider ?? new DefaultCorrelationIdProvider());
44 
45  return $middlewares;
46  }
47 
51  public static function createCorrelationIdMiddleware(CorrelationIdProvider $correlationIdProvider)
52  {
53  return Middleware::mapRequest(
54  function (RequestInterface $request) use ($correlationIdProvider) {
55  return $request->withAddedHeader(
56  'X_CORRELATION_ID',
57  $correlationIdProvider->getCorrelationId()
58  );
59  }
60  );
61  }
62 
66  public static function createRetryNAMiddleware(int $maxRetries)
67  {
68  return Middleware::retry(
69  function (
70  int $retries,
71  RequestInterface $request,
72  ResponseInterface $response = null,
73  \Exception $error = null
74  ) use ($maxRetries) {
75  if ($response instanceof ResponseInterface && $response->getStatusCode() < 500) {
76  return false;
77  }
78  if ($retries < $maxRetries) {
79  return false;
80  }
81  if ($error instanceof ServerException && $error->getCode() == 503) {
82  return true;
83  }
84  if ($response instanceof ResponseInterface && $response->getStatusCode() == 503) {
85  return true;
86  }
87  return false;
88  }
89  );
90  }
91 
95  public static function createMiddlewareForOAuthHandler(OAuth2Handler $handler)
96  {
97  return Middleware::mapRequest($handler);
98  }
99 
104  public static function createLoggerMiddleware(LoggerInterface $logger, string $logLevel = LogLevel::INFO, string $template = MessageFormatter::CLF)
105  {
106  return Middleware::log($logger, new MessageFormatter($template), $logLevel);
107  }
108 
114  public static function createReauthenticateMiddleware(OAuth2Handler $oauthHandler, int $maxRetries = 1)
115  {
116  return
121  function (callable $handler) use ($oauthHandler, $maxRetries) {
122  return
127  function (RequestInterface $request, array $options) use ($handler, $oauthHandler, $maxRetries): PromiseInterface {
128  return $handler($request, $options)->then(
129  function (ResponseInterface $response) use (
130  $request,
131  $handler,
132  $oauthHandler,
133  $options,
134  $maxRetries
135  ) {
136  if ($response->getStatusCode() == 401) {
137  if (!isset($options['reauth'])) {
138  $options['reauth'] = 0;
139  }
140  if ($options['reauth'] < $maxRetries) {
141  $options['reauth']++;
142  $token = $oauthHandler->refreshToken();
143  $request = $request->withHeader(
144  'Authorization',
145  'Bearer ' . $token->getValue()
146  );
147  return $handler($request, $options);
148  }
149  }
150  return $response;
151  }
152  );
153  };
154  };
155  }
156 }
static createLoggerMiddleware(LoggerInterface $logger, string $logLevel=LogLevel::INFO, string $template=MessageFormatter::CLF)
static createReauthenticateMiddleware(OAuth2Handler $oauthHandler, int $maxRetries=1)
static createDefaultMiddlewares(?OAuth2Handler $handler=null, ?LoggerInterface $logger=null, int $maxRetries=0, ?CorrelationIdProvider $correlationIdProvider=null)
static createCorrelationIdMiddleware(CorrelationIdProvider $correlationIdProvider)
static createRetryNAMiddleware(int $maxRetries)
static createMiddlewareForOAuthHandler(OAuth2Handler $handler)