Completed
Push — master ( 002acb...069052 )
by Pierre
03:01
created

Config::swaggerdoc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Controllers;
4
5
use App\Interfaces\Controllers\IApi;
6
use App\Reuse\Controllers\AbstractApi;
7
use App\Container;
8
use App\Http\Response;
9
10
/**
11
 * @OA\Info(title="Nymfonya",version="v1")
12
 * @OA\PathItem(path=".")
13
 */
14
final class Config extends AbstractApi implements IApi
15
{
16
17
    const KEY_LENGTH = 64;
18
    const _TITLE = 'title';
19
    const _ACTION = 'action';
20
    const _DATAS = 'datas';
21
    const _HOWTO = 'howto';
22
23
    /**
24
     * instanciate
25
     *
26
     * @param Container $container
27
     */
28 9
    public function __construct(Container $container)
29
    {
30 9
        parent::__construct($container);
31
    }
32
33
    /**
34
     * help action
35
     *
36
     * @Role anonymous
37
     * @return Config
38
     */
39 1
    final public function help(): Config
40
    {
41 1
        $baseUri = $this->baseRootUri();
42
        $helpAction = [
43
            [
44 1
                self::_TITLE => 'App key generate',
45 1
                self::_ACTION => $baseUri . 'keygen',
46 1
                self::_HOWTO => 'Copy paste result in '
47
                    . 'config/$env jwt/secret'
48
            ]
49
        ];
50 1
        $this->response
51 1
            ->setCode(Response::HTTP_OK)
52 1
            ->setContent($helpAction);
53 1
        return $this;
54
    }
55
56
    /**
57
     * false action
58
     *
59
     * @return boolean
60
     */
61 1
    final public function false()
62
    {
63 1
        return false;
64
    }
65
66
    /**
67
     * keygen action
68
     *
69
     * @Role anonymous
70
     * @return Config
71
     */
72 1
    final public function keygen(): Config
73
    {
74 1
        $this->response
75 1
            ->setCode(Response::HTTP_OK)
76 1
            ->setContent(
77 1
                $this->getActionItem(
78 1
                    'App key generate',
79 1
                    __FUNCTION__,
80 1
                    base64_encode(
81 1
                        openssl_random_pseudo_bytes(self::KEY_LENGTH)
82
                    )
83
                )
84
            );
85 1
        return $this;
86
    }
87
88
    /**
89
     * account action
90
     *
91
     * @Role anonymous
92
     * @return Config
93
     */
94 1
    final public function account(): Config
95
    {
96 1
        $line = 'Undefined function readline';
97 1
        if ($this->hasReadLine()) {
98
            //$line = readline("Command: ");
99
        }
100 1
        $this->response
101 1
            ->setCode(Response::HTTP_OK)
102 1
            ->setContent(['error' => false, 'command' => $line]);
103 1
        return $this;
104
    }
105
106
    /**
107
     * generate swagger doc from code
108
     *
109
     * @return Test
0 ignored issues
show
Bug introduced by
The type App\Controllers\Test was not found. Did you mean Test? If so, make sure to prefix the type with \.
Loading history...
110
     */
111 1
    final public function swaggerdoc()
112
    {
113 1
        $path = dirname(__FILE__);
114 1
        $controllerPath = '/Api/V1/Restful.php';
115 1
        $openapi = \OpenApi\scan($path . $controllerPath);
116 1
        $this->response
117 1
            ->setCode(Response::HTTP_OK)
118 1
            ->setContent($openapi->toYaml());
119 1
        return $this;
120
    }
121
122
    /**
123
     * return current base root uri
124
     *
125
     * @return string
126
     */
127 1
    protected function baseRootUri(): string
128
    {
129 1
        return dirname($this->request->getUri()) . '/';
130
    }
131
132
    /**
133
     * return array
134
     *
135
     * @param string $title
136
     * @param string $action
137
     * @param string $datas
138
     * @return array
139
     */
140 1
    protected function getActionItem(string $title, string $action, string $datas): array
141
    {
142
        return [
143
            [
144 1
                self::_TITLE => $title,
145 1
                self::_ACTION => $this->baseRootUri() . $action,
146 1
                self::_DATAS => $datas
147
            ]
148
        ];
149
    }
150
151
    /**
152
     * return true if php was configured with --readline option
153
     *
154
     * @return boolean
155
     */
156 1
    protected function hasReadLine(): bool
157
    {
158 1
        return function_exists("readline");
159
    }
160
}
161