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

api-platform / core / 3712739783

pending completion
3712739783

Pull #5254

github

GitHub
Merge 9dfa88fa6 into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

199 of 199 new or added lines in 6 files covered. (100.0%)

7494 of 12363 relevant lines covered (60.62%)

67.55 hits per line

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

96.3
/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;
17
use ApiPlatform\Exception\InvalidIdentifierException;
18
use ApiPlatform\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\State\UriVariablesResolverTrait;
23
use ApiPlatform\Util\CloneTrait;
24
use ApiPlatform\Util\OperationRequestInitiatorTrait;
25
use ApiPlatform\Util\RequestAttributesExtractor;
26
use ApiPlatform\Util\RequestParser;
27
use Symfony\Component\HttpKernel\Event\RequestEvent;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29

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

41
    public const OPERATION_ATTRIBUTE_KEY = 'read';
42

43
    public function __construct(private readonly ProviderInterface $provider, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, private readonly ?SerializerContextBuilderInterface $serializerContextBuilder = null, UriVariablesConverterInterface $uriVariablesConverter = null)
44
    {
45
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
644✔
46
        $this->uriVariablesConverter = $uriVariablesConverter;
644✔
47
    }
48

49
    /**
50
     * Calls the data provider and sets the data attribute.
51
     *
52
     * @throws NotFoundHttpException
53
     */
54
    public function onKernelRequest(RequestEvent $event): void
55
    {
56
        $request = $event->getRequest();
644✔
57
        $operation = $this->initializeOperation($request);
644✔
58

59
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request))) {
644✔
60
            return;
192✔
61
        }
62

63
        if (!$attributes['receive'] || !$operation || !($operation->canRead() ?? true) || (!$operation->getUriVariables() && !$request->isMethodSafe())) {
492✔
64
            return;
139✔
65
        }
66

67
        $context = ['operation' => $operation];
358✔
68

69
        if (null === $filters = $request->attributes->get('_api_filters')) {
358✔
70
            $queryString = RequestParser::getQueryString($request);
347✔
71
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
347✔
72
        }
73

74
        if ($filters) {
358✔
75
            $context['filters'] = $filters;
177✔
76
        }
77

78
        if ($this->serializerContextBuilder) {
358✔
79
            // Builtin data providers are able to use the serialization context to automatically add join clauses
80
            $context += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
358✔
81
            $request->attributes->set('_api_normalization_context', $normalizationContext);
358✔
82
        }
83

84
        $parameters = $request->attributes->all();
358✔
85
        $resourceClass = $operation->getClass() ?? $attributes['resource_class'];
358✔
86
        try {
87
            $uriVariables = $this->getOperationUriVariables($operation, $parameters, $resourceClass);
358✔
88
            $data = $this->provider->provide($operation, $uriVariables, $context);
358✔
89
        } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
5✔
90
            throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
91
        }
92

93
        if (null === $data) {
354✔
94
            throw new NotFoundHttpException('Not Found');
4✔
95
        }
96

97
        $request->attributes->set('data', $data);
350✔
98
        $request->attributes->set('previous_data', $this->clone($data));
350✔
99
    }
100
}
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