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

api-platform / core / 15256141101

26 May 2025 02:16PM UTC coverage: 21.714% (-4.8%) from 26.526%
15256141101

push

github

web-flow
Merge 4.1

6 of 387 new or added lines in 28 files covered. (1.55%)

1891 existing lines in 117 files now uncovered.

11118 of 51201 relevant lines covered (21.71%)

29.33 hits per line

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

90.91
/src/Symfony/Controller/MainController.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\Controller;
15

16
use ApiPlatform\Metadata\Exception\InvalidIdentifierException;
17
use ApiPlatform\Metadata\Exception\InvalidUriVariableException;
18
use ApiPlatform\Metadata\Exception\RuntimeException;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\UriVariablesConverterInterface;
22
use ApiPlatform\State\ProcessorInterface;
23
use ApiPlatform\State\ProviderInterface;
24
use ApiPlatform\State\SerializerContextBuilderInterface;
25
use ApiPlatform\State\UriVariablesResolverTrait;
26
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
27
use Psr\Log\LoggerInterface;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
31

32
final class MainController
33
{
34
    use OperationRequestInitiatorTrait;
35
    use UriVariablesResolverTrait;
36

37
    public function __construct(
38
        ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
39
        private readonly ProviderInterface $provider,
40
        private readonly ProcessorInterface $processor,
41
        ?UriVariablesConverterInterface $uriVariablesConverter = null,
42
        private readonly ?LoggerInterface $logger = null,
43
    ) {
44
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
682✔
45
        $this->uriVariablesConverter = $uriVariablesConverter;
682✔
46
    }
47

48
    public function __invoke(Request $request): Response
49
    {
50
        $operation = $this->initializeOperation($request);
682✔
51

52
        if (!$operation || !$operation instanceof HttpOperation) {
682✔
UNCOV
53
            throw new RuntimeException('Not an HTTP API operation.');
×
54
        }
55

56
        $uriVariables = [];
682✔
57
        if (!$request->attributes->has('exception')) {
682✔
58
            try {
59
                $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
674✔
60
                $request->attributes->set('_api_uri_variables', $uriVariables);
672✔
61
            } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
2✔
62
                throw new NotFoundHttpException('Invalid uri variables.', $e);
2✔
63
            }
64
        }
65

66
        $context = [
682✔
67
            'request' => $request,
682✔
68
            'uri_variables' => $uriVariables,
682✔
69
            'resource_class' => $operation->getClass(),
682✔
70
        ];
682✔
71

72
        if (null === $operation->canValidate()) {
682✔
73
            $operation = $operation->withValidate(!$request->isMethodSafe() && !$request->isMethod('DELETE'));
682✔
74
        }
75

76
        if (null === $operation->canRead()) {
682✔
77
            $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
681✔
78
        }
79

80
        if (null === $operation->canDeserialize()) {
682✔
81
            $operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true));
682✔
82
        }
83

84
        $denormalizationContext = $operation->getDenormalizationContext() ?? [];
682✔
85
        if ($operation->canDeserialize() && !isset($denormalizationContext[SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE])) {
682✔
86
            $method = $operation->getMethod();
241✔
87
            $assignObjectToPopulate = 'POST' === $method
241✔
88
                || 'PATCH' === $method
241✔
89
                || ('PUT' === $method && !($operation->getExtraProperties()['standard_put'] ?? true));
241✔
90

91
            $operation = $operation->withDenormalizationContext($denormalizationContext + [SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE => $assignObjectToPopulate]);
241✔
92
        }
93

94
        $body = $this->provider->provide($operation, $uriVariables, $context);
682✔
95

96
        // The provider can change the Operation, extract it again from the Request attributes
97
        if ($request->attributes->get('_api_operation') !== $operation) {
678✔
98
            $operation = $this->initializeOperation($request);
233✔
99

100
            if (!$request->attributes->has('exception')) {
233✔
101
                try {
102
                    $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
233✔
103
                } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
104
                    // if this occurs with our base operation we throw above so log instead of throw here
105
                    if ($this->logger) {
×
106
                        $this->logger->error($e->getMessage(), ['operation' => $operation]);
×
107
                    }
108
                }
109
            }
110
        }
111

112
        $context['previous_data'] = $request->attributes->get('previous_data');
678✔
113
        $context['data'] = $request->attributes->get('data');
678✔
114

115
        if (null === $operation->canWrite()) {
678✔
116
            $operation = $operation->withWrite(!$request->isMethodSafe());
676✔
117
        }
118

119
        if (null === $operation->canSerialize()) {
678✔
120
            $operation = $operation->withSerialize(true);
678✔
121
        }
122

123
        return $this->processor->process($body, $operation, $uriVariables, $context);
678✔
124
    }
125
}
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