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

api-platform / core / 14132025063

28 Mar 2025 03:10PM UTC coverage: 8.52% (-1.0%) from 9.544%
14132025063

push

github

soyuka
Merge 4.1

26 of 353 new or added lines in 22 files covered. (7.37%)

984 existing lines in 65 files now uncovered.

13394 of 157212 relevant lines covered (8.52%)

22.94 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\OperationMetadataFactoryInterface;
18
use ApiPlatform\State\ProcessorInterface;
19
use ApiPlatform\State\ProviderInterface;
20
use Illuminate\Http\Request;
21
use Illuminate\Routing\Controller;
22
use Symfony\Component\HttpFoundation\Response;
23

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

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

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

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

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

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

69
        if (null === $operation->canRead()) {
×
70
            $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
×
71
        }
72

73
        if (null === $operation->canDeserialize()) {
×
74
            $operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true));
×
75
        }
76

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

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

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

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

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

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

99
    /**
100
     * @return array<string, mixed>
101
     */
102
    private function getUriVariables(Request $request, HttpOperation $operation): array
103
    {
104
        $uriVariables = [];
×
105
        foreach ($operation->getUriVariables() ?? [] as $parameterName => $_) {
×
106
            $parameter = $request->route($parameterName);
×
107
            if (\is_string($parameter) && ($format = $request->attributes->get('_format')) && str_contains($parameter, $format)) {
×
108
                $parameter = substr($parameter, 0, \strlen($parameter) - (\strlen($format) + 1));
×
109
            }
110

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

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