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

api-platform / core / 10489563861

21 Aug 2024 12:09PM UTC coverage: 7.704% (+2.4%) from 5.352%
10489563861

push

github

web-flow
chore(laravel): code cleanup (#6526)

* chore(laravel): code cleanup

* fix ci

* fix phpdoc

* fix review

* fix review

0 of 11 new or added lines in 3 files covered. (0.0%)

1768 existing lines in 74 files now uncovered.

12476 of 161932 relevant lines covered (7.7%)

23.0 hits per line

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

0.0
/src/Laravel/Controller/ApiPlatformController.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\Laravel\Controller;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactory;
18
use ApiPlatform\State\ProcessorInterface;
19
use ApiPlatform\State\ProviderInterface;
20
use Illuminate\Foundation\Application;
21
use Illuminate\Http\Request;
22
use Illuminate\Routing\Controller;
23
use Symfony\Component\HttpFoundation\Response;
24

25
class ApiPlatformController extends Controller
26
{
27
    /**
28
     * @param ProviderInterface<object>                                  $provider
29
     * @param ProcessorInterface<iterable<object>|object|null, Response> $processor
30
     */
31
    public function __construct(
32
        protected OperationMetadataFactory $operationMetadataFactory,
33
        protected ProviderInterface $provider,
34
        protected ProcessorInterface $processor,
35
        protected Application $app,
36
    ) {
37
    }
×
38

39
    /**
40
     * Display a listing of the resource.
41
     */
42
    public function __invoke(Request $request): Response
43
    {
44
        $operation = $request->attributes->get('_api_operation');
×
UNCOV
45
        if (!$operation) {
×
46
            throw new \RuntimeException('Operation not found.');
×
47
        }
48

NEW
49
        if (!$operation instanceof HttpOperation) {
×
NEW
50
            throw new \LogicException('Operation is not an HttpOperation.');
×
51
        }
52

UNCOV
53
        $uriVariables = $this->getUriVariables($request, $operation);
×
54
        // at some point we could introduce that back
55
        // if ($this->uriVariablesConverter) {
56
        //     $context = ['operation' => $operation, 'uri_variables_map' => $uriVariablesMap];
57
        //     $identifiers = $this->uriVariablesConverter->convert($identifiers, $operation->getClass() ?? $resourceClass, $context);
58
        // }
59

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

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

NEW
70
        if (null === $operation->canRead()) {
×
NEW
71
            $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe()); // @phpstan-ignore-line
×
72
        }
73

NEW
74
        if (null === $operation->canDeserialize()) {
×
NEW
75
            $operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true)); // @phpstan-ignore-line
×
76
        }
77

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

80
        // The provider can change the Operation, extract it again from the Request attributes
81
        if ($request->attributes->get('_api_operation') !== $operation) {
×
82
            $operation = $request->attributes->get('_api_operation');
×
83
            $uriVariables = $this->getUriVariables($request, $operation);
×
84
        }
85

86
        $context['previous_data'] = $request->attributes->get('previous_data');
×
87
        $context['data'] = $request->attributes->get('data');
×
88

89
        if (null === $operation->canWrite()) {
×
90
            $operation = $operation->withWrite(!$request->isMethodSafe());
×
91
        }
92

93
        if (null === $operation->canSerialize()) {
×
94
            $operation = $operation->withSerialize(true);
×
95
        }
96

97
        return $this->processor->process($body, $operation, $uriVariables, $context);
×
98
    }
99

100
    /**
101
     * @return array<string, mixed>
102
     */
103
    private function getUriVariables(Request $request, HttpOperation $operation): array
104
    {
105
        $uriVariables = [];
×
106
        foreach ($operation->getUriVariables() ?? [] as $parameterName => $_) {
×
107
            $uriVariables[(string) $parameterName] = $request->route($parameterName);
×
108
        }
109

110
        return $uriVariables;
×
111
    }
112
}
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

© 2026 Coveralls, Inc