Passed
Pull Request — master (#1999)
by Antoine
02:58
created

ItemDataProvider::getItem()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 23
rs 9.8333
c 0
b 0
f 0
cc 4
nc 5
nop 4
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\Exception\IndexNotFoundException;
17
use ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Factory\DocumentMetadataFactoryInterface;
18
use ApiPlatform\Core\Bridge\Elasticsearch\Serializer\ItemNormalizer;
19
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
20
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
21
use ApiPlatform\Core\Exception\InvalidArgumentException;
22
use Elasticsearch\Client;
23
use Elasticsearch\Common\Exceptions\Missing404Exception;
24
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
25
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
26
27
/**
28
 * Item data provider for Elasticsearch.
29
 *
30
 * @experimental
31
 *
32
 * @author Baptiste Meyer <[email protected]>
33
 */
34
final class ItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
35
{
36
    private $client;
37
    private $indexMetadataFactory;
38
    private $denormalizer;
39
40
    public function __construct(Client $client, DocumentMetadataFactoryInterface $indexMetadataFactory, DenormalizerInterface $denormalizer)
41
    {
42
        $this->client = $client;
43
        $this->indexMetadataFactory = $indexMetadataFactory;
44
        $this->denormalizer = $denormalizer;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
51
    {
52
        try {
53
            $this->indexMetadataFactory->create($resourceClass);
54
        } catch (IndexNotFoundException $e) {
55
            return false;
56
        }
57
58
        return true;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
65
    {
66
        if (is_array($id)) {
67
            if (1 !== \count($id)) {
68
                throw new InvalidArgumentException('Composite identifiers not supported.');
69
            }
70
71
            $id = array_values($id)[0];
72
        }
73
74
        $indexMetadata = $this->indexMetadataFactory->create($resourceClass);
75
76
        try {
77
            $document = $this->client->get([
78
                'index' => $indexMetadata->getIndex(),
79
                'type' => $indexMetadata->getType(),
80
                'id' => (string) $id,
81
            ]);
82
        } catch (Missing404Exception $e) {
83
            return null;
84
        }
85
86
        return $this->denormalizer->denormalize($document, $resourceClass, ItemNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true]);
87
    }
88
}
89