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

api-platform / core / 14249627505

03 Apr 2025 05:58PM UTC coverage: 7.308%. Remained the same
14249627505

push

github

soyuka
docs: changelog 4.0.22

12514 of 171232 relevant lines covered (7.31%)

12.07 hits per line

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

81.82
/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\Error;
17
use ApiPlatform\Metadata\Exception\InvalidIdentifierException;
18
use ApiPlatform\Metadata\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\UriVariablesConverterInterface;
22
use ApiPlatform\Metadata\Util\CloneTrait;
23
use ApiPlatform\State\ProviderInterface;
24
use ApiPlatform\State\UriVariablesResolverTrait;
25
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
26
use ApiPlatform\State\Util\RequestAttributesExtractor;
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
    /**
42
     * @param ProviderInterface<object> $provider
43
     */
44
    public function __construct(
45
        private readonly ProviderInterface $provider,
46
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
47
        ?UriVariablesConverterInterface $uriVariablesConverter = null,
48
    ) {
49
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
131✔
50
        $this->uriVariablesConverter = $uriVariablesConverter;
131✔
51
    }
52

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

62
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !$attributes['receive']) {
131✔
63
            return;
26✔
64
        }
65

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

71
        if (null === $operation->canRead()) {
108✔
72
            $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
107✔
73
        }
74

75
        $uriVariables = [];
108✔
76
        if (!$operation instanceof Error && $operation instanceof HttpOperation) {
108✔
77
            try {
78
                $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
107✔
79
            } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
80
                if ($operation->canRead()) {
×
81
                    throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
82
                }
83
            }
84
        }
85

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