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

api-platform / core / 9562658349

18 Jun 2024 09:35AM UTC coverage: 62.637% (+0.4%) from 62.272%
9562658349

push

github

soyuka
Merge 3.4

52 of 55 new or added lines in 6 files covered. (94.55%)

236 existing lines in 20 files now uncovered.

11016 of 17587 relevant lines covered (62.64%)

60.45 hits per line

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

85.71
/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\Serializer\SerializerContextBuilderInterface as LegacySerializerContextBuilderInterface;
21
use ApiPlatform\State\Exception\ProviderNotFoundException;
22
use ApiPlatform\State\ProviderInterface;
23
use ApiPlatform\State\SerializerContextBuilderInterface;
24
use ApiPlatform\State\UriVariablesResolverTrait;
25
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
26
use ApiPlatform\State\Util\RequestParser;
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 LegacySerializerContextBuilderInterface|SerializerContextBuilderInterface|null $serializerContextBuilder = null,
43
    ) {
44
    }
304✔
45

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

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

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

62
        if ($filters) {
292✔
UNCOV
63
            $context['filters'] = $filters;
×
64
        }
65

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

75
        try {
76
            $data = $this->provider->provide($operation, $uriVariables, $context);
292✔
77
        } catch (ProviderNotFoundException $e) {
12✔
UNCOV
78
            $data = null;
×
79
        }
80

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

91
        $request?->attributes->set('data', $data);
288✔
92
        $request?->attributes->set('previous_data', $this->clone($data));
288✔
93

94
        return $data;
288✔
95
    }
96
}
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