1: <?php
2:
3: namespace OpenSearch;
4:
5: use OpenSearch\Aws\SigningClientFactory;
6: use OpenSearch\HttpClient\SymfonyHttpClientFactory;
7: use OpenSearch\Serializers\SmartSerializer;
8: use Psr\Log\LoggerInterface;
9:
10: /**
11: * Creates an OpenSearch client using Symfony HTTP Client.
12: */
13: class SymfonyClientFactory implements ClientFactoryInterface
14: {
15: public function __construct(
16: protected int $maxRetries = 0,
17: protected ?LoggerInterface $logger = null,
18: protected ?SigningClientFactory $awsSigningHttpClientFactory = null,
19: ) {
20: }
21:
22: /**
23: * Creates a new OpenSearch client using Symfony HTTP Client.
24: *
25: * @param array<string,mixed> $options
26: * The Symfony HTTP Client options.
27: */
28: public function create(array $options): Client
29: {
30: // Clean up the options array for the Symfony HTTP Client.
31: if (isset($options['auth_aws'])) {
32: $awsAuth = $options['auth_aws'];
33: unset($options['auth_aws']);
34: }
35:
36: $httpClient = (new SymfonyHttpClientFactory($this->maxRetries, $this->logger))->create($options);
37: $serializer = new SmartSerializer();
38:
39: $requestFactory = new RequestFactory(
40: $httpClient,
41: $httpClient,
42: $httpClient,
43: $serializer,
44: );
45:
46: $transportFactory = (new TransportFactory())
47: ->setRequestFactory($requestFactory);
48:
49: if (isset($awsAuth)) {
50: if (!isset($awsAuth['host'])) {
51: $awsAuth['host'] = parse_url($options['base_uri'], PHP_URL_HOST);
52: }
53: $signingClient = $this->getSigningClientFactory()->create($httpClient, $awsAuth);
54: $transportFactory->setHttpClient($signingClient);
55: } else {
56: $transportFactory->setHttpClient($httpClient);
57: }
58:
59: return new Client($transportFactory->create(), new EndpointFactory($serializer));
60: }
61:
62: /**
63: * Gets the AWS signing client factory.
64: */
65: protected function getSigningClientFactory(): SigningClientFactory
66: {
67: if ($this->awsSigningHttpClientFactory === null) {
68: $this->awsSigningHttpClientFactory = new SigningClientFactory();
69: }
70: return $this->awsSigningHttpClientFactory;
71: }
72: }
73: