1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * Copyright OpenSearch Contributors
7: * SPDX-License-Identifier: Apache-2.0
8: *
9: * OpenSearch PHP client
10: *
11: * @link https://github.com/opensearch-project/opensearch-php/
12: * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
13: * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
14: * @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
15: *
16: * Licensed to Elasticsearch B.V under one or more agreements.
17: * Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
18: * the GNU Lesser General Public License, Version 2.1, at your option.
19: * See the LICENSE file in the project root for more information.
20: */
21:
22: namespace OpenSearch\Connections;
23:
24: use OpenSearch\Serializers\SerializerInterface;
25: use Psr\Log\LoggerInterface;
26:
27: class ConnectionFactory implements ConnectionFactoryInterface
28: {
29: /**
30: * @var array
31: */
32: private $connectionParams;
33:
34: /**
35: * @var SerializerInterface
36: */
37: private $serializer;
38:
39: /**
40: * @var LoggerInterface
41: */
42: private $logger;
43:
44: /**
45: * @var LoggerInterface
46: */
47: private $tracer;
48:
49: /**
50: * @var callable
51: */
52: private $handler;
53:
54: /**
55: * @param array{client?: array{headers?: array<string, list<string>>, curl?: array<int, mixed>}} $connectionParams
56: */
57: public function __construct(callable $handler, array $connectionParams, SerializerInterface $serializer, LoggerInterface $logger, LoggerInterface $tracer)
58: {
59: $this->handler = $handler;
60: $this->connectionParams = $connectionParams;
61: $this->logger = $logger;
62: $this->tracer = $tracer;
63: $this->serializer = $serializer;
64: }
65:
66: public function create(array $hostDetails): ConnectionInterface
67: {
68: if (isset($hostDetails['path'])) {
69: $hostDetails['path'] = rtrim($hostDetails['path'], '/');
70: }
71:
72: return new Connection(
73: $this->handler,
74: $hostDetails,
75: $this->connectionParams,
76: $this->serializer,
77: $this->logger,
78: $this->tracer
79: );
80: }
81: }
82: