ApiAccessController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 115
c 0
b 0
f 0
dl 0
loc 235
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A editProcess() 0 32 3
A add() 0 20 1
A edit() 0 25 1
A index() 0 34 5
A deleteProcess() 0 14 2
A addProcess() 0 49 4
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 ApiAccessController
20
{
21
    /**
22
     * Access 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
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

27
    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...
28
    {
29
        $tokens = [];
30
        $tokens_list = Filesystem::listContents(PATH['project'] . '/tokens' . '/access/');
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' . '/access/' . $token['dirname'] . '/token.yaml')) {
35
                    $tokens[] = $token;
36
                }
37
            }
38
        }
39
40
        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

40
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
41
            $response,
42
            'plugins/admin/templates/system/api/access/index.html',
43
            [
44
                'menu_item' => 'api',
45
                'tokens' => $tokens,
46
                'links' =>  [
47
                    'api' => [
48
                        'link' => flextype('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_access' => [
52
                        'link' => flextype('router')->pathFor('admin.api_access.index'),
53
                        'title' => __('admin_access'),
54
                        'active' => true
55
                    ],
56
                ],
57
                'buttons' => [
58
                    'api_access_add' => [
59
                        'link' => flextype('router')->pathFor('admin.api_access.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
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

73
    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...
74
    {
75
        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

75
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
76
            $response,
77
            'plugins/admin/templates/system/api/access/add.html',
78
            [
79
                'menu_item' => 'api',
80
                'links' =>  [
81
                    'api' => [
82
                        'link' => flextype('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_access' => [
86
                        'link' => flextype('router')->pathFor('admin.api_access.index'),
87
                        'title' => __('admin_access')
88
                    ],
89
                    'api_access_add' => [
90
                        'link' => flextype('router')->pathFor('admin.api_access.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' . '/access/' . $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(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

124
            $time = date(/** @scrutinizer ignore-call */ flextype('registry')->get('flextype.settings.date_format'), time());
Loading history...
125
126
            // Create API Token account
127
            if (Filesystem::write(
128
                $api_token_file_path,
129
                flextype('serializers')->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' => flextype('session')->get('uuid'),
137
                    'created_at' => $time,
138
                    'updated_by' => flextype('session')->get('uuid'),
139
                    'updated_at' => $time,
140
                ])
141
            )) {
142
                flextype('flash')->addMessage('success', __('admin_message_access_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
                flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_access_api_token_created'));
Loading history...
143
            } else {
144
                flextype('flash')->addMessage('error', __('admin_message_access_api_token_was_not_created1'));
145
            }
146
        } else {
147
            flextype('flash')->addMessage('error', __('admin_message_access_api_token_was_not_created2'));
148
        }
149
150
        if (isset($post_data['create-and-edit'])) {
151
            return $response->withRedirect(flextype('router')->pathFor('admin.api_access.edit') . '?token=' . $api_token);
152
        } else {
153
            return $response->withRedirect(flextype('router')->pathFor('admin.api_access.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 = flextype('serializers')->yaml()->decode(Filesystem::read(PATH['project'] . '/tokens' . '/access/' . $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

166
        $token_data = /** @scrutinizer ignore-call */ flextype('serializers')->yaml()->decode(Filesystem::read(PATH['project'] . '/tokens' . '/access/' . $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...
167
168
        return flextype('twig')->render(
169
            $response,
170
            'plugins/admin/templates/system/api/access/edit.html',
171
            [
172
                'menu_item' => 'api',
173
                'token' => $token,
174
                'token_data' => $token_data,
175
                'links' =>  [
176
                    'api' => [
177
                        'link' => flextype('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_access' => [
181
                        'link' => flextype('router')->pathFor('admin.api_access.index'),
182
                        'title' => __('admin_access')
183
                    ],
184
                    'api_tokens_edit' => [
185
                        'link' => flextype('router')->pathFor('admin.api_access.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' . '/access/' . $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
                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

212
                /** @scrutinizer ignore-call */ 
213
                flextype('serializers')->yaml()->encode([
Loading history...
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' => flextype('session')->get('uuid'),
222
                    'updated_at' => date(flextype('registry')->get('flextype.settings.date_format'), time()),
223
                ])
224
            )) {
225
                flextype('flash')->addMessage('success', __('admin_message_access_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
                flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_access_api_token_updated'));
Loading history...
226
            }
227
        } else {
228
            flextype('flash')->addMessage('error', __('admin_message_access_api_token_was_not_updated'));
229
        }
230
231
        return $response->withRedirect(flextype('router')->pathFor('admin.api_access.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' . '/access/' . $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
            flextype('flash')->addMessage('success', __('admin_message_access_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

248
            /** @scrutinizer ignore-call */ 
249
            flextype('flash')->addMessage('success', __('admin_message_access_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

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