Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
60 / 60
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
IndexController
100.00% covered (success)
100.00%
60 / 60
100.00% covered (success)
100.00%
8 / 8
9
100.00% covered (success)
100.00%
1 / 1
 index
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 indexV0
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 openApiV0
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 info
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 options
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 notFound
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 notImplemented
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getApiSpecV0
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Controllers\Api;
6
7use cebe\openapi\spec\OpenApi;
8use Engelsystem\Http\Response;
9use League\OpenAPIValidation\PSR7\ValidatorBuilder as OpenApiValidatorBuilder;
10
11class IndexController extends ApiController
12{
13    public array $permissions = [
14        'index' => 'api',
15        'indexV0' => 'api',
16        'openApiV0' => 'api',
17    ];
18
19    public function index(): Response
20    {
21        return $this->response
22            ->withContent(json_encode([
23                'versions' => [
24                    $this->getApiSpecV0()->info->version => '/v0-beta',
25                ],
26            ]));
27    }
28
29    public function indexV0(): Response
30    {
31        $schema = $this->getApiSpecV0();
32        $info = $schema->info;
33        $paths = [];
34        foreach ($schema->paths->getIterator() as $path => $item) {
35            $paths[] = $path;
36        }
37
38        return $this->response
39            ->withContent(json_encode([
40                'version' => $info->version,
41                'description' => $info->description,
42                'paths' => $paths,
43            ]));
44    }
45
46    public function openApiV0(): Response
47    {
48        $schema = $this->getApiSpecV0();
49        $data = $schema->getSerializableData();
50        unset($data->servers[1]);
51        $data->servers[0]->url = url('/api/v0-beta');
52
53        return $this->response->withContent(json_encode($data));
54    }
55
56    public function info(): Response
57    {
58        $config = config();
59        $schema = $this->getApiSpecV0();
60
61        $data = ['data' => [
62            'api' => $schema->info->version,
63            'spec' => url('/api/v0-beta/openapi'),
64            'name' => (string) $config->get('name'),
65            'app_name' => (string) $config->get('app_name'),
66            'url' => url('/'),
67            'timezone' => (string) $config->get('timezone'),
68            'buildup' => [
69                'start' => $config->get('buildup_start'),
70            ],
71            'event' => [
72                'start' => $config->get('event_start'),
73                'end' => $config->get('event_end'),
74            ],
75            'teardown' => [
76                'end' => $config->get('teardown_end'),
77            ],
78        ]];
79
80        return $this->response
81            ->withContent(json_encode($data));
82    }
83
84    public function options(): Response
85    {
86        // Respond to browser preflight options requests
87        return $this->response
88            ->setStatusCode(200)
89            ->withHeader('allow', 'OPTIONS, HEAD, GET')
90            ->withHeader('access-control-allow-headers', 'Authorization, x-api-key');
91    }
92
93    public function notFound(): Response
94    {
95        return $this->response
96            ->setStatusCode(404)
97            ->withContent(json_encode(['message' => 'Not implemented']));
98    }
99
100    public function notImplemented(): Response
101    {
102        return $this->response
103            ->setStatusCode(405)
104            ->withHeader('allow', 'GET')
105            ->withContent(json_encode(['message' => 'Method not implemented']));
106    }
107
108    protected function getApiSpecV0(): OpenApi
109    {
110        $openApiDefinition = app()->get('path.resources.api') . '/openapi.yml';
111        return (new OpenApiValidatorBuilder())
112            ->fromYamlFile($openApiDefinition)
113            ->getResponseValidator()
114            ->getSchema();
115    }
116}