forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionProvider.php
More file actions
106 lines (91 loc) · 3.91 KB
/
CollectionProvider.php
File metadata and controls
106 lines (91 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Elasticsearch\State;
use ApiPlatform\Elasticsearch\Extension\RequestBodySearchCollectionExtensionInterface;
use ApiPlatform\Elasticsearch\Paginator;
use ApiPlatform\Metadata\InflectorInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Util\Inflector;
use ApiPlatform\State\ApiResource\Error;
use ApiPlatform\State\Pagination\Pagination;
use ApiPlatform\State\ProviderInterface;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Elasticsearch\Client as V7Client;
use Elasticsearch\Common\Exceptions\Missing404Exception as V7Missing404Exception;
use OpenSearch\Client as OpenSearchClient;
use OpenSearch\Common\Exceptions\Missing404Exception as OpenSearchMissing404Exception;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* Collection provider for Elasticsearch.
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
* @author Vincent Chalamon <vincentchalamon@gmail.com>
*/
final class CollectionProvider implements ProviderInterface
{
/**
* @param RequestBodySearchCollectionExtensionInterface[] $collectionExtensions
*/
public function __construct(
private readonly V7Client|Client|OpenSearchClient $client, // @phpstan-ignore-line
private readonly ?DenormalizerInterface $denormalizer = null,
private readonly ?Pagination $pagination = null,
private readonly iterable $collectionExtensions = [],
private readonly ?InflectorInterface $inflector = new Inflector(),
) {
}
/**
* {@inheritdoc}
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Paginator
{
$resourceClass = $operation->getClass();
$body = [];
foreach ($this->collectionExtensions as $collectionExtension) {
$body = $collectionExtension->applyToCollection($body, $resourceClass, $operation, $context);
}
if (!isset($body['query']) && !isset($body['aggs'])) {
$body['query'] = ['match_all' => new \stdClass()];
}
$limit = $body['size'] ??= $this->pagination->getLimit($operation, $context);
$offset = $body['from'] ??= $this->pagination->getOffset($operation, $context);
$options = $operation->getStateOptions() instanceof Options ? $operation->getStateOptions() : new Options(index: $this->getIndex($operation));
$params = [
'index' => $options->getIndex() ?? $this->getIndex($operation),
'body' => $body,
];
try {
$documents = $this->client->search($params); // @phpstan-ignore-line
} catch (V7Missing404Exception|OpenSearchMissing404Exception $e) { // @phpstan-ignore-line
throw new Error(status: $e->getCode(), detail: $e->getMessage(), title: $e->getMessage(), originalTrace: $e->getTrace()); // @phpstan-ignore-line
} catch (ClientResponseException $e) {
$response = $e->getResponse();
throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
}
if (class_exists(Elasticsearch::class) && $documents instanceof Elasticsearch) {
$documents = $documents->asArray();
}
return new Paginator(
$this->denormalizer,
$documents,
$resourceClass,
$limit,
$offset,
$context
);
}
private function getIndex(Operation $operation): string
{
return $this->inflector->tableize($operation->getShortName());
}
}