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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

93.18
/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;
239✔
45
        $this->uriVariablesConverter = $uriVariablesConverter;
239✔
46
    }
47

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

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

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

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

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

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

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

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

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

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

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

100
            if (!$request->attributes->has('exception')) {
133✔
101
                try {
102
                    $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
129✔
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');
234✔
113
        $context['data'] = $request->attributes->get('data');
234✔
114

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

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

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