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

api-platform / core / 7047458607

30 Nov 2023 01:53PM UTC coverage: 37.263% (+0.002%) from 37.261%
7047458607

push

github

soyuka
Merge 3.2

20 of 35 new or added lines in 12 files covered. (57.14%)

10 existing lines in 3 files now uncovered.

10295 of 27628 relevant lines covered (37.26%)

21.04 hits per line

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

91.67
/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\Exception\RuntimeException;
21
use ApiPlatform\Metadata\HttpOperation;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\State\ProcessorInterface;
24
use ApiPlatform\State\ProviderInterface;
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;
48✔
45
        $this->uriVariablesConverter = $uriVariablesConverter;
48✔
46
    }
47

48
    public function __invoke(Request $request): Response
49
    {
50
        $operation = $this->initializeOperation($request);
48✔
51
        if (!$operation) {
48✔
52
            throw new RuntimeException('Not an API operation.');
3✔
53
        }
54

55
        $uriVariables = [];
45✔
56
        if (!$operation instanceof Error) {
45✔
57
            try {
58
                $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
42✔
59
            } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
3✔
60
                throw new NotFoundHttpException('Invalid uri variables.', $e);
3✔
61
            }
62
        }
63

64
        $context = [
42✔
65
            'request' => $request,
42✔
66
            'uri_variables' => $uriVariables,
42✔
67
            'resource_class' => $operation->getClass(),
42✔
68
        ];
42✔
69

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

74
        if (null === $operation->canRead() && $operation instanceof HttpOperation) {
42✔
75
            $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
40✔
76
        }
77

78
        if (null === $operation->canDeserialize() && $operation instanceof HttpOperation) {
42✔
79
            $operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true));
42✔
80
        }
81

82
        $body = $this->provider->provide($operation, $uriVariables, $context);
42✔
83

84
        // The provider can change the Operation, extract it again from the Request attributes
85
        if ($request->attributes->get('_api_operation') !== $operation) {
40✔
86
            $operation = $this->initializeOperation($request);
40✔
87

88
            if (!$operation instanceof Error) {
40✔
89
                try {
90
                    $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
34✔
NEW
91
                } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
92
                    // if this occurs with our base operation we throw above so log instead of throw here
NEW
93
                    if ($this->logger) {
×
NEW
94
                        $this->logger->error($e->getMessage(), ['operation' => $operation]);
×
95
                    }
96
                }
97
            }
98
        }
99

100
        $context['previous_data'] = $request->attributes->get('previous_data');
40✔
101
        $context['data'] = $request->attributes->get('data');
40✔
102

103
        if (null === $operation->canWrite()) {
40✔
104
            $operation = $operation->withWrite(!$request->isMethodSafe());
40✔
105
        }
106

107
        if (null === $operation->canSerialize()) {
40✔
108
            $operation = $operation->withSerialize(true);
40✔
109
        }
110

111
        return $this->processor->process($body, $operation, $uriVariables, $context);
40✔
112
    }
113
}
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