commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
ClientFactory.php
1<?php
2
3declare(strict_types=1);
10namespace Commercetools\Client;
11
13use GuzzleHttp\Client as HttpClient;
14use GuzzleHttp\ClientInterface;
15use GuzzleHttp\HandlerStack;
16use Psr\Log\LoggerInterface;
17
19{
24 public function createGuzzleClient(Config $config, ?AuthConfig $authConfig = null, ?LoggerInterface $logger = null, array $middlewares = []): ClientInterface
25 {
26 $handler = null;
27 if (!is_null($authConfig)) {
28 $handler = OAuthHandlerFactory::ofAuthConfig($authConfig);
29 }
30
31 return $this->createGuzzleClientForHandler($config, $handler, $logger, $middlewares);
32 }
33
38 public function createGuzzleClientForHandler(Config $config, ?OAuth2Handler $handler = null, ?LoggerInterface $logger = null, array $middlewares = []): ClientInterface
39 {
40 $middlewares = array_merge(
41 MiddlewareFactory::createDefaultMiddlewares($handler, $logger, (int) ($config->getOptions()['maxRetries'] ?? 0)),
42 $middlewares
43 );
44 return $this->createGuzzleClientWithOptions($config->getOptions(), $middlewares);
45 }
46
52 Config $config,
53 array $middlewares = []
54 ): ClientInterface {
55 return $this->createGuzzleClientWithOptions($config->getOptions(), $middlewares);
56 }
57
62 private function createGuzzleClientWithOptions(array $options, array $middlewares = []): ClientInterface
63 {
64 if (isset($options['handler']) && $options['handler'] instanceof HandlerStack) {
65 $stack = $options['handler'];
66 } else {
67 $stack = HandlerStack::create();
68 $options['handler'] = $stack;
69 }
70
71 $options = array_replace(
72 [
73 'allow_redirects' => false,
74 'verify' => true,
75 'timeout' => 60,
76 'connect_timeout' => 10,
77 'pool_size' => 25,
78 'headers' => []
79 ],
80 $options
81 );
82 if (!isset($options['headers']['accept-encoding']) && $this->acceptCompressedResponse()) {
83 $options['headers']['accept-encoding'] = 'gzip';
84 }
85 if (!isset($options['headers']['user-agent'])) {
86 $options['headers']['user-agent'] = (new UserAgentProvider())->getUserAgent();
87 }
88 foreach ($middlewares as $key => $middleware) {
89 if (!is_callable($middleware)) {
90 throw new InvalidArgumentException('Middleware isn\'t callable');
91 }
93 $name = is_numeric($key) ? '' : $key;
94 $stack->push($middleware, $name);
95 }
96
97 $client = new HttpClient($options);
98
99 return $client;
100 }
101
102 private function acceptCompressedResponse(): bool
103 {
104 if (function_exists('gzdecode')) {
105 return true;
106 }
107
108 return false;
109 }
110
111 public static function of(): ClientFactory
112 {
113 return new self();
114 }
115}
createGuzzleClientForHandler(Config $config, ?OAuth2Handler $handler=null, ?LoggerInterface $logger=null, array $middlewares=[])
createGuzzleClient(Config $config, ?AuthConfig $authConfig=null, ?LoggerInterface $logger=null, array $middlewares=[])
createGuzzleClientForMiddlewares(Config $config, array $middlewares=[])
static createDefaultMiddlewares(?OAuth2Handler $handler=null, ?LoggerInterface $logger=null, int $maxRetries=0, ?CorrelationIdProvider $correlationIdProvider=null)
static ofAuthConfig(AuthConfig $authConfig, $cache=null)