Skip to content

Commit 8263aac

Browse files
authored
Make EndpointFactory and optional Client constructor param (#315)
* Make EndpointFactory and optional Client constructor param Signed-off-by: Kim Pepper <[email protected]> * Adds test Signed-off-by: Kim Pepper <[email protected]> --------- Signed-off-by: Kim Pepper <[email protected]>
1 parent 88235f6 commit 8263aac

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66

77
### Added
88
### Changed
9+
- Updated Client constructor to make EndpointFactory and optional parameter.
910
### Deprecated
1011
### Removed
1112
### Fixed

src/OpenSearch/Client.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,14 @@ class Client
317317
* Client constructor
318318
*
319319
* @param TransportInterface|Transport $transport
320-
* @param callable|EndpointFactoryInterface $endpointFactory
320+
* @param callable|EndpointFactoryInterface|null $endpointFactory
321321
* @param NamespaceBuilderInterface[] $registeredNamespaces
322322
*
323323
* @phpstan-ignore parameter.deprecatedClass
324324
*/
325325
public function __construct(
326326
TransportInterface|Transport $transport,
327-
callable|EndpointFactoryInterface $endpointFactory,
327+
callable|EndpointFactoryInterface|null $endpointFactory = null,
328328
array $registeredNamespaces = [],
329329
) {
330330
if (!$transport instanceof TransportInterface) {
@@ -336,17 +336,22 @@ public function __construct(
336336
} else {
337337
$this->httpTransport = $transport;
338338
}
339+
339340
if (is_callable($endpointFactory)) {
340341
@trigger_error('Passing a callable as the $endpointFactory param in ' . __METHOD__ . ' is deprecated in 2.4.0 and will be removed in 3.0.0. Pass an instance of \OpenSearch\EndpointFactoryInterface instead.', E_USER_DEPRECATED);
341342
$endpoints = $endpointFactory;
342343
// @phpstan-ignore new.deprecated
343344
$endpointFactory = new LegacyEndpointFactory($endpointFactory);
344345
} else {
346+
if ($endpointFactory === null) {
347+
$endpointFactory = new EndpointFactory();
348+
}
345349
$endpoints = function ($c) use ($endpointFactory) {
346350
@trigger_error('The $endpoints property is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
347351
return $endpointFactory->getEndpoint('OpenSearch\\Endpoints\\' . $c);
348352
};
349353
}
354+
350355
// @phpstan-ignore property.deprecated
351356
$this->endpoints = $endpoints;
352357
$this->endpointFactory = $endpointFactory;

tests/ClientTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,26 @@ public function testSendRawRequest(): void
122122
$this->assertEquals(['bang'], $response);
123123
}
124124

125+
/**
126+
* @covers ::request
127+
*/
128+
public function testOptionalEndpointFactory(): void
129+
{
130+
131+
$this->transport->expects($this->once())
132+
->method('sendRequest')
133+
->with('GET', '/', ['foo' => 'bar'], 'whizz')
134+
->willReturn(['bang']);
135+
136+
$this->client = new Client($this->transport);
137+
138+
$response = $this->client->request('GET', '/', [
139+
'params' => ['foo' => 'bar'],
140+
'body' => 'whizz',
141+
]);
142+
143+
$this->assertEquals(['bang'], $response);
144+
145+
}
146+
125147
}

util/template/client-class

+7-2
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ class Client
7070
* Client constructor
7171
*
7272
* @param TransportInterface|Transport $transport
73-
* @param callable|EndpointFactoryInterface $endpointFactory
73+
* @param callable|EndpointFactoryInterface|null $endpointFactory
7474
* @param NamespaceBuilderInterface[] $registeredNamespaces
7575
*
7676
* @phpstan-ignore parameter.deprecatedClass
7777
*/
7878
public function __construct(
7979
TransportInterface|Transport $transport,
80-
callable|EndpointFactoryInterface $endpointFactory,
80+
callable|EndpointFactoryInterface|null $endpointFactory = null,
8181
array $registeredNamespaces = [],
8282
) {
8383
if (!$transport instanceof TransportInterface) {
@@ -89,17 +89,22 @@ class Client
8989
} else {
9090
$this->httpTransport = $transport;
9191
}
92+
9293
if (is_callable($endpointFactory)) {
9394
@trigger_error('Passing a callable as the $endpointFactory param in ' . __METHOD__ . ' is deprecated in 2.4.0 and will be removed in 3.0.0. Pass an instance of \OpenSearch\EndpointFactoryInterface instead.', E_USER_DEPRECATED);
9495
$endpoints = $endpointFactory;
9596
// @phpstan-ignore new.deprecated
9697
$endpointFactory = new LegacyEndpointFactory($endpointFactory);
9798
} else {
99+
if ($endpointFactory === null) {
100+
$endpointFactory = new EndpointFactory();
101+
}
98102
$endpoints = function ($c) use ($endpointFactory) {
99103
@trigger_error('The $endpoints property is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
100104
return $endpointFactory->getEndpoint('OpenSearch\\Endpoints\\' . $c);
101105
};
102106
}
107+
103108
// @phpstan-ignore property.deprecated
104109
$this->endpoints = $endpoints;
105110
$this->endpointFactory = $endpointFactory;

0 commit comments

Comments
 (0)