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

eliashaeussler / typo3-badges / 10149625393

29 Jul 2024 06:17PM UTC coverage: 98.947%. Remained the same
10149625393

Pull #810

github

web-flow
Merge 8d1596ae6 into 483491931
Pull Request #810: [TASK] Update eslint to v9

470 of 475 relevant lines covered (98.95%)

7.47 hits per line

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

100.0
/src/Controller/ApiSpecificationController.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Symfony project "eliashaeussler/typo3-badges".
7
 *
8
 * Copyright (C) 2021-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace App\Controller;
25

26
use Symfony\Component\HttpFoundation\JsonResponse;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
30
use Symfony\Component\Routing\Annotation\Route;
31
use Symfony\Component\Yaml\Yaml;
32

33
use function dirname;
34
use function is_array;
35

36
/**
37
 * ApiSpecificationController.
38
 *
39
 * @author Elias Häußler <elias@haeussler.dev>
40
 * @license GPL-3.0-or-later
41
 */
42
#[Route(
43
    path: '/spec.{_format}',
44
    requirements: [
45
        '_format' => 'json|yaml',
46
    ],
47
    methods: ['GET'],
48
    format: 'json',
49
)]
50
final class ApiSpecificationController
51
{
52
    public function __invoke(Request $request): Response
3✔
53
    {
54
        $format = $request->getRequestFormat();
3✔
55
        $specification = $this->loadSpecification();
3✔
56

57
        switch ($format) {
58
            case 'yaml':
3✔
59
                $yaml = Yaml::dump($specification, 99, 2);
1✔
60
                $response = new Response($yaml);
1✔
61
                $contentType = 'application/openapi+yaml';
1✔
62
                break;
1✔
63

64
            case 'json':
2✔
65
            default:
66
                $json = json_encode(
2✔
67
                    $specification,
2✔
68
                    \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_NUMERIC_CHECK,
2✔
69
                );
2✔
70
                $response = JsonResponse::fromJsonString($json);
2✔
71
                $contentType = 'application/openapi+json';
2✔
72
                break;
2✔
73
        }
74

75
        $response->headers->set('Content-Type', $contentType);
3✔
76

77
        return $response;
3✔
78
    }
79

80
    /**
81
     * @return array<string, mixed>
82
     */
83
    private function loadSpecification(): array
3✔
84
    {
85
        $apiSpecificationPath = dirname(__DIR__, 2).'/spec/typo3-badges.oas3.yaml';
3✔
86
        $fileContents = Yaml::parseFile($apiSpecificationPath);
3✔
87

88
        if (!is_array($fileContents)) {
3✔
89
            // @codeCoverageIgnoreStart
90
            throw new BadRequestHttpException('Unable to load API specification file.');
91
            // @codeCoverageIgnoreEnd
92
        }
93

94
        return $fileContents;
3✔
95
    }
96
}
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