• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Elasticsearch/State/ItemProvider.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Elasticsearch\State;
15

16
use ApiPlatform\Elasticsearch\Serializer\DocumentNormalizer;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\InflectorInterface;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Util\Inflector;
21
use ApiPlatform\State\ApiResource\Error;
22
use ApiPlatform\State\ProviderInterface;
23
use Elastic\Elasticsearch\Client;
24
use Elastic\Elasticsearch\Exception\ClientResponseException;
25
use Elastic\Elasticsearch\Response\Elasticsearch;
26
use Elasticsearch\Client as V7Client;
27
use Elasticsearch\Common\Exceptions\Missing404Exception as V7Missing404Exception;
28
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
29
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
30

31
/**
32
 * Item provider for Elasticsearch.
33
 *
34
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
35
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
36
 */
37
final class ItemProvider implements ProviderInterface
38
{
39
    public function __construct(
40
        private readonly V7Client|Client $client, // @phpstan-ignore-line
41
        private readonly ?DenormalizerInterface $denormalizer = null,
42
        private readonly ?InflectorInterface $inflector = new Inflector(),
43
    ) {
UNCOV
44
    }
×
45

46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
50
    {
51
        $resourceClass = $operation->getClass();
×
52
        $options = $operation->getStateOptions() instanceof Options ? $operation->getStateOptions() : new Options(index: $this->getIndex($operation));
×
53
        if (!$options instanceof Options) {
×
54
            throw new RuntimeException(\sprintf('The "%s" provider was called without "%s".', self::class, Options::class));
×
55
        }
56

57
        $params = [
×
58
            'index' => $options->getIndex() ?? $this->getIndex($operation),
×
59
            'id' => (string) reset($uriVariables),
×
60
        ];
×
61

62
        try {
NEW
63
            $document = $this->client->get($params); // @phpstan-ignore-line
×
NEW
64
        } catch (V7Missing404Exception) { // @phpstan-ignore-line
×
NEW
65
            return null;
×
66
        } catch (ClientResponseException $e) {
×
67
            $response = $e->getResponse();
×
68
            if (404 === $response->getStatusCode()) {
×
69
                return null;
×
70
            }
71

72
            throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
×
73
        }
74

NEW
75
        if (class_exists(Elasticsearch::class) && $document instanceof Elasticsearch) {
×
76
            $document = $document->asArray();
×
77
        }
78

79
        $item = $this->denormalizer->denormalize($document, $resourceClass, DocumentNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true]);
×
80
        if (!\is_object($item) && null !== $item) {
×
81
            throw new \UnexpectedValueException('Expected item to be an object or null.');
×
82
        }
83

84
        return $item;
×
85
    }
86

87
    private function getIndex(Operation $operation): string
88
    {
89
        return $this->inflector->tableize($operation->getShortName());
×
90
    }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc