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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

85.19
/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
    }
85✔
44

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

51
        $request = ($context['request'] ?? null);
85✔
52

53
        if (!$operation->canRead()) {
85✔
54
            return null;
9✔
55
        }
56

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

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

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

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

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

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

93
        return $data;
73✔
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

© 2026 Coveralls, Inc