Passed
Push — master ( 247c45...35bddf )
by Sergey
03:13 queued 10s
created

ThemesAdminTemplatesController::edit()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
nc 1
nop 2
dl 0
loc 37
c 1
b 0
f 0
cc 5
rs 9.1928
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype\Plugin\ThemesAdmin\Controllers;
6
7
use Flextype\Component\Filesystem\Filesystem;
8
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...
9
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...
10
use function date;
11
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...
12
13
class ThemesAdminTemplatesController
14
{
15
    /**
16
     * Index page
17
     *
18
     * @param Request  $request  PSR7 request
19
     * @param Response $response PSR7 response
20
     */
21
    public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
22
    {
23
        // Get theme from request query params
24
        $theme = $request->getQueryParams()['theme'];
25
26
        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

26
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
27
            $response,
28
            'plugins/themes-admin/templates/extends/themes/templates/index.html',
29
            [
30
                'menu_item' => 'themes',
31
                'theme' => $theme,
32
                'templates_list' => flextype('themes')->getTemplates($theme),
33
                'partials_list' => flextype('themes')->getPartials($theme),
34
                'links' =>  [
35
                    'themes' => [
36
                        'link' => flextype('router')->pathFor('admin.themes.index'),
37
                        'title' => __('themes_admin_themes'),
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

37
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
38
39
                    ],
40
                    'templates' => [
41
                        'link' => flextype('router')->pathFor('admin.themes.templates.index') . '?theme=' . $theme,
42
                        'title' => __('themes_admin_templates'),
43
                        'active' => true
44
                    ],
45
                ],
46
                'buttons' => [
47
                    'templates_create' => [
48
                        'link' => flextype('router')->pathFor('admin.themes.templates.add') . '?theme=' . $theme,
49
                        'title' => __('themes_admin_create_new_template'),
50
51
                    ],
52
                ],
53
            ]
54
        );
55
    }
56
57
    /**
58
     * Add template
59
     *
60
     * @param Request  $request  PSR7 request
61
     * @param Response $response PSR7 response
62
     */
63
    public function add(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
64
    {
65
        // Get theme from request query params
66
        $theme = $request->getQueryParams()['theme'];
67
68
        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

68
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
69
            $response,
70
            'plugins/themes-admin/templates/extends/themes/templates/add.html',
71
            [
72
                'menu_item' => 'themes',
73
                'theme' => $theme,
74
                'links' =>  [
75
                    'themes' => [
76
                        'link' => flextype('router')->pathFor('admin.themes.index'),
77
                        'title' => __('themes_admin_themes'),
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

77
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
78
79
                    ],
80
                    'templates' => [
81
                        'link' => flextype('router')->pathFor('admin.themes.templates.index') . '?theme=' . $theme,
82
                        'title' => __('themes_admin_templates'),
83
84
                    ],
85
                    'templates_add' => [
86
                        'link' => flextype('router')->pathFor('admin.themes.templates.add') . '?theme=' . $theme,
87
                        'title' => __('themes_admin_create_new_template'),
88
                        'active' => true
89
                    ],
90
                ],
91
            ]
92
        );
93
    }
94
95
    /**
96
     * Add template process
97
     *
98
     * @param Request  $request  PSR7 request
99
     * @param Response $response PSR7 response
100
     */
101
    public function addProcess(Request $request, Response $response) : Response
102
    {
103
        // Get data from POST
104
        $post_data = $request->getParsedBody();
105
106
        $id    = $post_data['id'];
107
        $type  = $post_data['type'];
108
        $theme = $post_data['theme'];
109
110
        $file = PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . flextype('slugify')->slugify($id) . '.html';
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\ThemesAdmin\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

110
        $file = PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . /** @scrutinizer ignore-call */ flextype('slugify')->slugify($id) . '.html';
Loading history...
111
112
        if (! Filesystem::has($file)) {
113
            if (Filesystem::write(
114
                $file,
115
                ''
116
            )) {
117
                flextype('flash')->addMessage('success', __('themes_admin_message_' . $type . '_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

117
                flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_created'));
Loading history...
118
            } else {
119
                flextype('flash')->addMessage('error', __('themes_admin_message_' . $type . '_was_not_created'));
120
            }
121
        } else {
122
            flextype('flash')->addMessage('error', __('themes_admin_message_' . $type . '_was_not_created'));
123
        }
124
125
        if (isset($post_data['create-and-edit'])) {
126
            return $response->withRedirect(flextype('router')->pathFor('admin.themes.templates.edit') . '?theme=' . $theme . '&type=' . $type . '&id=' . $id);
127
        }
128
129
        return $response->withRedirect(flextype('router')->pathFor('admin.themes.templates.index') . '?theme=' . $theme);
130
    }
131
132
    /**
133
     * Edit template
134
     *
135
     * @param Request  $request  PSR7 request
136
     * @param Response $response PSR7 response
137
     */
138
    public function edit(Request $request, Response $response) : Response
139
    {
140
        // Get type and theme from request query params
141
        $type  = $request->getQueryParams()['type'];
142
        $theme = $request->getQueryParams()['theme'];
143
144
        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

144
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
145
            $response,
146
            'plugins/themes-admin/templates/extends/themes/templates/edit.html',
147
            [
148
                'menu_item' => 'themes',
149
                'theme' => $theme,
150
                'id' => $request->getQueryParams()['id'],
151
                'data' => Filesystem::read(PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getQueryParams()['id'] . '.html'),
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\ThemesAdmin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
152
                'type' => ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template'),
153
                'links' => [
154
                    'themes' => [
155
                        'link' => flextype('router')->pathFor('admin.themes.index'),
156
                        'title' => __('themes_admin_themes'),
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

156
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
157
158
                    ],
159
                    'templates' => [
160
                        'link' => flextype('router')->pathFor('admin.themes.templates.index') . '?theme=' . $theme,
161
                        'title' => __('themes_admin_templates'),
162
163
                    ],
164
                    'templates_editor' => [
165
                        'link' => flextype('router')->pathFor('admin.themes.templates.edit') . '?id=' . $request->getQueryParams()['id'] . '&type=' . ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template') . '&theme=' . $theme,
166
                        'title' => __('admin_editor'),
167
                        'active' => true
168
                    ],
169
                ],
170
                'buttons' => [
171
                    'save_template' => [
172
                        'link'       => 'javascript:;',
173
                        'title'      => __('admin_save'),
174
                        'type' => 'action',
175
                    ],
176
                ],
177
            ]
178
        );
179
    }
180
181
    /**
182
     * Edit template process
183
     *
184
     * @param Request  $request  PSR7 request
185
     * @param Response $response PSR7 response
186
     */
187
    public function editProcess(Request $request, Response $response) : Response
188
    {
189
        // Get theme and type and id from request query params
190
        $theme = $request->getParsedBody()['theme'];
191
        $id    = $request->getParsedBody()['id'];
192
        $type  = $request->getParsedBody()['type'];
193
194
        if (Filesystem::write(PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()['id'] . '.html', $request->getParsedBody()['data'])) {
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\ThemesAdmin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
195
            flextype('flash')->addMessage('success', __('themes_admin_message_' . $type . '_saved'));
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

195
            flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_saved'));
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

195
            /** @scrutinizer ignore-call */ 
196
            flextype('flash')->addMessage('success', __('themes_admin_message_' . $type . '_saved'));
Loading history...
196
        } else {
197
            flextype('flash')->addMessage('error', __('themes_admin_message_' . $type . '_was_not_saved'));
198
        }
199
200
        return $response->withRedirect(flextype('router')->pathFor('admin.themes.templates.edit') . '?id=' . $id . '&type=' . $type . '&theme=' . $theme);
201
    }
202
203
    /**
204
     * Rename template
205
     *
206
     * @param Request  $request  PSR7 request
207
     * @param Response $response PSR7 response
208
     */
209
    public function rename(Request $request, Response $response) : Response
210
    {
211
        // Get theme from request query params
212
        $theme = $request->getQueryParams()['theme'];
213
214
        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

214
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
215
            $response,
216
            'plugins/themes-admin/templates/extends/themes/templates/rename.html',
217
            [
218
                'menu_item' => 'themes',
219
                'theme' => $theme,
220
                'types' => ['partial' => __('admin_partial'), 'template' => __('admin_template')],
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

220
                'types' => ['partial' => /** @scrutinizer ignore-call */ __('admin_partial'), 'template' => __('admin_template')],
Loading history...
221
                'id_current' => $request->getQueryParams()['id'],
222
                'type_current' => ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template'),
223
                'links' => [
224
                    'themes' => [
225
                        'link' => flextype('router')->pathFor('admin.themes.index'),
226
                        'title' => __('themes_admin_themes'),
227
228
                    ],
229
                    'templates' => [
230
                        'link' => flextype('router')->pathFor('admin.themes.templates.index') . '?theme=' . $theme,
231
                        'title' => __('themes_admin_templates'),
232
233
                    ],
234
                    'templates_rename' => [
235
                        'link' => flextype('router')->pathFor('admin.themes.templates.rename') . '?id=' . $request->getQueryParams()['id'] . '&type=' . ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template') . '&theme=' . $theme,
236
                        'title' => __('admin_rename'),
237
                        'active' => true
238
                    ],
239
                ],
240
            ]
241
        );
242
    }
243
244
    /**
245
     * Rename template process
246
     *
247
     * @param Request  $request  PSR7 request
248
     * @param Response $response PSR7 response
249
     */
250
    public function renameProcess(Request $request, Response $response) : Response
251
    {
252
        // Get theme and type from request query params
253
        $theme = $request->getParsedBody()['theme'];
254
        $type  = $request->getParsedBody()['type_current'];
255
256
        if (! Filesystem::has(PATH['project'] . '/themes/' . flextype('registry')->get('plugins.site.settings.theme') . '/' . $this->_type_location($type) . $request->getParsedBody()['id'] . '.html')) {
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\ThemesAdmin\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

256
        if (! Filesystem::has(PATH['project'] . '/themes/' . /** @scrutinizer ignore-call */ flextype('registry')->get('plugins.site.settings.theme') . '/' . $this->_type_location($type) . $request->getParsedBody()['id'] . '.html')) {
Loading history...
257
            if (Filesystem::rename(
258
                PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()['id_current'] . '.html',
259
                PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()['id'] . '.html'
260
            )
261
            ) {
262
                flextype('flash')->addMessage('success', __('themes_admin_message_' . $type . '_renamed'));
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

262
                flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_renamed'));
Loading history...
263
            } else {
264
                flextype('flash')->addMessage('error', __('themes_admin_message_' . $type . '_was_not_renamed'));
265
            }
266
        } else {
267
            flextype('flash')->addMessage('error', __('themes_admin_message_' . $type . '_was_not_renamed'));
268
        }
269
270
        return $response->withRedirect(flextype('router')->pathFor('admin.themes.templates.index') . '?theme=' . $theme);
271
    }
272
273
    /**
274
     * Delete template process
275
     *
276
     * @param Request  $request  PSR7 request
277
     * @param Response $response PSR7 response
278
     */
279
    public function deleteProcess(Request $request, Response $response) : Response
280
    {
281
        // Get theme and type from request query params
282
        $theme = $request->getParsedBody()['theme'];
283
        $type  = $request->getParsedBody()['type'];
284
285
        $file_path = PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()[$type . '-id'] . '.html';
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\ThemesAdmin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
286
287
        if (Filesystem::delete($file_path)) {
288
            flextype('flash')->addMessage('success', __('themes_admin_message_' . $type . '_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

288
            /** @scrutinizer ignore-call */ 
289
            flextype('flash')->addMessage('success', __('themes_admin_message_' . $type . '_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

288
            flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_deleted'));
Loading history...
289
        } else {
290
            flextype('flash')->addMessage('error', __('themes_admin_message_' . $type . '_was_not_deleted'));
291
        }
292
293
        return $response->withRedirect(flextype('router')->pathFor('admin.themes.templates.index') . '?theme=' . $theme);
294
    }
295
296
    /**
297
     * Duplicate template process
298
     *
299
     * @param Request  $request  PSR7 request
300
     * @param Response $response PSR7 response
301
     */
302
    public function duplicateProcess(Request $request, Response $response) : Response
303
    {
304
        // Get theme and type from request query params
305
        $theme = $request->getParsedBody()['theme'];
306
        $type  = $request->getParsedBody()['type'];
307
308
        $file_path     = PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()[$type . '-id'] . '.html';
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\ThemesAdmin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
309
        $file_path_new = PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()[$type . '-id'] . '-duplicate-' . date('Ymd_His') . '.html';
310
311
        if (Filesystem::copy($file_path, $file_path_new)) {
312
            flextype('flash')->addMessage('success', __('themes_admin_message_' . $type . '_duplicated'));
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

312
            flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_duplicated'));
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

312
            /** @scrutinizer ignore-call */ 
313
            flextype('flash')->addMessage('success', __('themes_admin_message_' . $type . '_duplicated'));
Loading history...
313
        } else {
314
            flextype('flash')->addMessage('error', __('themes_admin_message_' . $type . '_was_not_duplicated'));
315
        }
316
317
        return $response->withRedirect(flextype('router')->pathFor('admin.themes.templates.index') . '?theme=' . $theme);
318
    }
319
320
    private function _type_location($type)
321
    {
322
        if ($type === 'partial') {
323
            $_type = '/templates/partials/';
324
        } else {
325
            $_type = '/templates/';
326
        }
327
328
        return $_type;
329
    }
330
}
331