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

api-platform / core / 13724522058

07 Mar 2025 04:04PM UTC coverage: 8.175% (-0.3%) from 8.518%
13724522058

Pull #7005

github

web-flow
Merge 322407532 into 1e0bc9dc8
Pull Request #7005: fix(validation): deprecate string message for ValidationException con…

4 of 6 new or added lines in 1 file covered. (66.67%)

159 existing lines in 24 files now uncovered.

12839 of 157045 relevant lines covered (8.18%)

13.55 hits per line

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

90.0
/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\UriVariablesResolverTrait;
24
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
25
use ApiPlatform\State\Util\RequestParser;
26
use Psr\Log\LoggerInterface;
27
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28

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

40
    public function __construct(
41
        private readonly ProviderInterface $provider,
42
        private readonly ?SerializerContextBuilderInterface $serializerContextBuilder = null,
43
        private readonly ?LoggerInterface $logger = null,
44
    ) {
45
    }
996✔
46

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

53
        $request = ($context['request'] ?? null);
980✔
54
        if (!$operation->canRead()) {
980✔
55
            return null;
151✔
56
        }
57

58
        if (null === ($filters = $request?->attributes->get('_api_filters')) && $request) {
861✔
59
            $queryString = RequestParser::getQueryString($request);
850✔
60
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
850✔
61
        }
62

63
        if ($filters) {
861✔
64
            $context['filters'] = $filters;
406✔
65
        }
66

67
        $resourceClass = $operation->getClass();
861✔
68

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

78
        try {
79
            $data = $this->provider->provide($operation, $uriVariables, $context);
861✔
80
        } catch (ProviderNotFoundException $e) {
11✔
81
            // In case the dev just forgot to implement it
UNCOV
82
            $this->logger?->debug('No provider registered for {resource_class}', ['resource_class' => $resourceClass]);
×
UNCOV
83
            $data = null;
×
84
        }
85

86
        if (
87
            null === $data
859✔
88
            && 'POST' !== $operation->getMethod()
859✔
89
            && ('PUT' !== $operation->getMethod()
859✔
90
                || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
859✔
91
            )
92
        ) {
93
            throw new NotFoundHttpException('Not Found', $e ?? null);
7✔
94
        }
95

96
        $request?->attributes->set('data', $data);
858✔
97
        $request?->attributes->set('previous_data', $this->clone($data));
858✔
98

99
        return $data;
858✔
100
    }
101
}
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