ApiEntriesController::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
19
class ApiEntriesController
20
{
21
22
    /**
23
     * __construct
24
     */
25
     public function __construct()
26
     {
27
28
     }
29
30
    /**
31
     * Entries Index page
32
     *
33
     * @param Request  $request  PSR7 request
34
     * @param Response $response PSR7 response
35
     */
36
    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

36
    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...
37
    {
38
        $tokens = [];
39
        $tokens_list = Filesystem::listContents(PATH['project'] . '/tokens/entries/');
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...
40
41
        if (count($tokens_list) > 0) {
42
            foreach ($tokens_list as $token) {
43
                if ($token['type'] == 'dir' && Filesystem::has(PATH['project'] . '/tokens/entries/' . $token['dirname'] . '/token.yaml')) {
44
                    $tokens[] = $token;
45
                }
46
            }
47
        }
48
49
        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

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

58
                        'title' => /** @scrutinizer ignore-call */ __('admin_api'),
Loading history...
59
                    ],
60
                    'api_entries' => [
61
                        'link' => flextype('router')->pathFor('admin.api_entries.index'),
62
                        'title' => __('admin_entries'),
63
                        'active' => true
64
                    ],
65
                ],
66
                'buttons' => [
67
                    'api_entries_add' => [
68
                        'link' => flextype('router')->pathFor('admin.api_entries.add'),
69
                        'title' => __('admin_create_new_token')
70
                    ],
71
                ],
72
            ]
73
        );
74
    }
75
76
    /**
77
     * Add token page
78
     *
79
     * @param Request  $request  PSR7 request
80
     * @param Response $response PSR7 response
81
     */
82
    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

82
    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...
83
    {
84
        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

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

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

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

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

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

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

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

234
                flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_entries_api_token_updated'));
Loading history...
235
            }
236
        } else {
237
            flextype('flash')->addMessage('error', __('admin_message_entries_api_token_was_not_updated'));
238
        }
239
240
        return $response->withRedirect(flextype('router')->pathFor('admin.api_entries.index'));
241
    }
242
243
    /**
244
     * Delete token - process
245
     *
246
     * @param Request  $request  PSR7 request
247
     * @param Response $response PSR7 response
248
     */
249
    public function deleteProcess(Request $request, Response $response) : Response
250
    {
251
        // Get POST data
252
        $post_data = $request->getParsedBody();
253
254
        $api_token_dir_path = PATH['project'] . '/tokens/entries/' . $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...
255
256
        if (Filesystem::deleteDir($api_token_dir_path)) {
257
            flextype('flash')->addMessage('success', __('admin_message_entries_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

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

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