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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 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
    ) {
UNCOV
45
    }
799✔
46

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

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

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

UNCOV
63
        if ($filters) {
677✔
UNCOV
64
            $context['filters'] = $filters;
301✔
65
        }
66

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

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

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

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

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

UNCOV
99
        return $data;
675✔
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