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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    ) {
UNCOV
44
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
×
UNCOV
45
        $this->uriVariablesConverter = $uriVariablesConverter;
×
46
    }
47

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

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

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

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

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

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

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

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

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

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

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

UNCOV
100
            if (!$request->attributes->has('exception')) {
×
101
                try {
UNCOV
102
                    $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
×
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

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

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

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

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