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

api-platform / core / 10797021570

10 Sep 2024 04:49PM UTC coverage: 7.679% (+0.02%) from 7.659%
10797021570

push

github

soyuka
fix(laravel): parameter fixes and tests

32 of 49 new or added lines in 6 files covered. (65.31%)

2574 existing lines in 81 files now uncovered.

12619 of 164324 relevant lines covered (7.68%)

23.18 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');
×
45
        if (!$operation) {
×
46
            throw new \RuntimeException('Operation not found.');
×
47
        }
48

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

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

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

74
        if (null === $operation->canDeserialize()) {
×
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
            $parameter = $request->route($parameterName);
×
UNCOV
108
            if (\is_string($parameter) && ($format = $request->attributes->get('_format')) && str_contains($parameter, $format)) {
×
UNCOV
109
                $parameter = substr($parameter, 0, \strlen($parameter) - (\strlen($format) + 1));
×
110
            }
111

UNCOV
112
            $uriVariables[(string) $parameterName] = $parameter;
×
113
        }
114

UNCOV
115
        return $uriVariables;
×
116
    }
117
}
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