forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemProvider.php
More file actions
92 lines (79 loc) · 3.27 KB
/
ItemProvider.php
File metadata and controls
92 lines (79 loc) · 3.27 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
<?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\Serializer\DocumentNormalizer;
use ApiPlatform\Metadata\InflectorInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Util\Inflector;
use ApiPlatform\State\ApiResource\Error;
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\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* Item provider for Elasticsearch.
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
* @author Vincent Chalamon <vincentchalamon@gmail.com>
*/
final class ItemProvider implements ProviderInterface
{
public function __construct(
private readonly V7Client|Client|OpenSearchClient $client, // @phpstan-ignore-line
private readonly ?DenormalizerInterface $denormalizer = null,
private readonly ?InflectorInterface $inflector = new Inflector(),
) {
}
/**
* {@inheritdoc}
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
{
$resourceClass = $operation->getClass();
$options = $operation->getStateOptions();
if (!$options instanceof Options) {
$options = new Options(index: $this->getIndex($operation));
}
$params = [
'index' => $options->getIndex() ?? $this->getIndex($operation),
'id' => (string) reset($uriVariables),
];
try {
$document = $this->client->get($params); // @phpstan-ignore-line
} catch (V7Missing404Exception|OpenSearchMissing404Exception) { // @phpstan-ignore-line
return null;
} catch (ClientResponseException $e) {
$response = $e->getResponse();
if (404 === $response->getStatusCode()) {
return null;
}
throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
}
if (class_exists(Elasticsearch::class) && $document instanceof Elasticsearch) {
$document = $document->asArray();
}
$item = $this->denormalizer->denormalize($document, $resourceClass, DocumentNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true]);
if (!\is_object($item) && null !== $item) {
throw new \UnexpectedValueException('Expected item to be an object or null.');
}
return $item;
}
private function getIndex(Operation $operation): string
{
return $this->inflector->tableize($operation->getShortName());
}
}