Passed
Push — dev ( e66ec2...f81248 )
by Sergey
12:00 queued 11s
created

PluginsController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 74
c 0
b 0
f 0
dl 0
loc 175
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A settings() 0 34 1
A pluginStatusProcess() 0 19 2
A index() 0 25 1
A information() 0 28 1
A settingsProcess() 0 17 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype\Plugin\Admin\Controllers;
6
7
use Flextype\Component\Arrays\Arrays;
0 ignored issues
show
Bug introduced by
The type Flextype\Component\Arrays\Arrays 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
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...
16
17
/**
18
 * @property View $view
19
 * @property Router $router
20
 * @property Cache $cache
21
 * @property Registry $registry
22
 */
23
class PluginsController 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
34
        $plugins_list = $this->registry->get('plugins');
35
36
        ksort($plugins_list);
37
38
        return $this->twig->render(
39
            $response,
40
            'plugins/admin/templates/extends/plugins/index.html',
41
            [
42
                'plugins_list' => $plugins_list,
43
                'menu_item' => 'plugins',
44
                'links' =>  [
45
                    'plugins' => [
46
                        'link' => $this->router->pathFor('admin.plugins.index'),
47
                        '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

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

116
                        'title' => /** @scrutinizer ignore-call */ __('admin_plugins'),
Loading history...
117
118
                    ],
119
                    'plugins_information' => [
120
                        'link' => $this->router->pathFor('admin.plugins.information') . '?id=' . $request->getQueryParams()['id'],
121
                        'title' => __('admin_information'),
122
                        'active' => true
123
                    ],
124
                ],
125
            ]
126
        );
127
    }
128
129
    /**
130
     * Plugin settings
131
     *
132
     * @param Request  $request  PSR7 request
133
     * @param Response $response PSR7 response
134
     */
135
    public function settings(Request $request, Response $response) : Response
136
    {
137
        // Get Plugin ID
138
        $id = $request->getQueryParams()['id'];
139
140
        // Set plugin custom setting file
141
        $custom_plugin_settings_file = PATH['project'] . '/config/' . '/plugins/' . $id . '/settings.yaml';
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
142
143
        // Get plugin custom setting file content
144
        $custom_plugin_settings_file_content = Filesystem::read($custom_plugin_settings_file);
145
146
        return $this->twig->render(
147
            $response,
148
            'plugins/admin/templates/extends/plugins/settings.html',
149
            [
150
                'menu_item' => 'plugins',
151
                'id' => $id,
152
                'plugin_settings' => $custom_plugin_settings_file_content,
153
                'links' =>  [
154
                    'plugins' => [
155
                        'link' => $this->router->pathFor('admin.plugins.index'),
156
                        '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

156
                        'title' => /** @scrutinizer ignore-call */ __('admin_plugins'),
Loading history...
157
                    ],
158
                    'plugins_settings' => [
159
                        'link' => $this->router->pathFor('admin.plugins.settings') . '?id=' . $request->getQueryParams()['id'],
160
                        'title' => __('admin_settings'),
161
                        'active' => true
162
                    ],
163
                ],
164
                'buttons' => [
165
                    'save_plugin_settings' => [
166
                        'link' => 'javascript:;',
167
                        'title' => __('admin_save'),
168
                        'type' => 'action'
169
                    ],
170
                ],
171
            ]
172
        );
173
    }
174
175
    /**
176
     * Plugin settings process
177
     *
178
     * @param Request  $request  PSR7 request
179
     * @param Response $response PSR7 response
180
     */
181
    public function settingsProcess(Request $request, Response $response) : Response
182
    {
183
        $post_data = $request->getParsedBody();
184
185
        $id   = $post_data['id'];
186
        $data = $post_data['data'];
187
188
        $custom_plugin_settings_dir  = PATH['project'] . '/config/' . '/plugins/' . $id;
0 ignored issues
show
Unused Code introduced by
The assignment to $custom_plugin_settings_dir is dead and can be removed.
Loading history...
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
189
        $custom_plugin_settings_file = PATH['project'] . '/config/' . '/plugins/' . $id . '/settings.yaml';
190
191
        if (Filesystem::write($custom_plugin_settings_file, $data)) {
192
            $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

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