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

api-platform / core / 8540407641

03 Apr 2024 02:23PM UTC coverage: 66.768%. Remained the same
8540407641

push

github

soyuka
fix(graphql): increment graphql normalizer priority

closes #6279 #6264

16399 of 24561 relevant lines covered (66.77%)

38.63 hits per line

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

88.24
/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\Put;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\UriVariablesConverterInterface;
22
use ApiPlatform\Metadata\Util\CloneTrait;
23
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
24
use ApiPlatform\State\Exception\ProviderNotFoundException;
25
use ApiPlatform\State\ProviderInterface;
26
use ApiPlatform\State\UriVariablesResolverTrait;
27
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
28
use ApiPlatform\State\Util\RequestParser;
29
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
30
use Symfony\Component\HttpKernel\Event\RequestEvent;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32

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

44
    public function __construct(
45
        private readonly ProviderInterface $provider,
46
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
47
        private readonly ?SerializerContextBuilderInterface $serializerContextBuilder = null,
48
        LegacyUriVariablesConverterInterface|UriVariablesConverterInterface|null $uriVariablesConverter = null,
49
    ) {
50
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
156✔
51
        $this->uriVariablesConverter = $uriVariablesConverter;
156✔
52
    }
53

54
    /**
55
     * Calls the data provider and sets the data attribute.
56
     *
57
     * @throws NotFoundHttpException
58
     */
59
    public function onKernelRequest(RequestEvent $event): void
60
    {
61
        $request = $event->getRequest();
156✔
62
        $operation = $this->initializeOperation($request);
156✔
63

64
        if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
156✔
65
            return;
79✔
66
        }
67

68
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request))) {
83✔
69
            return;
53✔
70
        }
71

72
        if (!$attributes['receive'] || !$operation || !($operation->canRead() ?? true) || (!$operation->getUriVariables() && !$request->isMethodSafe())) {
33✔
73
            return;
11✔
74
        }
75

76
        $context = ['operation' => $operation];
22✔
77

78
        if (null === $filters = $request->attributes->get('_api_filters')) {
22✔
79
            $queryString = RequestParser::getQueryString($request);
22✔
80
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
22✔
81
        }
82

83
        if ($filters) {
22✔
84
            $context['filters'] = $filters;
×
85
        }
86

87
        if ($this->serializerContextBuilder) {
22✔
88
            // Builtin data providers are able to use the serialization context to automatically add join clauses
89
            $context += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
22✔
90
            $request->attributes->set('_api_normalization_context', $normalizationContext);
22✔
91
        }
92

93
        $parameters = $request->attributes->all();
22✔
94
        $resourceClass = $operation->getClass() ?? $attributes['resource_class'];
22✔
95
        try {
96
            $uriVariables = $this->getOperationUriVariables($operation, $parameters, $resourceClass);
22✔
97
            $data = $this->provider->provide($operation, $uriVariables, $context);
22✔
98
        } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
1✔
99
            throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
100
        } catch (ProviderNotFoundException $e) {
1✔
101
            $data = null;
×
102
        }
103

104
        if (
105
            null === $data
21✔
106
            && 'POST' !== $operation->getMethod()
21✔
107
            && (
108
                'PUT' !== $operation->getMethod()
21✔
109
                || ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
21✔
110
            )
111
        ) {
112
            throw new NotFoundHttpException('Not Found');
×
113
        }
114

115
        $request->attributes->set('data', $data);
21✔
116
        $request->attributes->set('previous_data', $this->clone($data));
21✔
117
    }
118
}
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