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

ThemesAdminController::settingsProcess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
nc 2
nop 2
dl 0
loc 16
c 1
b 0
f 0
cc 2
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype\Plugin\ThemesAdmin\Controllers;
6
7
use Flextype\Component\Arrays\Arrays;
8
use Flextype\Component\Filesystem\Filesystem;
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 function array_merge;
12
use function array_replace_recursive;
13
use function count;
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 is_array;
16
use function trim;
17
18
class ThemesAdminController
19
{
20
    /**
21
     * Index page
22
     *
23
     * @param Request  $request  PSR7 request
24
     * @param Response $response PSR7 response
25
     */
26
    public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
27
    {
28
        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

28
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
29
            $response,
30
            'plugins/themes-admin/templates/extends/themes/index.html',
31
            [
32
                'menu_item' => 'themes',
33
                'themes_list' => flextype('registry')->get('themes'),
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
                        'active' => true
39
                    ],
40
                ],
41
                'buttons' => [
42
                    'themes_get_more' => [
43
                        'link' => 'https://flextype.org/en/downloads/extend/themes',
44
                        'title' => __('themes_admin_get_more_themes'),
45
                        'target' => '_blank',
46
                    ],
47
                ],
48
            ]
49
        );
50
    }
51
52
    /**
53
     * Сhange theme status process
54
     *
55
     * @param Request  $request  PSR7 request
56
     * @param Response $response PSR7 response
57
     */
58
    public function activateProcess(Request $request, Response $response) : Response
59
    {
60
        // Get data from the request
61
        $post_data = $request->getParsedBody();
62
63
        $custom_settings_file = PATH['project'] . '/config/plugins/site/settings.yaml';
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...
64
        $custom_settings_file_data = flextype('serializers')->yaml()->decode(Filesystem::read($custom_settings_file));
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

64
        $custom_settings_file_data = /** @scrutinizer ignore-call */ flextype('serializers')->yaml()->decode(Filesystem::read($custom_settings_file));
Loading history...
65
66
        Arrays::set($custom_settings_file_data, 'theme', $post_data['theme-id']);
67
68
        Filesystem::write($custom_settings_file, flextype('serializers')->yaml()->encode($custom_settings_file_data));
69
70
        // clear cache
71
        Filesystem::deleteDir(PATH['tmp'] . '/data');
72
73
        // Redirect to themes index page
74
        return $response->withRedirect(flextype('router')->pathFor('admin.themes.index'));
75
    }
76
77
    /**
78
     * Theme information
79
     *
80
     * @param Request  $request  PSR7 request
81
     * @param Response $response PSR7 response
82
     */
83
    public function information(Request $request, Response $response) : Response
84
    {
85
        // Get Theme ID
86
        $id = $request->getQueryParams()['id'];
87
88
        // Set theme custom manifest content
89
        $custom_theme_manifest_file = PATH['project'] . '/themes/' . $id . '/theme.yaml';
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...
90
91
        // Get theme custom manifest content
92
        $custom_theme_manifest_file_content = Filesystem::read($custom_theme_manifest_file);
93
94
        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

94
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
95
            $response,
96
            'plugins/themes-admin/templates/extends/themes/information.html',
97
            [
98
                'menu_item' => 'themes',
99
                'id' => $id,
100
                'theme_manifest' => flextype('serializers')->yaml()->decode($custom_theme_manifest_file_content),
101
                'links' =>  [
102
                    'themes' => [
103
                        'link' => flextype('router')->pathFor('admin.themes.index'),
104
                        '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

104
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
105
                    ],
106
                    'themes_information' => [
107
                        'link' => flextype('router')->pathFor('admin.themes.information') . '?id=' . $request->getQueryParams()['id'],
108
                        'title' => __('admin_information'),
109
                        'active' => true
110
                    ],
111
                ],
112
            ]
113
        );
114
    }
115
116
    /**
117
     * Theme settings
118
     *
119
     * @param Request  $request  PSR7 request
120
     * @param Response $response PSR7 response
121
     */
122
    public function settings(Request $request, Response $response) : Response
123
    {
124
        // Get Theme ID
125
        $id = $request->getQueryParams()['id'];
126
127
        // Set theme config
128
        $custom_theme_settings_file = PATH['project'] . '/config/themes/' . $id . '/settings.yaml';
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...
129
130
        // Get theme settings content
131
        $custom_theme_settings_file_content = Filesystem::read($custom_theme_settings_file);
132
133
        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

133
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
134
            $response,
135
            'plugins/themes-admin/templates/extends/themes/settings.html',
136
            [
137
                'menu_item' => 'themes',
138
                'id' => $id,
139
                'theme_settings' => $custom_theme_settings_file_content,
140
                'links' =>  [
141
                    'themes' => [
142
                        'link' => flextype('router')->pathFor('admin.themes.index'),
143
                        '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

143
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
144
                    ],
145
                    'themes_settings' => [
146
                        'link' => flextype('router')->pathFor('admin.themes.settings') . '?id=' . $request->getQueryParams()['id'],
147
                        'title' => __('admin_settings'),
148
                        'active' => true
149
                    ],
150
                ],
151
                'buttons' => [
152
                    'save_theme_settings' => [
153
                        'link' => 'javascript:;',
154
                        'title' => __('admin_save'),
155
                        'type' => 'action',
156
                    ],
157
                ],
158
            ]
159
        );
160
    }
161
162
    /**
163
     * Theme settings process
164
     *
165
     * @param Request  $request  PSR7 request
166
     * @param Response $response PSR7 response
167
     */
168
    public function settingsProcess(Request $request, Response $response) : Response
169
    {
170
        $post_data = $request->getParsedBody();
171
172
        $id   = $post_data['id'];
173
        $data = $post_data['data'];
174
175
        $custom_theme_settings_file = PATH['project'] . '/config/themes/' . $id . '/settings.yaml';
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...
176
177
        if (Filesystem::write($custom_theme_settings_file, $data)) {
178
            flextype('flash')->addMessage('success', __('themes_admin_message_theme_settings_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

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

178
            /** @scrutinizer ignore-call */ 
179
            flextype('flash')->addMessage('success', __('themes_admin_message_theme_settings_saved'));
Loading history...
179
        } else {
180
            flextype('flash')->addMessage('error', __('themes_admin_message_theme_settings_not_saved'));
181
        }
182
183
        return $response->withRedirect(flextype('router')->pathFor('admin.themes.settings') . '?id=' . $id);
184
    }
185
}
186