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

api-platform / core / 16530614392

25 Jul 2025 08:04PM UTC coverage: 22.07%. Remained the same
16530614392

push

github

web-flow
fix(symfony): explicitly set the target class when mapping entities to resources (#7311)

3 of 3 new or added lines in 2 files covered. (100.0%)

125 existing lines in 11 files now uncovered.

11573 of 52437 relevant lines covered (22.07%)

23.54 hits per line

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

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

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

39
    /**
40
     * @param ProviderInterface<object> $provider
41
     */
42
    public function __construct(
43
        private readonly ProviderInterface $provider,
44
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
45
        ?UriVariablesConverterInterface $uriVariablesConverter = null,
46
        private readonly ?ProviderInterface $parameterProvider = null,
47
    ) {
UNCOV
48
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
294✔
UNCOV
49
        $this->uriVariablesConverter = $uriVariablesConverter;
294✔
50
    }
51

52
    /**
53
     * Calls the data provider and sets the data attribute.
54
     *
55
     * @throws NotFoundHttpException
56
     */
57
    public function onKernelRequest(RequestEvent $event): void
58
    {
UNCOV
59
        $request = $event->getRequest();
294✔
60

UNCOV
61
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !$attributes['receive']) {
294✔
UNCOV
62
            return;
31✔
63
        }
64

UNCOV
65
        $operation = $this->initializeOperation($request);
266✔
UNCOV
66
        if (!$operation) {
266✔
67
            return;
×
68
        }
69

UNCOV
70
        if (null === $operation->canRead()) {
266✔
UNCOV
71
            $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
265✔
72
        }
73

UNCOV
74
        $uriVariables = [];
266✔
UNCOV
75
        if (!$request->attributes->has('exception')) {
266✔
76
            try {
UNCOV
77
                $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
262✔
UNCOV
78
            } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
1✔
UNCOV
79
                if ($operation->canRead()) {
1✔
UNCOV
80
                    throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
1✔
81
                }
82
            }
83
        }
84

UNCOV
85
        $request->attributes->set('_api_uri_variables', $uriVariables);
266✔
UNCOV
86
        $context = [
266✔
UNCOV
87
            'request' => $request,
266✔
UNCOV
88
            'uri_variables' => $uriVariables,
266✔
UNCOV
89
            'resource_class' => $operation->getClass(),
266✔
UNCOV
90
        ];
266✔
UNCOV
91
        $this->parameterProvider?->provide($operation, $uriVariables, $context);
266✔
UNCOV
92
        $this->provider->provide($operation, $uriVariables, $context);
266✔
93
    }
94
}
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