Passed
Branch dev (4236f0)
by Sergey
10:08
created

PluginsController::settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 2
dl 0
loc 34
rs 9.568
c 0
b 0
f 0
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 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...
14
use function trim;
15
16
/**
17
 * @property View $view
18
 * @property Router $router
19
 * @property Cache $cache
20
 * @property Registry $registry
21
 */
22
class PluginsController 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...
23
{
24
    /**
25
     * Index page
26
     *
27
     * @param Request  $request  PSR7 request
28
     * @param Response $response PSR7 response
29
     */
30
    public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
31
    {
32
33
        $plugins_list = $this->registry->get('plugins');
34
35
        ksort($plugins_list);
36
37
        return $this->twig->render(
38
            $response,
39
            'plugins/admin/templates/extends/plugins/index.html',
40
            [
41
                'plugins_list' => $plugins_list,
42
                'menu_item' => 'plugins',
43
                'links' =>  [
44
                    'plugins' => [
45
                        'link' => $this->router->pathFor('admin.plugins.index'),
46
                        'title' => __('admin_plugins'),
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

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

115
                        'title' => /** @scrutinizer ignore-call */ __('admin_plugins'),
Loading history...
116
117
                    ],
118
                    'plugins_information' => [
119
                        'link' => $this->router->pathFor('admin.plugins.information') . '?id=' . $request->getQueryParams()['id'],
120
                        'title' => __('admin_information'),
121
                        'active' => true
122
                    ],
123
                ],
124
            ]
125
        );
126
    }
127
128
    /**
129
     * Plugin settings
130
     *
131
     * @param Request  $request  PSR7 request
132
     * @param Response $response PSR7 response
133
     */
134
    public function settings(Request $request, Response $response) : Response
135
    {
136
        // Get Plugin ID
137
        $id = $request->getQueryParams()['id'];
138
139
        // Set plugin custom setting file
140
        $custom_plugin_settings_file = PATH['site'] . '/config/' . '/plugins/' . $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...
141
142
        // Get plugin custom setting file content
143
        $custom_plugin_settings_file_content = Filesystem::read($custom_plugin_settings_file);
144
145
        return $this->twig->render(
146
            $response,
147
            'plugins/admin/templates/extends/plugins/settings.html',
148
            [
149
                'menu_item' => 'plugins',
150
                'id' => $id,
151
                'plugin_settings' => $custom_plugin_settings_file_content,
152
                'links' =>  [
153
                    'plugins' => [
154
                        'link' => $this->router->pathFor('admin.plugins.index'),
155
                        'title' => __('admin_plugins'),
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

155
                        'title' => /** @scrutinizer ignore-call */ __('admin_plugins'),
Loading history...
156
                    ],
157
                    'plugins_settings' => [
158
                        'link' => $this->router->pathFor('admin.plugins.settings') . '?id=' . $request->getQueryParams()['id'],
159
                        'title' => __('admin_settings'),
160
                        'active' => true
161
                    ],
162
                ],
163
                'buttons' => [
164
                    'save_plugin_settings' => [
165
                        'link' => 'javascript:;',
166
                        'title' => __('admin_save'),
167
                        'type' => 'action'
168
                    ],
169
                ],
170
            ]
171
        );
172
    }
173
174
    /**
175
     * Plugin settings process
176
     *
177
     * @param Request  $request  PSR7 request
178
     * @param Response $response PSR7 response
179
     */
180
    public function settingsProcess(Request $request, Response $response) : Response
181
    {
182
        $post_data = $request->getParsedBody();
183
184
        $id   = $post_data['id'];
185
        $data = $post_data['data'];
186
187
        $custom_plugin_settings_dir  = PATH['site'] . '/config/' . '/plugins/' . $id;
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...
Unused Code introduced by
The assignment to $custom_plugin_settings_dir is dead and can be removed.
Loading history...
188
        $custom_plugin_settings_file = PATH['site'] . '/config/' . '/plugins/' . $id . '/settings.yaml';
189
190
        if (Filesystem::write($custom_plugin_settings_file, $data)) {
191
            $this->flash->addMessage('success', __('admin_message_plugin_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

191
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_plugin_settings_saved'));
Loading history...
192
        } else {
193
            $this->flash->addMessage('error', __('admin_message_plugin_settings_not_saved'));
194
        }
195
196
        return $response->withRedirect($this->router->pathFor('admin.plugins.settings') . '?id=' . $id);
197
    }
198
}
199