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

api-platform / core / 6978770879

24 Nov 2023 09:02AM UTC coverage: 37.284% (-0.1%) from 37.409%
6978770879

push

github

soyuka
Merge 3.2

79 of 149 new or added lines in 21 files covered. (53.02%)

16 existing lines in 8 files now uncovered.

10287 of 27591 relevant lines covered (37.28%)

20.53 hits per line

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

84.85
/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\Api\UriVariablesConverterInterface;
17
use ApiPlatform\Exception\InvalidIdentifierException;
18
use ApiPlatform\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\Error;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\State\ProcessorInterface;
23
use ApiPlatform\State\ProviderInterface;
24
use ApiPlatform\State\UriVariablesResolverTrait;
25
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
26
use Psr\Log\LoggerInterface;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30

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

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

47
    public function __invoke(Request $request): Response
48
    {
49
        $operation = $this->initializeOperation($request);
30✔
50
        $uriVariables = [];
30✔
51

52
        if (!$operation instanceof Error) {
30✔
53
            try {
54
                $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
30✔
NEW
55
            } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
NEW
56
                throw new NotFoundHttpException('Invalid uri variables.', $e);
×
57
            }
58
        }
59

60
        $context = [
30✔
61
            'request' => $request,
30✔
62
            'uri_variables' => $uriVariables,
30✔
63
            'resource_class' => $operation->getClass(),
30✔
64
        ];
30✔
65

66
        if (null === $operation->canValidate()) {
30✔
67
            $operation = $operation->withValidate(!$request->isMethodSafe() && !$request->isMethod('DELETE'));
30✔
68
        }
69

70
        if (null === $operation->canRead() && $operation instanceof HttpOperation) {
30✔
71
            $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
28✔
72
        }
73

74
        if (null === $operation->canDeserialize() && $operation instanceof HttpOperation) {
30✔
75
            $operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true));
30✔
76
        }
77

78
        $body = $this->provider->provide($operation, $uriVariables, $context);
30✔
79

80
        // The provider can change the Operation, extract it again from the Request attributes
81
        if ($request->attributes->get('_api_operation') !== $operation) {
28✔
82
            $operation = $this->initializeOperation($request);
28✔
83
            try {
84
                $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
28✔
85
            } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
86
                // if this occurs with our base operation we throw above so log instead of throw here
87
                if ($this->logger) {
×
88
                    $this->logger->error($e->getMessage(), ['operation' => $operation]);
×
89
                }
90
            }
91
        }
92

93
        $context['previous_data'] = $request->attributes->get('previous_data');
28✔
94
        $context['data'] = $request->attributes->get('data');
28✔
95

96
        if (null === $operation->canWrite()) {
28✔
97
            $operation = $operation->withWrite(!$request->isMethodSafe());
28✔
98
        }
99

100
        if (null === $operation->canSerialize()) {
28✔
101
            $operation = $operation->withSerialize(true);
28✔
102
        }
103

104
        return $this->processor->process($body, $operation, $uriVariables, $context);
28✔
105
    }
106
}
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