Passed
Push — dev ( e66ec2...f81248 )
by Sergey
12:00 queued 11s
created

ApiRegistryController::index()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 23
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 34
rs 9.2408
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype\Plugin\Admin\Controllers;
6
7
use Flextype\Component\Filesystem\Filesystem;
0 ignored issues
show
Bug introduced by
The type Flextype\Component\Filesystem\Filesystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Flextype\Component\Session\Session;
0 ignored issues
show
Bug introduced by
The type Flextype\Component\Session\Session was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Psr\Http\Message\ResponseInterface as Response;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Psr\Http\Message\ServerRequestInterface as Request;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Ramsey\Uuid\Uuid;
0 ignored issues
show
Bug introduced by
The type Ramsey\Uuid\Uuid was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use function bin2hex;
13
use function date;
14
use function Flextype\Component\I18n\__;
0 ignored issues
show
introduced by
The function Flextype\Component\I18n\__ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
use function random_bytes;
16
use function time;
17
use Flextype\App\Foundation\Container;
0 ignored issues
show
Bug introduced by
The type Flextype\App\Foundation\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class ApiRegistryController extends Container
20
{
21
    /**
22
     * Registry Index page
23
     *
24
     * @param Request  $request  PSR7 request
25
     * @param Response $response PSR7 response
26
     */
27
    public function index(Request $request, Response $response) : Response
28
    {
29
        $tokens = [];
30
        $tokens_list = Filesystem::listContents(PATH['project'] . '/tokens' . '/registry/');
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
32
        if (count($tokens_list) > 0) {
33
            foreach ($tokens_list as $token) {
34
                if ($token['type'] == 'dir' && Filesystem::has(PATH['project'] . '/tokens' . '/registry/' . $token['dirname'] . '/token.yaml')) {
35
                    $tokens[] = $token;
36
                }
37
            }
38
        }
39
40
        return $this->twig->render(
41
            $response,
42
            'plugins/admin/templates/system/api/registry/index.html',
43
            [
44
                'menu_item' => 'api',
45
                'tokens' => $tokens,
46
                'links' =>  [
47
                    'api' => [
48
                        'link' => $this->router->pathFor('admin.api.index'),
49
                        'title' => __('admin_api'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
                        'title' => /** @scrutinizer ignore-call */ __('admin_api'),
Loading history...
50
                    ],
51
                    'api_registry' => [
52
                        'link' => $this->router->pathFor('admin.api_registry.index'),
53
                        'title' => __('admin_registry'),
54
                        'active' => true
55
                    ],
56
                ],
57
                'buttons' => [
58
                    'api_registry_add' => [
59
                        'link' => $this->router->pathFor('admin.api_registry.add'),
60
                        'title' => __('admin_create_new_token')
61
                    ],
62
                ],
63
            ]
64
        );
65
    }
66
67
    /**
68
     * Add token page
69
     *
70
     * @param Request  $request  PSR7 request
71
     * @param Response $response PSR7 response
72
     */
73
    public function add(Request $request, Response $response) : Response
74
    {
75
        return $this->twig->render(
76
            $response,
77
            'plugins/admin/templates/system/api/registry/add.html',
78
            [
79
                'menu_item' => 'api',
80
                'links' =>  [
81
                    'api' => [
82
                        'link' => $this->router->pathFor('admin.api.index'),
83
                        'title' => __('admin_api'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
                        'title' => /** @scrutinizer ignore-call */ __('admin_api'),
Loading history...
84
                    ],
85
                    'api_registry' => [
86
                        'link' => $this->router->pathFor('admin.api_registry.index'),
87
                        'title' => __('admin_registry')
88
                    ],
89
                    'api_registry_add' => [
90
                        'link' => $this->router->pathFor('admin.api_registry.add'),
91
                        'title' => __('admin_create_new_token'),
92
                        'active' => true
93
                    ],
94
                ],
95
            ]
96
        );
97
    }
98
99
    /**
100
     * Add new token - process
101
     *
102
     * @param Request  $request  PSR7 request
103
     * @param Response $response PSR7 response
104
     */
105
    public function addProcess(Request $request, Response $response) : Response
106
    {
107
        // Get POST data
108
        $post_data = $request->getParsedBody();
109
110
        // Generate API token
111
        $api_token = bin2hex(random_bytes(16));
112
113
        $api_token_dir_path  = PATH['project'] . '/tokens/registry/' . $api_token;
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
114
        $api_token_file_path = $api_token_dir_path . '/token.yaml';
115
116
        if (! Filesystem::has($api_token_file_path)) {
117
118
            Filesystem::createDir($api_token_dir_path);
119
120
            // Generate UUID
121
            $uuid = Uuid::uuid4()->toString();
122
123
            // Get time
124
            $time = date($this->registry->get('flextype.settings.date_format'), time());
125
126
            // Create API Token account
127
            if (Filesystem::write(
128
                $api_token_file_path,
129
                $this->yaml->encode([
130
                    'title' => $post_data['title'],
131
                    'icon' => $post_data['icon'],
132
                    'limit_calls' => (int) $post_data['limit_calls'],
133
                    'calls' => (int) 0,
134
                    'state' => $post_data['state'],
135
                    'uuid' => $uuid,
136
                    'created_by' => Session::get('uuid'),
137
                    'created_at' => $time,
138
                    'updated_by' => Session::get('uuid'),
139
                    'updated_at' => $time,
140
                ])
141
            )) {
142
                $this->flash->addMessage('success', __('admin_message_registry_api_token_created'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
                $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_registry_api_token_created'));
Loading history...
143
            } else {
144
                $this->flash->addMessage('error', __('admin_message_registry_api_token_was_not_created1'));
145
            }
146
        } else {
147
            $this->flash->addMessage('error', __('admin_message_registry_api_token_was_not_created2'));
148
        }
149
150
        if (isset($post_data['create-and-edit'])) {
151
            return $response->withRedirect($this->router->pathFor('admin.api_registry.edit') . '?token=' . $api_token);
152
        } else {
153
            return $response->withRedirect($this->router->pathFor('admin.api_registry.index'));
154
        }
155
    }
156
157
    /**
158
     * Edit token page
159
     *
160
     * @param Request  $request  PSR7 request
161
     * @param Response $response PSR7 response
162
     */
163
    public function edit(Request $request, Response $response) : Response
164
    {
165
        $token      = $request->getQueryParams()['token'];
166
        $token_data = $this->yaml->decode(Filesystem::read(PATH['project'] . '/tokens/registry/' . $token . '/token.yaml'));
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
167
168
        return $this->twig->render(
169
            $response,
170
            'plugins/admin/templates/system/api/registry/edit.html',
171
            [
172
                'menu_item' => 'api',
173
                'token' => $token,
174
                'token_data' => $token_data,
175
                'links' =>  [
176
                    'api' => [
177
                        'link' => $this->router->pathFor('admin.api.index'),
178
                        'title' => __('admin_api')
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
                        'title' => /** @scrutinizer ignore-call */ __('admin_api')
Loading history...
179
                    ],
180
                    'api_registry' => [
181
                        'link' => $this->router->pathFor('admin.api_registry.index'),
182
                        'title' => __('admin_registry')
183
                    ],
184
                    'api_tokens_edit' => [
185
                        'link' => $this->router->pathFor('admin.api_registry.edit'),
186
                        'title' => __('admin_edit_token'),
187
                        'active' => true
188
                    ],
189
                ]
190
            ]
191
        );
192
    }
193
194
    /**
195
     * Edit token - process
196
     *
197
     * @param Request  $request  PSR7 request
198
     * @param Response $response PSR7 response
199
     */
200
    public function editProcess(Request $request, Response $response) : Response
201
    {
202
        // Get POST data
203
        $post_data = $request->getParsedBody();
204
205
        $api_token_dir_path  = PATH['project'] . '/tokens/registry/' . $post_data['token'];
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
206
        $api_token_file_path = $api_token_dir_path . '/' . 'token.yaml';
207
208
        // Update API Token File
209
        if (Filesystem::has($api_token_file_path)) {
210
            if (Filesystem::write(
211
                $api_token_file_path,
212
                $this->yaml->encode([
213
                    'title' => $post_data['title'],
214
                    'icon' => $post_data['icon'],
215
                    'limit_calls' => (int) $post_data['limit_calls'],
216
                    'calls' => (int) $post_data['calls'],
217
                    'state' => $post_data['state'],
218
                    'uuid' => $post_data['uuid'],
219
                    'created_by' => $post_data['created_by'],
220
                    'created_at' => $post_data['created_at'],
221
                    'updated_by' => Session::get('uuid'),
222
                    'updated_at' => date($this->registry->get('flextype.settings.date_format'), time()),
223
                ])
224
            )) {
225
                $this->flash->addMessage('success', __('admin_message_registry_api_token_updated'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
                $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_registry_api_token_updated'));
Loading history...
226
            }
227
        } else {
228
            $this->flash->addMessage('error', __('admin_message_registry_api_token_was_not_updated'));
229
        }
230
231
        return $response->withRedirect($this->router->pathFor('admin.api_registry.index'));
232
    }
233
234
    /**
235
     * Delete token - process
236
     *
237
     * @param Request  $request  PSR7 request
238
     * @param Response $response PSR7 response
239
     */
240
    public function deleteProcess(Request $request, Response $response) : Response
241
    {
242
        // Get POST data
243
        $post_data = $request->getParsedBody();
244
245
        $api_token_dir_path = PATH['project'] . '/tokens/registry/' . $post_data['token'];
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
246
247
        if (Filesystem::deleteDir($api_token_dir_path)) {
248
            $this->flash->addMessage('success', __('admin_message_registry_api_token_deleted'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

248
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_registry_api_token_deleted'));
Loading history...
249
        } else {
250
            $this->flash->addMessage('error', __('admin_message_registry_api_token_was_not_deleted'));
251
        }
252
253
        return $response->withRedirect($this->router->pathFor('admin.api_registry.index'));
254
    }
255
}
256