88use GuzzleHttp \HandlerStack ;
99use SixtyEightPublishers \AmpClient \Http \Cache \CacheControl ;
1010use SixtyEightPublishers \AmpClient \Http \Cache \CacheStorageInterface ;
11+ use SixtyEightPublishers \AmpClient \Http \Middleware \MiddlewareInterface ;
1112use SixtyEightPublishers \AmpClient \Response \Hydrator \ResponseHydratorInterface ;
1213
1314final class HttpClientFactory implements HttpClientFactoryInterface
@@ -17,15 +18,21 @@ final class HttpClientFactory implements HttpClientFactoryInterface
1718 /** @var array<string, mixed> */
1819 private array $ guzzleClientConfig ;
1920
21+ /** @var list<MiddlewareInterface> */
22+ private array $ middlewares ;
23+
2024 /**
21- * @param array<string, mixed> $guzzleClientConfig
25+ * @param array<string, mixed> $guzzleClientConfig
26+ * @param list<MiddlewareInterface> $middlewares
2227 */
2328 public function __construct (
2429 ResponseHydratorInterface $ responseHydrator ,
25- array $ guzzleClientConfig = []
30+ array $ guzzleClientConfig = [],
31+ array $ middlewares = []
2632 ) {
2733 $ this ->responseHydrator = $ responseHydrator ;
2834 $ this ->guzzleClientConfig = $ guzzleClientConfig ;
35+ $ this ->middlewares = $ middlewares ;
2936 }
3037
3138 public function create (
@@ -41,6 +48,10 @@ public function create(
4148 $ guzzleClientConfig ['handler ' ] = $ handlerStack ;
4249 $ guzzleClientConfig ['http_errors ' ] = false ;
4350
51+ foreach ($ this ->middlewares as $ middleware ) {
52+ $ middlewares = $ middlewares ->with ($ middleware );
53+ }
54+
4455 foreach ($ middlewares as $ middleware ) {
4556 $ handlerStack ->push ($ middleware , $ middleware ->getName ()); // @phpstan-ignore-line
4657 }
0 commit comments