Passed
Push — master ( 3d316c...cbe4a6 )
by Sergey
07:19 queued 10s
created

ThemesController::settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 34
rs 9.568
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype;
6
7
use Flextype\Component\Arr\Arr;
0 ignored issues
show
Bug introduced by
The type Flextype\Component\Arr\Arr 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\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...
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
/**
19
 * @property twig $twig
20
 * @property Router $router
21
 * @property Cache $cache
22
 * @property Themes $themes
23
 */
24
class ThemesController 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...
25
{
26
    /**
27
     * Index page
28
     *
29
     * @param Request  $request  PSR7 request
30
     * @param Response $response PSR7 response
31
     */
32
    public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
33
    {
34
        return $this->twig->render(
35
            $response,
36
            'plugins/themes-admin/templates/extends/themes/index.html',
37
            [
38
                'menu_item' => 'themes',
39
                'themes_list' => $this->registry->get('themes'),
40
                'links' =>  [
41
                    'themes' => [
42
                        'link' => $this->router->pathFor('admin.themes.index'),
43
                        '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

43
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
44
                        'active' => true
45
                    ],
46
                ],
47
                'buttons' => [
48
                    'themes_get_more' => [
49
                        'link' => 'https://github.com/flextype/themes',
50
                        'title' => __('themes_admin_get_more_themes'),
51
                        'target' => '_blank',
52
                    ],
53
                ],
54
            ]
55
        );
56
    }
57
58
    /**
59
     * Сhange theme status process
60
     *
61
     * @param Request  $request  PSR7 request
62
     * @param Response $response PSR7 response
63
     */
64
    public function activateProcess(Request $request, Response $response) : Response
65
    {
66
        // Get data from the request
67
        $post_data = $request->getParsedBody();
68
69
        $custom_settings_file = PATH['project'] . '/config/plugins/site/settings.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...
70
        $custom_settings_file_data = $this->serializer->decode(Filesystem::read($custom_settings_file), 'yaml');
71
72
        Arr::set($custom_settings_file_data, 'theme', $post_data['theme-id']);
73
74
        Filesystem::write($custom_settings_file, $this->serializer->encode($custom_settings_file_data, 'yaml'));
75
76
        // clear cache
77
        $this->cache->clear('doctrine');
78
79
        // Redirect to themes index page
80
        return $response->withRedirect($this->router->pathFor('admin.themes.index'));
81
    }
82
83
    /**
84
     * Theme information
85
     *
86
     * @param Request  $request  PSR7 request
87
     * @param Response $response PSR7 response
88
     */
89
    public function information(Request $request, Response $response) : Response
90
    {
91
        // Get Theme ID
92
        $id = $request->getQueryParams()['id'];
93
94
        // Set theme custom manifest content
95
        $custom_theme_manifest_file = PATH['project'] . '/themes/' . $id . '/theme.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...
96
97
        // Get theme custom manifest content
98
        $custom_theme_manifest_file_content = Filesystem::read($custom_theme_manifest_file);
99
100
        return $this->twig->render(
101
            $response,
102
            'plugins/themes-admin/templates/extends/themes/information.html',
103
            [
104
                'menu_item' => 'themes',
105
                'id' => $id,
106
                'theme_manifest' => $this->serializer->decode($custom_theme_manifest_file_content, 'yaml'),
107
                'links' =>  [
108
                    'themes' => [
109
                        'link' => $this->router->pathFor('admin.themes.index'),
110
                        '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

110
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
111
                    ],
112
                    'themes_information' => [
113
                        'link' => $this->router->pathFor('admin.themes.information') . '?id=' . $request->getQueryParams()['id'],
114
                        'title' => __('admin_information'),
115
                        'active' => true
116
                    ],
117
                ],
118
            ]
119
        );
120
    }
121
122
    /**
123
     * Theme settings
124
     *
125
     * @param Request  $request  PSR7 request
126
     * @param Response $response PSR7 response
127
     */
128
    public function settings(Request $request, Response $response) : Response
129
    {
130
        // Get Theme ID
131
        $id = $request->getQueryParams()['id'];
132
133
        // Set theme config
134
        $custom_theme_settings_file = PATH['project'] . '/config/themes/' . $id . '/settings.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...
135
136
        // Get theme settings content
137
        $custom_theme_settings_file_content = Filesystem::read($custom_theme_settings_file);
138
139
        return $this->twig->render(
140
            $response,
141
            'plugins/themes-admin/templates/extends/themes/settings.html',
142
            [
143
                'menu_item' => 'themes',
144
                'id' => $id,
145
                'theme_settings' => $custom_theme_settings_file_content,
146
                'links' =>  [
147
                    'themes' => [
148
                        'link' => $this->router->pathFor('admin.themes.index'),
149
                        '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

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

184
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('themes_admin_message_theme_settings_saved'));
Loading history...
185
        } else {
186
            $this->flash->addMessage('error', __('themes_admin_message_theme_settings_not_saved'));
187
        }
188
189
        return $response->withRedirect($this->router->pathFor('admin.themes.settings') . '?id=' . $id);
190
    }
191
}
192