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

api-platform / core / 10508505187

22 Aug 2024 12:55PM UTC coverage: 7.704%. Remained the same
10508505187

push

github

soyuka
Merge 3.4

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

8840 existing lines in 284 files now uncovered.

12477 of 161949 relevant lines covered (7.7%)

22.99 hits per line

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

96.43
/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 Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27

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

39
    public function __construct(
40
        private readonly ProviderInterface $provider,
41
        private readonly ?SerializerContextBuilderInterface $serializerContextBuilder = null,
42
    ) {
UNCOV
43
    }
1,847✔
44

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

UNCOV
51
        $request = ($context['request'] ?? null);
1,838✔
UNCOV
52
        if (!$operation->canRead()) {
1,838✔
UNCOV
53
            return null;
467✔
54
        }
55

UNCOV
56
        if (null === ($filters = $request?->attributes->get('_api_filters')) && $request) {
1,474✔
UNCOV
57
            $queryString = RequestParser::getQueryString($request);
1,441✔
UNCOV
58
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
1,441✔
59
        }
60

UNCOV
61
        if ($filters) {
1,474✔
UNCOV
62
            $context['filters'] = $filters;
540✔
63
        }
64

UNCOV
65
        if ($this->serializerContextBuilder && $request) {
1,474✔
66
            // Builtin data providers are able to use the serialization context to automatically add join clauses
UNCOV
67
            $context += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, [
1,474✔
UNCOV
68
                'resource_class' => $operation->getClass(),
1,474✔
UNCOV
69
                'operation' => $operation,
1,474✔
UNCOV
70
            ]);
1,474✔
UNCOV
71
            $request->attributes->set('_api_normalization_context', $normalizationContext);
1,474✔
72
        }
73

74
        try {
UNCOV
75
            $data = $this->provider->provide($operation, $uriVariables, $context);
1,474✔
UNCOV
76
        } catch (ProviderNotFoundException $e) {
23✔
77
            $data = null;
4✔
78
        }
79

80
        if (
UNCOV
81
            null === $data
1,474✔
UNCOV
82
            && 'POST' !== $operation->getMethod()
1,474✔
UNCOV
83
            && ('PUT' !== $operation->getMethod()
1,474✔
UNCOV
84
                || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
1,474✔
85
            )
86
        ) {
UNCOV
87
            throw new NotFoundHttpException('Not Found');
18✔
88
        }
89

UNCOV
90
        $request?->attributes->set('data', $data);
1,471✔
UNCOV
91
        $request?->attributes->set('previous_data', $this->clone($data));
1,471✔
92

UNCOV
93
        return $data;
1,471✔
94
    }
95
}
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