Skip to content

Commit e7358b6

Browse files
committed
feat(elasticsearch): introduce v7 support
1 parent eb53218 commit e7358b6

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"doctrine/mongodb-odm": "^2.6",
128128
"doctrine/mongodb-odm-bundle": "^4.0 || ^5.0",
129129
"doctrine/orm": "^2.17 || ^3.0",
130-
"elasticsearch/elasticsearch": "^8.4",
130+
"elasticsearch/elasticsearch": "^7.17 || ^8.4",
131131
"friends-of-behat/mink-browserkit-driver": "^1.3.1",
132132
"friends-of-behat/mink-extension": "^2.2",
133133
"friends-of-behat/symfony-extension": "^2.1",

src/Elasticsearch/State/CollectionProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use ApiPlatform\State\ApiResource\Error;
2222
use ApiPlatform\State\Pagination\Pagination;
2323
use ApiPlatform\State\ProviderInterface;
24+
use Elasticsearch\Client as V7Client;
25+
use Elasticsearch\Common\Exceptions\Missing404Exception as V7Missing404Exception;
2426
use Elastic\Elasticsearch\Client;
2527
use Elastic\Elasticsearch\Exception\ClientResponseException;
2628
use Elastic\Elasticsearch\Response\Elasticsearch;
@@ -37,7 +39,7 @@ final class CollectionProvider implements ProviderInterface
3739
/**
3840
* @param RequestBodySearchCollectionExtensionInterface[] $collectionExtensions
3941
*/
40-
public function __construct(private readonly Client $client, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?Pagination $pagination = null, private readonly iterable $collectionExtensions = [], private readonly ?InflectorInterface $inflector = new Inflector())
42+
public function __construct(private readonly V7Client|Client $client, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?Pagination $pagination = null, private readonly iterable $collectionExtensions = [], private readonly ?InflectorInterface $inflector = new Inflector())
4143
{
4244
}
4345

@@ -69,12 +71,14 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6971

7072
try {
7173
$documents = $this->client->search($params);
74+
} catch (V7Missing404Exception $e) {
75+
throw new Error(status: $e->getCode(), detail: $e->getMessage(), title: $e->getMessage(), originalTrace: $e->getTrace());
7276
} catch (ClientResponseException $e) {
7377
$response = $e->getResponse();
7478
throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
7579
}
7680

77-
if ($documents instanceof Elasticsearch) {
81+
if ($this->client instanceof Client && $documents instanceof Elasticsearch) {
7882
$documents = $documents->asArray();
7983
}
8084

src/Elasticsearch/State/ItemProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use ApiPlatform\Metadata\Util\Inflector;
2121
use ApiPlatform\State\ApiResource\Error;
2222
use ApiPlatform\State\ProviderInterface;
23+
use Elasticsearch\Client as V7Client;
24+
use Elasticsearch\Common\Exceptions\Missing404Exception as V7Missing404Exception;
2325
use Elastic\Elasticsearch\Client;
2426
use Elastic\Elasticsearch\Exception\ClientResponseException;
2527
use Elastic\Elasticsearch\Response\Elasticsearch;
@@ -34,7 +36,7 @@
3436
*/
3537
final class ItemProvider implements ProviderInterface
3638
{
37-
public function __construct(private readonly Client $client, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?InflectorInterface $inflector = new Inflector())
39+
public function __construct(private readonly V7Client|Client $client, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?InflectorInterface $inflector = new Inflector())
3840
{
3941
}
4042

@@ -56,6 +58,8 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5658

5759
try {
5860
$document = $this->client->get($params);
61+
} catch (V7Missing404Exception) {
62+
return null;
5963
} catch (ClientResponseException $e) {
6064
$response = $e->getResponse();
6165
if (404 === $response->getStatusCode()) {
@@ -65,7 +69,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6569
throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
6670
}
6771

68-
if ($document instanceof Elasticsearch) {
72+
if ($this->client instanceof Client && $documents instanceof Elasticsearch) {
6973
$document = $document->asArray();
7074
}
7175

src/Elasticsearch/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"api-platform/metadata": "^3.4 || ^4.0",
2828
"api-platform/serializer": "^3.4 || ^4.0",
2929
"api-platform/state": "^3.4 || ^4.0",
30-
"elasticsearch/elasticsearch": "^8.4",
30+
"elasticsearch/elasticsearch": "^7.17 || ^8.4",
3131
"symfony/cache": "^6.4 || ^7.0",
3232
"symfony/console": "^6.4 || ^7.0",
3333
"symfony/property-access": "^6.4 || ^7.0",

0 commit comments

Comments
 (0)