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

api-platform / core / 8100440090

29 Feb 2024 05:54PM UTC coverage: 57.09% (+0.2%) from 56.922%
8100440090

Pull #6138

github

web-flow
chore(deps): bump codecov/codecov-action from 3 to 4

Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 3 to 4.
- [Release notes](https://github.com/codecov/codecov-action/releases)
- [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/codecov/codecov-action/compare/v3...v4)

---
updated-dependencies:
- dependency-name: codecov/codecov-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #6138: chore(deps): bump codecov/codecov-action from 3 to 4

9538 of 16707 relevant lines covered (57.09%)

30.81 hits per line

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

25.49
/src/Symfony/EventListener/ReadListener.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\Symfony\EventListener;
15

16
use ApiPlatform\Api\UriVariablesConverterInterface as LegacyUriVariablesConverterInterface;
17
use ApiPlatform\Exception\InvalidIdentifierException;
18
use ApiPlatform\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\Error;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\Put;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\UriVariablesConverterInterface;
24
use ApiPlatform\Metadata\Util\CloneTrait;
25
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
26
use ApiPlatform\State\CallableProvider;
27
use ApiPlatform\State\Exception\ProviderNotFoundException;
28
use ApiPlatform\State\Provider\ReadProvider;
29
use ApiPlatform\State\ProviderInterface;
30
use ApiPlatform\State\UriVariablesResolverTrait;
31
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
32
use ApiPlatform\State\Util\RequestParser;
33
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
34
use Symfony\Component\HttpKernel\Event\RequestEvent;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36

37
/**
38
 * Retrieves data from the applicable data provider and sets it as a request parameter called data.
39
 *
40
 * @author Kévin Dunglas <dunglas@gmail.com>
41
 */
42
final class ReadListener
43
{
44
    use CloneTrait;
45
    use OperationRequestInitiatorTrait;
46
    use UriVariablesResolverTrait;
47

48
    public function __construct(
49
        private readonly ProviderInterface $provider,
50
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
51
        private readonly ?SerializerContextBuilderInterface $serializerContextBuilder = null,
52
        LegacyUriVariablesConverterInterface|UriVariablesConverterInterface|null $uriVariablesConverter = null,
53
    ) {
54
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
37✔
55
        $this->uriVariablesConverter = $uriVariablesConverter;
37✔
56

57
        if ($provider instanceof CallableProvider) {
37✔
58
            trigger_deprecation('api-platform/core', '3.3', 'Use a "%s" as first argument in "%s" instead of "%s".', ReadProvider::class, self::class, $provider::class);
31✔
59
        }
60
    }
61

62
    /**
63
     * Calls the data provider and sets the data attribute.
64
     *
65
     * @throws NotFoundHttpException
66
     */
67
    public function onKernelRequest(RequestEvent $event): void
68
    {
69
        $request = $event->getRequest();
37✔
70

71
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !$attributes['receive']) {
37✔
72
            return;
19✔
73
        }
74

75
        $operation = $this->initializeOperation($request);
21✔
76

77
        if ($operation && !$this->provider instanceof CallableProvider) {
21✔
78
            if (null === $operation->canRead()) {
×
79
                $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
×
80
            }
81

82
            $uriVariables = [];
×
83
            if (!$operation instanceof Error && $operation instanceof HttpOperation) {
×
84
                try {
85
                    $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
×
86
                } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
87
                    throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
88
                }
89
            }
90

91
            $request->attributes->set('_api_uri_variables', $uriVariables);
×
92
            $this->provider->provide($operation, $uriVariables, [
×
93
                'request' => $request,
×
94
                'uri_variables' => $uriVariables,
×
95
                'resource_class' => $operation->getClass(),
×
96
            ]);
×
97

98
            return;
×
99
        }
100

101
        if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
21✔
102
            return;
18✔
103
        }
104

105
        if (!$operation || !($operation->canRead() ?? true) || (!$operation->getUriVariables() && !$request->isMethodSafe())) {
3✔
106
            return;
3✔
107
        }
108

109
        $context = ['operation' => $operation];
×
110

111
        if (null === $filters = $request->attributes->get('_api_filters')) {
×
112
            $queryString = RequestParser::getQueryString($request);
×
113
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
×
114
        }
115

116
        if ($filters) {
×
117
            $context['filters'] = $filters;
×
118
        }
119

120
        if ($this->serializerContextBuilder) {
×
121
            // Builtin data providers are able to use the serialization context to automatically add join clauses
122
            $context += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
×
123
            $request->attributes->set('_api_normalization_context', $normalizationContext);
×
124
        }
125

126
        $parameters = $request->attributes->all();
×
127
        $resourceClass = $operation->getClass() ?? $attributes['resource_class'];
×
128
        try {
129
            $uriVariables = $this->getOperationUriVariables($operation, $parameters, $resourceClass);
×
130
            $data = $this->provider->provide($operation, $uriVariables, $context);
×
131
        } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
132
            throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
133
        } catch (ProviderNotFoundException $e) {
×
134
            $data = null;
×
135
        }
136

137
        if (
138
            null === $data
×
139
            && 'POST' !== $operation->getMethod()
×
140
            && (
141
                'PUT' !== $operation->getMethod()
×
142
                || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
×
143
            )
144
        ) {
145
            throw new NotFoundHttpException('Not Found');
×
146
        }
147

148
        $request->attributes->set('data', $data);
×
149
        $request->attributes->set('previous_data', $this->clone($data));
×
150
    }
151
}
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