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

api-platform / core / 6173831445

13 Sep 2023 02:23PM UTC coverage: 36.999% (-0.1%) from 37.094%
6173831445

push

github

soyuka
chore: subtree component descriptions

10101 of 27301 relevant lines covered (37.0%)

19.98 hits per line

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

81.48
/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;
21
use ApiPlatform\State\Exception\ProviderNotFoundException;
22
use ApiPlatform\State\ProviderInterface;
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
    ) {
43
    }
58✔
44

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

51
        $request = ($context['request'] ?? null);
58✔
52
        if (!($operation->canRead() ?? true) || (!$operation->getUriVariables() && !$request?->isMethodSafe())) {
58✔
53
            return null;
6✔
54
        }
55

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

61
        if ($filters) {
52✔
62
            $context['filters'] = $filters;
×
63
        }
64

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

73
        try {
74
            $data = $this->provider->provide($operation, $uriVariables, $context);
52✔
75
        } catch (ProviderNotFoundException $e) {
×
76
            $data = null;
×
77
        }
78

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

89
        $request?->attributes->set('data', $data);
52✔
90
        $request?->attributes->set('previous_data', $this->clone($data));
52✔
91

92
        return $data;
52✔
93
    }
94
}
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