Passed
Push — dev ( cc3488...536a25 )
by Sergey
05:38
created

ApiManagementEntriesController::editProcess()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 32
rs 9.568
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype;
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
18
class ApiManagementEntriesController extends Container
0 ignored issues
show
Bug introduced by
The type Flextype\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...
19
{
20
    /**
21
     * Management Entries Index page
22
     *
23
     * @param Request  $request  PSR7 request
24
     * @param Response $response PSR7 response
25
     */
26
    public function index(Request $request, Response $response) : Response
27
    {
28
        $tokens = [];
29
        $tokens_list = Filesystem::listContents(PATH['project'] . '/tokens' . '/management/entries/');
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
30
31
        if (count($tokens_list) > 0) {
32
            foreach ($tokens_list as $token) {
33
                if ($token['type'] == 'dir' && Filesystem::has(PATH['project'] . '/tokens' . '/management/entries/' . $token['dirname'] . '/token.yaml')) {
34
                    $tokens[] = $token;
35
                }
36
            }
37
        }
38
39
        return $this->twig->render(
40
            $response,
41
            'plugins/admin/templates/system/api/management/entries/index.html',
42
            [
43
                'menu_item' => 'api',
44
                'tokens' => $tokens,
45
                'links' =>  [
46
                    'api' => [
47
                        'link' => $this->router->pathFor('admin.api.index'),
48
                        '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

48
                        'title' => /** @scrutinizer ignore-call */ __('admin_api'),
Loading history...
49
                    ],
50
                    'api_management' => [
51
                        'link' => $this->router->pathFor('admin.api_management.index'),
52
                        'title' => __('admin_management')
53
                    ],
54
                    'api_management_entries' => [
55
                        'link' => $this->router->pathFor('admin.api_management_entries.index'),
56
                        'title' => __('admin_entries'),
57
                        'active' => true
58
                    ],
59
                ],
60
                'buttons' => [
61
                    'api_management_entries_add' => [
62
                        'link' => $this->router->pathFor('admin.api_management_entries.add'),
63
                        'title' => __('admin_create_new_token')
64
                    ],
65
                ],
66
            ]
67
        );
68
    }
69
70
    /**
71
     * Add token page
72
     *
73
     * @param Request  $request  PSR7 request
74
     * @param Response $response PSR7 response
75
     */
76
    public function add(Request $request, Response $response) : Response
77
    {
78
        return $this->twig->render(
79
            $response,
80
            'plugins/admin/templates/system/api/management/entries/add.html',
81
            [
82
                'menu_item' => 'api',
83
                'links' =>  [
84
                    'api' => [
85
                        'link' => $this->router->pathFor('admin.api.index'),
86
                        '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

86
                        'title' => /** @scrutinizer ignore-call */ __('admin_api'),
Loading history...
87
                    ],
88
                    'api_management' => [
89
                        'link' => $this->router->pathFor('admin.api_management.index'),
90
                        'title' => __('admin_management')
91
                    ],
92
                    'api_management_entries' => [
93
                        'link' => $this->router->pathFor('admin.api_management_entries.index'),
94
                        'title' => __('admin_entries')
95
                    ],
96
                    'api_management_entries_add' => [
97
                        'link' => $this->router->pathFor('admin.api_management_entries.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' . '/management/entries/' . $api_token;
0 ignored issues
show
Bug introduced by
The constant Flextype\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($this->registry->get('flextype.settings.date_format'), time());
132
133
            // Create API Token account
134
            if (Filesystem::write(
135
                $api_token_file_path,
136
                $this->serializer->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' => Session::get('uuid'),
144
                    'created_at' => $time,
145
                    'updated_by' => Session::get('uuid'),
146
                    'updated_at' => $time,
147
                ], 'yaml')
148
            )) {
149
                $this->flash->addMessage('success', __('admin_message_management_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

149
                $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_management_entries_api_token_created'));
Loading history...
150
            } else {
151
                $this->flash->addMessage('error', __('admin_message_management_entries_api_token_was_not_created1'));
152
            }
153
        } else {
154
            $this->flash->addMessage('error', __('admin_message_management_entries_api_token_was_not_created2'));
155
        }
156
157
        if (isset($post_data['create-and-edit'])) {
158
            return $response->withRedirect($this->router->pathFor('admin.api_management_entries.edit') . '?token=' . $api_token);
159
        } else {
160
            return $response->withRedirect($this->router->pathFor('admin.api_management_entries.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 = $this->serializer->decode(Filesystem::read(PATH['project'] . '/tokens' . '/management/entries/' . $token . '/token.yaml'), 'yaml');
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
174
175
        return $this->twig->render(
176
            $response,
177
            'plugins/admin/templates/system/api/management/entries/edit.html',
178
            [
179
                'menu_item' => 'api',
180
                'token' => $token,
181
                'token_data' => $token_data,
182
                'links' =>  [
183
                    'api' => [
184
                        'link' => $this->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_tokens' => [
188
                        'link' => $this->router->pathFor('admin.api_management.index'),
189
                        'title' => __('admin_management')
190
                    ],
191
                    'api_management_entries' => [
192
                        'link' => $this->router->pathFor('admin.api_management_entries.index'),
193
                        'title' => __('admin_entries')
194
                    ],
195
                    'api_tokens_edit' => [
196
                        'link' => $this->router->pathFor('admin.api_management_entries.edit'),
197
                        'title' => __('admin_edit_delivery_token'),
198
                        'active' => true
199
                    ],
200
                ]
201
            ]
202
        );
203
    }
204
205
    /**
206
     * Edit token - process
207
     *
208
     * @param Request  $request  PSR7 request
209
     * @param Response $response PSR7 response
210
     */
211
    public function editProcess(Request $request, Response $response) : Response
212
    {
213
        // Get POST data
214
        $post_data = $request->getParsedBody();
215
216
        $api_token_dir_path  = PATH['project'] . '/tokens' . '/management/entries/' . $post_data['token'];
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
217
        $api_token_file_path = $api_token_dir_path . '/' . 'token.yaml';
218
219
        // Update API Token File
220
        if (Filesystem::has($api_token_file_path)) {
221
            if (Filesystem::write(
222
                $api_token_file_path,
223
                $this->serializer->encode([
224
                    'title' => $post_data['title'],
225
                    'icon' => $post_data['icon'],
226
                    'limit_calls' => (int) $post_data['limit_calls'],
227
                    'calls' => (int) $post_data['calls'],
228
                    'state' => $post_data['state'],
229
                    'uuid' => $post_data['uuid'],
230
                    'created_by' => $post_data['created_by'],
231
                    'created_at' => $post_data['created_at'],
232
                    'updated_by' => Session::get('uuid'),
233
                    'updated_at' => date($this->registry->get('flextype.settings.date_format'), time()),
234
                ], 'yaml')
235
            )) {
236
                $this->flash->addMessage('success', __('admin_message_management_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

236
                $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_management_entries_api_token_updated'));
Loading history...
237
            }
238
        } else {
239
            $this->flash->addMessage('error', __('admin_message_management_entries_api_token_was_not_updated'));
240
        }
241
242
        return $response->withRedirect($this->router->pathFor('admin.api_management_entries.index'));
243
    }
244
245
    /**
246
     * Delete token - process
247
     *
248
     * @param Request  $request  PSR7 request
249
     * @param Response $response PSR7 response
250
     */
251
    public function deleteProcess(Request $request, Response $response) : Response
252
    {
253
        // Get POST data
254
        $post_data = $request->getParsedBody();
255
256
        $api_token_dir_path = PATH['project'] . '/tokens' . '/management/entries/' . $post_data['token'];
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
257
258
        if (Filesystem::deleteDir($api_token_dir_path)) {
259
            $this->flash->addMessage('success', __('admin_message_management_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

259
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_management_entries_api_token_deleted'));
Loading history...
260
        } else {
261
            $this->flash->addMessage('error', __('admin_message_management_entries_api_token_was_not_deleted'));
262
        }
263
264
        return $response->withRedirect($this->router->pathFor('admin.api_management_entries.index'));
265
    }
266
}
267