Passed
Push — master ( c9364f...8e14d3 )
by Sergey
16:34 queued 12s
created

TemplatesController   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 142
c 2
b 0
f 0
dl 0
loc 316
rs 10
wmc 27

9 Methods

Rating   Name   Duplication   Size   Complexity  
A editProcess() 0 14 2
A add() 0 26 1
A rename() 0 29 5
A deleteProcess() 0 15 2
A addProcess() 0 29 4
A edit() 0 37 5
A renameProcess() 0 21 3
A index() 0 29 1
A duplicateProcess() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype\Plugin\ThemesAdmin\Controllers;
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 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
use Flextype\App\Foundation\Container;
0 ignored issues
show
Bug introduced by
The type Flextype\App\Foundation\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...
14
15
/**
16
 * @property twig $twig
17
 * @property Router $router
18
 * @property Cache $cache
19
 * @property Themes $themes
20
 * @property Slugify $slugify
21
 * @property Flash $flash
22
 */
23
class TemplatesController extends Container
24
{
25
    /**
26
     * Index page
27
     *
28
     * @param Request  $request  PSR7 request
29
     * @param Response $response PSR7 response
30
     */
31
    public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
32
    {
33
        // Get theme from request query params
34
        $theme = $request->getQueryParams()['theme'];
35
36
        return $this->twig->render(
37
            $response,
38
            'plugins/themes-admin/templates/extends/themes/templates/index.html',
39
            [
40
                'menu_item' => 'themes',
41
                'theme' => $theme,
42
                'templates_list' => $this->themes->getTemplates($theme),
43
                'partials_list' => $this->themes->getPartials($theme),
44
                'links' =>  [
45
                    'themes' => [
46
                        'link' => $this->router->pathFor('admin.themes.index'),
47
                        '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

47
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
48
49
                    ],
50
                    'templates' => [
51
                        'link' => $this->router->pathFor('admin.templates.index') . '?theme=' . $theme,
52
                        'title' => __('themes_admin_templates'),
53
                        'active' => true
54
                    ],
55
                ],
56
                'buttons' => [
57
                    'templates_create' => [
58
                        'link' => $this->router->pathFor('admin.templates.add') . '?theme=' . $theme,
59
                        'title' => __('themes_admin_create_new_template'),
60
61
                    ],
62
                ],
63
            ]
64
        );
65
    }
66
67
    /**
68
     * Add template
69
     *
70
     * @param Request  $request  PSR7 request
71
     * @param Response $response PSR7 response
72
     */
73
    public function add(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
74
    {
75
        // Get theme from request query params
76
        $theme = $request->getQueryParams()['theme'];
77
78
        return $this->twig->render(
79
            $response,
80
            'plugins/themes-admin/templates/extends/themes/templates/add.html',
81
            [
82
                'menu_item' => 'themes',
83
                'theme' => $theme,
84
                'links' =>  [
85
                    'themes' => [
86
                        'link' => $this->router->pathFor('admin.themes.index'),
87
                        '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

87
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
88
89
                    ],
90
                    'templates' => [
91
                        'link' => $this->router->pathFor('admin.templates.index') . '?theme=' . $theme,
92
                        'title' => __('themes_admin_templates'),
93
94
                    ],
95
                    'templates_add' => [
96
                        'link' => $this->router->pathFor('admin.templates.add') . '?theme=' . $theme,
97
                        'title' => __('themes_admin_create_new_template'),
98
                        'active' => true
99
                    ],
100
                ],
101
            ]
102
        );
103
    }
104
105
    /**
106
     * Add template process
107
     *
108
     * @param Request  $request  PSR7 request
109
     * @param Response $response PSR7 response
110
     */
111
    public function addProcess(Request $request, Response $response) : Response
112
    {
113
        // Get data from POST
114
        $post_data = $request->getParsedBody();
115
116
        $id    = $post_data['id'];
117
        $type  = $post_data['type'];
118
        $theme = $post_data['theme'];
119
120
        $file = PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $this->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...
121
122
        if (! Filesystem::has($file)) {
123
            if (Filesystem::write(
124
                $file,
125
                ''
126
            )) {
127
                $this->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

127
                $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_created'));
Loading history...
128
            } else {
129
                $this->flash->addMessage('error', __('themes_admin_message_' . $type . '_was_not_created'));
130
            }
131
        } else {
132
            $this->flash->addMessage('error', __('themes_admin_message_' . $type . '_was_not_created'));
133
        }
134
135
        if (isset($post_data['create-and-edit'])) {
136
            return $response->withRedirect($this->router->pathFor('admin.templates.edit') . '?theme=' . $theme . '&type=' . $type . '&id=' . $id);
137
        }
138
139
        return $response->withRedirect($this->router->pathFor('admin.templates.index') . '?theme=' . $theme);
140
    }
141
142
    /**
143
     * Edit template
144
     *
145
     * @param Request  $request  PSR7 request
146
     * @param Response $response PSR7 response
147
     */
148
    public function edit(Request $request, Response $response) : Response
149
    {
150
        // Get type and theme from request query params
151
        $type  = $request->getQueryParams()['type'];
152
        $theme = $request->getQueryParams()['theme'];
153
154
        return $this->twig->render(
155
            $response,
156
            'plugins/themes-admin/templates/extends/themes/templates/edit.html',
157
            [
158
                'menu_item' => 'themes',
159
                'theme' => $theme,
160
                'id' => $request->getQueryParams()['id'],
161
                '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...
162
                'type' => ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template'),
163
                'links' => [
164
                    'themes' => [
165
                        'link' => $this->router->pathFor('admin.themes.index'),
166
                        '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

166
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
167
168
                    ],
169
                    'templates' => [
170
                        'link' => $this->router->pathFor('admin.templates.index') . '?theme=' . $theme,
171
                        'title' => __('themes_admin_templates'),
172
173
                    ],
174
                    'templates_editor' => [
175
                        'link' => $this->router->pathFor('admin.templates.edit') . '?id=' . $request->getQueryParams()['id'] . '&type=' . ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template') . '&theme=' . $theme,
176
                        'title' => __('admin_editor'),
177
                        'active' => true
178
                    ],
179
                ],
180
                'buttons' => [
181
                    'save_template' => [
182
                        'link'       => 'javascript:;',
183
                        'title'      => __('admin_save'),
184
                        'type' => 'action',
185
                    ],
186
                ],
187
            ]
188
        );
189
    }
190
191
    /**
192
     * Edit template process
193
     *
194
     * @param Request  $request  PSR7 request
195
     * @param Response $response PSR7 response
196
     */
197
    public function editProcess(Request $request, Response $response) : Response
198
    {
199
        // Get theme and type and id from request query params
200
        $theme = $request->getParsedBody()['theme'];
201
        $id    = $request->getParsedBody()['id'];
202
        $type  = $request->getParsedBody()['type'];
203
204
        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...
205
            $this->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

205
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_saved'));
Loading history...
206
        } else {
207
            $this->flash->addMessage('error', __('themes_admin_message_' . $type . '_was_not_saved'));
208
        }
209
210
        return $response->withRedirect($this->router->pathFor('admin.templates.edit') . '?id=' . $id . '&type=' . $type . '&theme=' . $theme);
211
    }
212
213
    /**
214
     * Rename template
215
     *
216
     * @param Request  $request  PSR7 request
217
     * @param Response $response PSR7 response
218
     */
219
    public function rename(Request $request, Response $response) : Response
220
    {
221
        // Get theme from request query params
222
        $theme = $request->getQueryParams()['theme'];
223
224
        return $this->twig->render(
225
            $response,
226
            'plugins/themes-admin/templates/extends/themes/templates/rename.html',
227
            [
228
                'menu_item' => 'themes',
229
                'theme' => $theme,
230
                '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

230
                'types' => ['partial' => /** @scrutinizer ignore-call */ __('admin_partial'), 'template' => __('admin_template')],
Loading history...
231
                'id_current' => $request->getQueryParams()['id'],
232
                'type_current' => ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template'),
233
                'links' => [
234
                    'themes' => [
235
                        'link' => $this->router->pathFor('admin.themes.index'),
236
                        'title' => __('themes_admin_themes'),
237
238
                    ],
239
                    'templates' => [
240
                        'link' => $this->router->pathFor('admin.templates.index') . '?theme=' . $theme,
241
                        'title' => __('themes_admin_templates'),
242
243
                    ],
244
                    'templates_rename' => [
245
                        'link' => $this->router->pathFor('admin.templates.rename') . '?id=' . $request->getQueryParams()['id'] . '&type=' . ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template') . '&theme=' . $theme,
246
                        'title' => __('admin_rename'),
247
                        'active' => true
248
                    ],
249
                ],
250
            ]
251
        );
252
    }
253
254
    /**
255
     * Rename template process
256
     *
257
     * @param Request  $request  PSR7 request
258
     * @param Response $response PSR7 response
259
     */
260
    public function renameProcess(Request $request, Response $response) : Response
261
    {
262
        // Get theme and type from request query params
263
        $theme = $request->getParsedBody()['theme'];
264
        $type  = $request->getParsedBody()['type_current'];
265
266
        if (! Filesystem::has(PATH['project'] . '/themes/' . $this->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...
267
            if (Filesystem::rename(
268
                PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()['id_current'] . '.html',
269
                PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()['id'] . '.html'
270
            )
271
            ) {
272
                $this->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

272
                $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_renamed'));
Loading history...
273
            } else {
274
                $this->flash->addMessage('error', __('themes_admin_message_' . $type . '_was_not_renamed'));
275
            }
276
        } else {
277
            $this->flash->addMessage('error', __('themes_admin_message_' . $type . '_was_not_renamed'));
278
        }
279
280
        return $response->withRedirect($this->router->pathFor('admin.templates.index') . '?theme=' . $theme);
281
    }
282
283
    /**
284
     * Delete template process
285
     *
286
     * @param Request  $request  PSR7 request
287
     * @param Response $response PSR7 response
288
     */
289
    public function deleteProcess(Request $request, Response $response) : Response
290
    {
291
        // Get theme and type from request query params
292
        $theme = $request->getParsedBody()['theme'];
293
        $type  = $request->getParsedBody()['type'];
294
295
        $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...
296
297
        if (Filesystem::delete($file_path)) {
298
            $this->flash->addMessage('success', __('themes_admin_message_' . $type . '_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

298
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_deleted'));
Loading history...
299
        } else {
300
            $this->flash->addMessage('error', __('themes_admin_message_' . $type . '_was_not_deleted'));
301
        }
302
303
        return $response->withRedirect($this->router->pathFor('admin.templates.index') . '?theme=' . $theme);
304
    }
305
306
    /**
307
     * Duplicate template process
308
     *
309
     * @param Request  $request  PSR7 request
310
     * @param Response $response PSR7 response
311
     */
312
    public function duplicateProcess(Request $request, Response $response) : Response
313
    {
314
        // Get theme and type from request query params
315
        $theme = $request->getParsedBody()['theme'];
316
        $type  = $request->getParsedBody()['type'];
317
318
        $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...
319
        $file_path_new = PATH['project'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()[$type . '-id'] . '-duplicate-' . date('Ymd_His') . '.html';
320
321
        if (Filesystem::copy($file_path, $file_path_new)) {
322
            $this->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

322
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_' . $type . '_duplicated'));
Loading history...
323
        } else {
324
            $this->flash->addMessage('error', __('themes_admin_message_' . $type . '_was_not_duplicated'));
325
        }
326
327
        return $response->withRedirect($this->router->pathFor('admin.templates.index') . '?theme=' . $theme);
328
    }
329
330
    private function _type_location($type)
331
    {
332
        if ($type === 'partial') {
333
            $_type = '/templates/partials/';
334
        } else {
335
            $_type = '/templates/';
336
        }
337
338
        return $_type;
339
    }
340
}
341