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

api-platform / core / 17768546587

16 Sep 2025 02:07PM UTC coverage: 21.902% (-0.02%) from 21.92%
17768546587

push

github

web-flow
ci: laravel e2e installation (#7388)

11865 of 54174 relevant lines covered (21.9%)

12.86 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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