• 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/State/SwaggerUiProcessor.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\State;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\Metadata\UrlGeneratorInterface;
19
use ApiPlatform\OpenApi\OpenApi;
20
use ApiPlatform\OpenApi\Options;
21
use ApiPlatform\OpenApi\Serializer\NormalizeOperationNameTrait;
22
use ApiPlatform\State\ProcessorInterface;
23
use Illuminate\Http\Response;
24
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25

26
/**
27
 * @internal
28
 *
29
 * @implements ProcessorInterface<OpenApi, Response>
30
 */
31
final class SwaggerUiProcessor implements ProcessorInterface
32
{
33
    use NormalizeOperationNameTrait;
34

35
    /**
36
     * @param array<string, string[]> $formats
37
     */
38
    public function __construct(
39
        private readonly UrlGeneratorInterface $urlGenerator,
40
        private readonly NormalizerInterface $normalizer,
41
        private readonly Options $openApiOptions,
42
        private readonly array $formats = [],
43
        private readonly ?string $oauthClientId = null,
44
        private readonly ?string $oauthClientSecret = null,
45
        private readonly bool $oauthPkce = false,
46
    ) {
UNCOV
47
    }
×
48

49
    /**
50
     * @param OpenApi $openApi
51
     */
52
    public function process(mixed $openApi, Operation $operation, array $uriVariables = [], array $context = []): Response
53
    {
54
        $request = $context['request'] ?? null;
×
55

56
        $swaggerContext = [
×
57
            'formats' => $this->formats,
×
58
            'title' => $openApi->getInfo()->getTitle(),
×
59
            'description' => $openApi->getInfo()->getDescription(),
×
60
            'originalRoute' => $request->attributes->get('_api_original_route', $request->attributes->get('_route')),
×
61
            'originalRouteParams' => $request->attributes->get('_api_original_route_params', $request->attributes->get('_route_params', [])),
×
62
        ];
×
63

64
        $swaggerData = [
×
65
            'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
×
66
            'spec' => $this->normalizer->normalize($openApi, 'json', []),
×
67
            'oauth' => [
×
68
                'enabled' => $this->openApiOptions->getOAuthEnabled(),
×
69
                'type' => $this->openApiOptions->getOAuthType(),
×
70
                'flow' => $this->openApiOptions->getOAuthFlow(),
×
71
                'tokenUrl' => $this->openApiOptions->getOAuthTokenUrl(),
×
72
                'authorizationUrl' => $this->openApiOptions->getOAuthAuthorizationUrl(),
×
73
                'scopes' => $this->openApiOptions->getOAuthScopes(),
×
74
                'clientId' => $this->oauthClientId,
×
75
                'clientSecret' => $this->oauthClientSecret,
×
76
                'pkce' => $this->oauthPkce,
×
77
            ],
×
78
        ];
×
79

80
        $status = 200;
×
81
        $requestedOperation = $request?->attributes->get('_api_requested_operation') ?? null;
×
82
        if ($request->isMethodSafe() && $requestedOperation && $requestedOperation->getName()) {
×
83
            $swaggerData['id'] = $request->get('id');
×
84
            $swaggerData['queryParameters'] = $request->query->all();
×
85

86
            $swaggerData['shortName'] = $requestedOperation->getShortName();
×
87
            $swaggerData['operationId'] = $this->normalizeOperationName($requestedOperation->getName());
×
88

89
            [$swaggerData['path'], $swaggerData['method']] = $this->getPathAndMethod($swaggerData);
×
90
            $status = $requestedOperation->getStatus() ?? $status;
×
91
        }
92

93
        return new Response(view('api-platform::swagger-ui', $swaggerContext + ['swagger_data' => $swaggerData]), 200);
×
94
    }
95

96
    /**
97
     * @param array<string, mixed> $swaggerData
98
     *
99
     * @return array{0: string, 1: string}
100
     */
101
    private function getPathAndMethod(array $swaggerData): array
102
    {
103
        foreach ($swaggerData['spec']['paths'] as $path => $operations) {
×
104
            foreach ($operations as $method => $operation) {
×
105
                if (($operation['operationId'] ?? null) === $swaggerData['operationId']) {
×
106
                    return [$path, $method];
×
107
                }
108
            }
109
        }
110

111
        throw new RuntimeException(\sprintf('The operation "%s" cannot be found in the Swagger specification.', $swaggerData['operationId']));
×
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

© 2026 Coveralls, Inc