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

api-platform / core / 15973247443

30 Jun 2025 12:44PM UTC coverage: 22.065% (+0.001%) from 22.064%
15973247443

push

github

web-flow
chore: use treat phpdoc type as certain (#7250)

8 of 16 new or added lines in 13 files covered. (50.0%)

123 existing lines in 11 files now uncovered.

11518 of 52201 relevant lines covered (22.06%)

22.04 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\InflectorInterface;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Util\Inflector;
20
use ApiPlatform\State\ApiResource\Error;
21
use ApiPlatform\State\ProviderInterface;
22
use Elastic\Elasticsearch\Client;
23
use Elastic\Elasticsearch\Exception\ClientResponseException;
24
use Elastic\Elasticsearch\Response\Elasticsearch;
25
use Elasticsearch\Client as V7Client;
26
use Elasticsearch\Common\Exceptions\Missing404Exception as V7Missing404Exception;
27
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
28
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
29

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

45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
49
    {
50
        $resourceClass = $operation->getClass();
×
NEW
51
        $options = $operation->getStateOptions();
×
52
        if (!$options instanceof Options) {
×
NEW
53
            $options = new Options(index: $this->getIndex($operation));
×
54
        }
55

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

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

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

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

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

83
        return $item;
×
84
    }
85

86
    private function getIndex(Operation $operation): string
87
    {
88
        return $this->inflector->tableize($operation->getShortName());
×
89
    }
90
}
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