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

api-platform / core / 7582976395

19 Jan 2024 11:09AM UTC coverage: 61.988% (+0.03%) from 61.96%
7582976395

push

github

web-flow
feat(metadata): headers configuration (#6074)

27 of 40 new or added lines in 14 files covered. (67.5%)

46 existing lines in 8 files now uncovered.

17483 of 28204 relevant lines covered (61.99%)

32.43 hits per line

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

80.65
/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
    }
108✔
44

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

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

56
        $output = $operation->getOutput() ?? [];
96✔
57
        if (\array_key_exists('class', $output) && null === $output['class']) {
96✔
NEW
58
            $request?->attributes->set('data', null);
×
59

NEW
60
            return null;
×
61
        }
62

63
        if (null === $filters = $request?->attributes->get('_api_filters')) {
96✔
64
            $queryString = RequestParser::getQueryString($request);
96✔
65
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
96✔
66
        }
67

68
        if ($filters) {
96✔
69
            $context['filters'] = $filters;
×
70
        }
71

72
        if ($this->serializerContextBuilder && $request) {
96✔
73
            // Builtin data providers are able to use the serialization context to automatically add join clauses
74
            $context += $this->serializerContextBuilder->createFromRequest($request, true, [
96✔
75
                'resource_class' => $operation->getClass(),
96✔
76
                'operation' => $operation,
96✔
77
            ]);
96✔
78
        }
79

80
        try {
81
            $data = $this->provider->provide($operation, $uriVariables, $context);
96✔
82
        } catch (ProviderNotFoundException $e) {
4✔
83
            $data = null;
×
84
        }
85

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

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

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