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

api-platform / core / 10490215375

21 Aug 2024 12:48PM UTC coverage: 7.704%. Remained the same
10490215375

push

github

web-flow
docs: cleanup and minor marketing improvements (#6525)

* fix: ignore more files in the Laravel package archive

* chore: various minor docs improvements

12476 of 161932 relevant lines covered (7.7%)

23.0 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
    ) {
43
    }
1,847✔
44

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

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

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

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

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

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

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

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

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