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

api-platform / core / 18089937549

29 Sep 2025 07:56AM UTC coverage: 21.764% (-0.3%) from 22.093%
18089937549

Pull #7416

github

web-flow
Merge 061bcc790 into abe0438be
Pull Request #7416: fix(laravel): serializer attributes on Eloquent methods

0 of 151 new or added lines in 11 files covered. (0.0%)

5028 existing lines in 173 files now uncovered.

11889 of 54626 relevant lines covered (21.76%)

25.32 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
    ) {
UNCOV
43
    }
×
44

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

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

61
        try {
UNCOV
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

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

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

UNCOV
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

UNCOV
83
        return $item;
×
84
    }
85

86
    private function getIndex(Operation $operation): string
87
    {
UNCOV
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