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

api-platform / core / 10749392760

07 Sep 2024 06:39AM UTC coverage: 7.646%. Remained the same
10749392760

push

github

soyuka
test: remove hydra prefix from guides

0 of 21 new or added lines in 6 files covered. (0.0%)

6834 existing lines in 225 files now uncovered.

12538 of 163980 relevant lines covered (7.65%)

22.78 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
    ) {
UNCOV
49
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
116✔
UNCOV
50
        $this->uriVariablesConverter = $uriVariablesConverter;
116✔
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
    {
UNCOV
60
        $request = $event->getRequest();
116✔
61

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

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

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

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

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