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

api-platform / core / 10943429050

19 Sep 2024 02:48PM UTC coverage: 7.647% (-0.03%) from 7.675%
10943429050

push

github

web-flow
feat: api-platform/json-hal component (#6621)

* feat: add hal support for laravel

* feat: quick review

* fix: typo & cs-fixer

* fix: typo in composer.json

* fix: cs-fixer & phpstan

* fix: forgot about hal item normalizer, therefore there's no more createbook nor updatebook test as Hal is a readonly format

0 of 94 new or added lines in 2 files covered. (0.0%)

9082 existing lines in 291 files now uncovered.

12629 of 165144 relevant lines covered (7.65%)

22.89 hits per line

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

91.89
/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\Error;
17
use ApiPlatform\Metadata\Exception\InvalidIdentifierException;
18
use ApiPlatform\Metadata\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\Exception\RuntimeException;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\Metadata\UriVariablesConverterInterface;
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
    ) {
UNCOV
44
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
1,988✔
UNCOV
45
        $this->uriVariablesConverter = $uriVariablesConverter;
1,988✔
46
    }
47

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

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

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

UNCOV
71
        if (null === $operation->canValidate()) {
1,982✔
UNCOV
72
            $operation = $operation->withValidate(!$request->isMethodSafe() && !$request->isMethod('DELETE'));
1,982✔
73
        }
74

UNCOV
75
        if (null === $operation->canRead() && $operation instanceof HttpOperation) {
1,982✔
UNCOV
76
            $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
1,979✔
77
        }
78

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

UNCOV
83
        $body = $this->provider->provide($operation, $uriVariables, $context);
1,982✔
84

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

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

UNCOV
101
        $context['previous_data'] = $request->attributes->get('previous_data');
1,964✔
UNCOV
102
        $context['data'] = $request->attributes->get('data');
1,964✔
103

UNCOV
104
        if (null === $operation->canWrite()) {
1,964✔
UNCOV
105
            $operation = $operation->withWrite(!$request->isMethodSafe());
1,958✔
106
        }
107

UNCOV
108
        if (null === $operation->canSerialize()) {
1,964✔
UNCOV
109
            $operation = $operation->withSerialize(true);
1,964✔
110
        }
111

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