Passed
Pull Request — master (#1999)
by
unknown
04:10
created

ItemDataProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 29
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getItem() 0 19 3
A __construct() 0 6 1
A supports() 0 15 3
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 Elasticsearch\Client;
24
use Elasticsearch\Common\Exceptions\Missing404Exception;
25
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
26
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
27
28
/**
29
 * Item data provider for Elasticsearch.
30
 *
31
 * @experimental
32
 *
33
 * @author Baptiste Meyer <[email protected]>
34
 */
35
final class ItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
36
{
37
    private $client;
38
    private $documentMetadataFactory;
39
    private $identifierExtractor;
40
    private $denormalizer;
41
42
    public function __construct(Client $client, DocumentMetadataFactoryInterface $documentMetadataFactory, IdentifierExtractorInterface $identifierExtractor, DenormalizerInterface $denormalizer)
43
    {
44
        $this->client = $client;
45
        $this->documentMetadataFactory = $documentMetadataFactory;
46
        $this->identifierExtractor = $identifierExtractor;
47
        $this->denormalizer = $denormalizer;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function supports(string $resourceClass, ?string $operationName = null, array $context = []): bool
54
    {
55
        try {
56
            $this->documentMetadataFactory->create($resourceClass);
57
        } catch (IndexNotFoundException $e) {
58
            return false;
59
        }
60
61
        try {
62
            $this->identifierExtractor->getIdentifierFromResourceClass($resourceClass);
63
        } catch (NonUniqueIdentifierException $e) {
64
            return false;
65
        }
66
67
        return true;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getItem(string $resourceClass, $id, ?string $operationName = null, array $context = [])
74
    {
75
        if (\is_array($id)) {
76
            $id = $id[$this->identifierExtractor->getIdentifierFromResourceClass($resourceClass)];
77
        }
78
79
        $documentMetadata = $this->documentMetadataFactory->create($resourceClass);
80
81
        try {
82
            $document = $this->client->get([
83
                'index' => $documentMetadata->getIndex(),
84
                'type' => $documentMetadata->getType(),
85
                'id' => (string) $id,
86
            ]);
87
        } catch (Missing404Exception $e) {
88
            return null;
89
        }
90
91
        return $this->denormalizer->denormalize($document, $resourceClass, ItemNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true]);
92
    }
93
}
94