1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the API Platform project. |
||
5 | * |
||
6 | * (c) Kévin Dunglas <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace ApiPlatform\Core\Bridge\Elasticsearch\DataProvider; |
||
15 | |||
16 | use ApiPlatform\Core\Bridge\Elasticsearch\Api\IdentifierExtractorInterface; |
||
17 | use ApiPlatform\Core\Bridge\Elasticsearch\Exception\IndexNotFoundException; |
||
18 | use ApiPlatform\Core\Bridge\Elasticsearch\Exception\NonUniqueIdentifierException; |
||
19 | use ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\Factory\DocumentMetadataFactoryInterface; |
||
20 | use ApiPlatform\Core\Bridge\Elasticsearch\Serializer\ItemNormalizer; |
||
21 | use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; |
||
22 | use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; |
||
23 | use ApiPlatform\Core\Exception\ResourceClassNotFoundException; |
||
24 | use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
||
25 | use Elasticsearch\Client; |
||
26 | use Elasticsearch\Common\Exceptions\Missing404Exception; |
||
27 | use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
||
28 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
||
29 | |||
30 | /** |
||
31 | * Item data provider for Elasticsearch. |
||
32 | * |
||
33 | * @experimental |
||
34 | * |
||
35 | * @author Baptiste Meyer <[email protected]> |
||
36 | */ |
||
37 | final class ItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface |
||
38 | { |
||
39 | private $client; |
||
40 | private $documentMetadataFactory; |
||
41 | private $identifierExtractor; |
||
42 | private $denormalizer; |
||
43 | private $resourceMetadataFactory; |
||
44 | |||
45 | public function __construct(Client $client, DocumentMetadataFactoryInterface $documentMetadataFactory, IdentifierExtractorInterface $identifierExtractor, DenormalizerInterface $denormalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory) |
||
46 | { |
||
47 | $this->client = $client; |
||
48 | $this->documentMetadataFactory = $documentMetadataFactory; |
||
49 | $this->identifierExtractor = $identifierExtractor; |
||
50 | $this->denormalizer = $denormalizer; |
||
51 | $this->resourceMetadataFactory = $resourceMetadataFactory; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function supports(string $resourceClass, ?string $operationName = null, array $context = []): bool |
||
58 | { |
||
59 | try { |
||
60 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
61 | if (false === $resourceMetadata->getItemOperationAttribute($operationName, 'elasticsearch', true, true)) { |
||
62 | return false; |
||
63 | } |
||
64 | } catch (ResourceClassNotFoundException $e) { |
||
65 | return false; |
||
66 | } |
||
67 | |||
68 | try { |
||
69 | $this->documentMetadataFactory->create($resourceClass); |
||
70 | } catch (IndexNotFoundException $e) { |
||
71 | return false; |
||
72 | } |
||
73 | |||
74 | try { |
||
75 | $this->identifierExtractor->getIdentifierFromResourceClass($resourceClass); |
||
76 | } catch (NonUniqueIdentifierException $e) { |
||
77 | return false; |
||
78 | } |
||
79 | |||
80 | return true; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function getItem(string $resourceClass, $id, ?string $operationName = null, array $context = []) |
||
87 | { |
||
88 | if (\is_array($id)) { |
||
89 | $id = $id[$this->identifierExtractor->getIdentifierFromResourceClass($resourceClass)]; |
||
90 | } |
||
91 | |||
92 | $documentMetadata = $this->documentMetadataFactory->create($resourceClass); |
||
93 | |||
94 | try { |
||
95 | $document = $this->client->get([ |
||
96 | 'index' => $documentMetadata->getIndex(), |
||
97 | 'type' => $documentMetadata->getType(), |
||
98 | 'id' => (string) $id, |
||
99 | ]); |
||
100 | } catch (Missing404Exception $e) { |
||
101 | return null; |
||
102 | } |
||
103 | |||
104 | return $this->denormalizer->denormalize($document, $resourceClass, ItemNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true]); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
105 | } |
||
106 | } |
||
107 |