ApiRegistryController::edit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 25
rs 9.6333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype\Plugin\Admin\Controllers;
6
7
use Flextype\Component\Filesystem\Filesystem;
8
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
18
class ApiRegistryController
19
{
20
    /**
21
     * __construct
22
     */
23
     public function __construct()
24
     {
25
26
     }
27
28
    /**
29
     * Registry Index page
30
     *
31
     * @param Request  $request  PSR7 request
32
     * @param Response $response PSR7 response
33
     */
34
    public function index(Request $request, Response $response) : Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

34
    public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        $tokens = [];
37
        $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...
38
39
        if (count($tokens_list) > 0) {
40
            foreach ($tokens_list as $token) {
41
                if ($token['type'] == 'dir' && Filesystem::has(PATH['project'] . '/tokens' . '/registry/' . $token['dirname'] . '/token.yaml')) {
42
                    $tokens[] = $token;
43
                }
44
            }
45
        }
46
47
        return flextype('twig')->render(
0 ignored issues
show
Bug introduced by
The function flextype 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

47
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
48
            $response,
49
            'plugins/admin/templates/system/api/registry/index.html',
50
            [
51
                'menu_item' => 'api',
52
                'tokens' => $tokens,
53
                'links' =>  [
54
                    'api' => [
55
                        'link' => flextype('router')->pathFor('admin.api.index'),
56
                        '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

56
                        'title' => /** @scrutinizer ignore-call */ __('admin_api'),
Loading history...
57
                    ],
58
                    'api_registry' => [
59
                        'link' => flextype('router')->pathFor('admin.api_registry.index'),
60
                        'title' => __('admin_registry'),
61
                        'active' => true
62
                    ],
63
                ],
64
                'buttons' => [
65
                    'api_registry_add' => [
66
                        'link' => flextype('router')->pathFor('admin.api_registry.add'),
67
                        'title' => __('admin_create_new_token')
68
                    ],
69
                ],
70
            ]
71
        );
72
    }
73
74
    /**
75
     * Add token page
76
     *
77
     * @param Request  $request  PSR7 request
78
     * @param Response $response PSR7 response
79
     */
80
    public function add(Request $request, Response $response) : Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

80
    public function add(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        return flextype('twig')->render(
0 ignored issues
show
Bug introduced by
The function flextype 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

82
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
83
            $response,
84
            'plugins/admin/templates/system/api/registry/add.html',
85
            [
86
                'menu_item' => 'api',
87
                'links' =>  [
88
                    'api' => [
89
                        'link' => flextype('router')->pathFor('admin.api.index'),
90
                        '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

90
                        'title' => /** @scrutinizer ignore-call */ __('admin_api'),
Loading history...
91
                    ],
92
                    'api_registry' => [
93
                        'link' => flextype('router')->pathFor('admin.api_registry.index'),
94
                        'title' => __('admin_registry')
95
                    ],
96
                    'api_registry_add' => [
97
                        'link' => flextype('router')->pathFor('admin.api_registry.add'),
98
                        'title' => __('admin_create_new_token'),
99
                        'active' => true
100
                    ],
101
                ],
102
            ]
103
        );
104
    }
105
106
    /**
107
     * Add new token - process
108
     *
109
     * @param Request  $request  PSR7 request
110
     * @param Response $response PSR7 response
111
     */
112
    public function addProcess(Request $request, Response $response) : Response
113
    {
114
        // Get POST data
115
        $post_data = $request->getParsedBody();
116
117
        // Generate API token
118
        $api_token = bin2hex(random_bytes(16));
119
120
        $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...
121
        $api_token_file_path = $api_token_dir_path . '/token.yaml';
122
123
        if (! Filesystem::has($api_token_file_path)) {
124
125
            Filesystem::createDir($api_token_dir_path);
126
127
            // Generate UUID
128
            $uuid = Uuid::uuid4()->toString();
129
130
            // Get time
131
            $time = date(flextype('registry')->get('flextype.settings.date_format'), time());
0 ignored issues
show
Bug introduced by
The function flextype 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

131
            $time = date(/** @scrutinizer ignore-call */ flextype('registry')->get('flextype.settings.date_format'), time());
Loading history...
132
133
            // Create API Token account
134
            if (Filesystem::write(
135
                $api_token_file_path,
136
                flextype('serializers')->yaml()->encode([
137
                    'title' => $post_data['title'],
138
                    'icon' => $post_data['icon'],
139
                    'limit_calls' => (int) $post_data['limit_calls'],
140
                    'calls' => (int) 0,
141
                    'state' => $post_data['state'],
142
                    'uuid' => $uuid,
143
                    'created_by' => flextype('session')->get('uuid'),
144
                    'created_at' => $time,
145
                    'updated_by' => flextype('session')->get('uuid'),
146
                    'updated_at' => $time,
147
                ])
148
            )) {
149
                flextype('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

149
                flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_registry_api_token_created'));
Loading history...
150
            } else {
151
                flextype('flash')->addMessage('error', __('admin_message_registry_api_token_was_not_created1'));
152
            }
153
        } else {
154
            flextype('flash')->addMessage('error', __('admin_message_registry_api_token_was_not_created2'));
155
        }
156
157
        if (isset($post_data['create-and-edit'])) {
158
            return $response->withRedirect(flextype('router')->pathFor('admin.api_registry.edit') . '?token=' . $api_token);
159
        } else {
160
            return $response->withRedirect(flextype('router')->pathFor('admin.api_registry.index'));
161
        }
162
    }
163
164
    /**
165
     * Edit token page
166
     *
167
     * @param Request  $request  PSR7 request
168
     * @param Response $response PSR7 response
169
     */
170
    public function edit(Request $request, Response $response) : Response
171
    {
172
        $token      = $request->getQueryParams()['token'];
173
        $token_data = flextype('serializers')->yaml()->decode(Filesystem::read(PATH['project'] . '/tokens/registry/' . $token . '/token.yaml'));
0 ignored issues
show
Bug introduced by
The function flextype 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

173
        $token_data = /** @scrutinizer ignore-call */ flextype('serializers')->yaml()->decode(Filesystem::read(PATH['project'] . '/tokens/registry/' . $token . '/token.yaml'));
Loading history...
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...
174
175
        return flextype('twig')->render(
176
            $response,
177
            'plugins/admin/templates/system/api/registry/edit.html',
178
            [
179
                'menu_item' => 'api',
180
                'token' => $token,
181
                'token_data' => $token_data,
182
                'links' =>  [
183
                    'api' => [
184
                        'link' => flextype('router')->pathFor('admin.api.index'),
185
                        '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

185
                        'title' => /** @scrutinizer ignore-call */ __('admin_api')
Loading history...
186
                    ],
187
                    'api_registry' => [
188
                        'link' => flextype('router')->pathFor('admin.api_registry.index'),
189
                        'title' => __('admin_registry')
190
                    ],
191
                    'api_tokens_edit' => [
192
                        'link' => flextype('router')->pathFor('admin.api_registry.edit'),
193
                        'title' => __('admin_edit_token'),
194
                        'active' => true
195
                    ],
196
                ]
197
            ]
198
        );
199
    }
200
201
    /**
202
     * Edit token - process
203
     *
204
     * @param Request  $request  PSR7 request
205
     * @param Response $response PSR7 response
206
     */
207
    public function editProcess(Request $request, Response $response) : Response
208
    {
209
        // Get POST data
210
        $post_data = $request->getParsedBody();
211
212
        $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...
213
        $api_token_file_path = $api_token_dir_path . '/' . 'token.yaml';
214
215
        // Update API Token File
216
        if (Filesystem::has($api_token_file_path)) {
217
            if (Filesystem::write(
218
                $api_token_file_path,
219
                flextype('serializers')->yaml()->encode([
0 ignored issues
show
Bug introduced by
The function flextype 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

219
                /** @scrutinizer ignore-call */ 
220
                flextype('serializers')->yaml()->encode([
Loading history...
220
                    'title' => $post_data['title'],
221
                    'icon' => $post_data['icon'],
222
                    'limit_calls' => (int) $post_data['limit_calls'],
223
                    'calls' => (int) $post_data['calls'],
224
                    'state' => $post_data['state'],
225
                    'uuid' => $post_data['uuid'],
226
                    'created_by' => $post_data['created_by'],
227
                    'created_at' => $post_data['created_at'],
228
                    'updated_by' => flextype('session')->get('uuid'),
229
                    'updated_at' => date(flextype('registry')->get('flextype.settings.date_format'), time()),
230
                ])
231
            )) {
232
                flextype('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

232
                flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_registry_api_token_updated'));
Loading history...
233
            }
234
        } else {
235
            flextype('flash')->addMessage('error', __('admin_message_registry_api_token_was_not_updated'));
236
        }
237
238
        return $response->withRedirect(flextype('router')->pathFor('admin.api_registry.index'));
239
    }
240
241
    /**
242
     * Delete token - process
243
     *
244
     * @param Request  $request  PSR7 request
245
     * @param Response $response PSR7 response
246
     */
247
    public function deleteProcess(Request $request, Response $response) : Response
248
    {
249
        // Get POST data
250
        $post_data = $request->getParsedBody();
251
252
        $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...
253
254
        if (Filesystem::deleteDir($api_token_dir_path)) {
255
            flextype('flash')->addMessage('success', __('admin_message_registry_api_token_deleted'));
0 ignored issues
show
Bug introduced by
The function flextype 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

255
            /** @scrutinizer ignore-call */ 
256
            flextype('flash')->addMessage('success', __('admin_message_registry_api_token_deleted'));
Loading history...
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

255
            flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_registry_api_token_deleted'));
Loading history...
256
        } else {
257
            flextype('flash')->addMessage('error', __('admin_message_registry_api_token_was_not_deleted'));
258
        }
259
260
        return $response->withRedirect(flextype('router')->pathFor('admin.api_registry.index'));
261
    }
262
}
263