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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

90.63
/src/State/Provider/ReadProvider.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\State\Provider;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\Metadata\Put;
19
use ApiPlatform\Metadata\Util\CloneTrait;
20
use ApiPlatform\State\Exception\ProviderNotFoundException;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\State\SerializerContextBuilderInterface;
23
use ApiPlatform\State\StopwatchAwareInterface;
24
use ApiPlatform\State\StopwatchAwareTrait;
25
use ApiPlatform\State\UriVariablesResolverTrait;
26
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
27
use ApiPlatform\State\Util\RequestParser;
28
use Psr\Log\LoggerInterface;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30

31
/**
32
 * Retrieves data from the applicable data provider, based on the current IRI, and sets it as a request parameter called data.
33
 *
34
 * @author Kévin Dunglas <dunglas@gmail.com>
35
 */
36
final class ReadProvider implements ProviderInterface, StopwatchAwareInterface
37
{
38
    use CloneTrait;
39
    use OperationRequestInitiatorTrait;
40
    use StopwatchAwareTrait;
41
    use UriVariablesResolverTrait;
42

43
    public function __construct(
44
        private readonly ProviderInterface $provider,
45
        private readonly ?SerializerContextBuilderInterface $serializerContextBuilder = null,
46
        private readonly ?LoggerInterface $logger = null,
47
    ) {
48
    }
631✔
49

50
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
51
    {
52
        if (!$operation instanceof HttpOperation) {
614✔
53
            return null;
×
54
        }
55

56
        $request = ($context['request'] ?? null);
614✔
57
        if (!$operation->canRead()) {
614✔
58
            return null;
24✔
59
        }
60

61
        $this->stopwatch?->start('api_platform.provider.read');
600✔
62

63
        if (null === ($filters = $request?->attributes->get('_api_filters')) && $request) {
600✔
64
            $queryString = RequestParser::getQueryString($request);
600✔
65
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
600✔
66
        }
67

68
        if ($filters) {
600✔
69
            $context['filters'] = $filters;
338✔
70
        }
71

72
        $resourceClass = $operation->getClass();
600✔
73

74
        if ($this->serializerContextBuilder && $request) {
600✔
75
            // Builtin data providers are able to use the serialization context to automatically add join clauses
76
            $context += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, [
600✔
77
                'resource_class' => $resourceClass,
600✔
78
                'operation' => $operation,
600✔
79
            ]);
600✔
80
            $request->attributes->set('_api_normalization_context', $normalizationContext);
600✔
81
        }
82

83
        try {
84
            $data = $this->provider->provide($operation, $uriVariables, $context);
600✔
85
        } catch (ProviderNotFoundException $e) {
6✔
86
            // In case the dev just forgot to implement it
87
            $this->logger?->debug('No provider registered for {resource_class}', ['resource_class' => $resourceClass]);
×
88
            $data = null;
×
89
        }
90

91
        if (
92
            null === $data
598✔
93
            && 'POST' !== $operation->getMethod()
598✔
94
            && ('PUT' !== $operation->getMethod()
598✔
95
                || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
598✔
96
            )
97
        ) {
98
            throw new NotFoundHttpException('Not Found', $e ?? null);
2✔
99
        }
100

101
        $request?->attributes->set('data', $data);
598✔
102
        $request?->attributes->set('previous_data', $this->clone($data));
598✔
103

104
        $this->stopwatch?->stop('api_platform.provider.read');
598✔
105

106
        return $data;
598✔
107
    }
108
}
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

© 2025 Coveralls, Inc