PluginsController::settingsProcess()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 17
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype\Plugin\Admin\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 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
class PluginsController
17
{
18
    /**
19
     * Index page
20
     *
21
     * @param Request  $request  PSR7 request
22
     * @param Response $response PSR7 response
23
     */
24
    public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
25
    {
26
27
        $plugins_list = flextype('registry')->get('plugins');
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

27
        $plugins_list = /** @scrutinizer ignore-call */ flextype('registry')->get('plugins');
Loading history...
28
29
        ksort($plugins_list);
30
31
        return flextype('twig')->render(
32
            $response,
33
            'plugins/admin/templates/extends/plugins/index.html',
34
            [
35
                'plugins_list' => $plugins_list,
36
                'menu_item' => 'plugins',
37
                'links' =>  [
38
                    'plugins' => [
39
                        'link' => flextype('router')->pathFor('admin.plugins.index'),
40
                        '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

40
                        'title' => /** @scrutinizer ignore-call */ __('admin_plugins'),
Loading history...
41
                        'active' => true
42
                    ],
43
                ],
44
                'buttons' =>  [
45
                    'plugins_get_more' => [
46
                        'link' => 'https://flextype.org/en/downloads/extend/plugins',
47
                        'title' => __('admin_get_more_plugins'),
48
                        'target' => '_blank'
49
                    ],
50
                ],
51
            ]
52
        );
53
    }
54
55
    /**
56
     * Сhange plugin status process
57
     *
58
     * @param Request  $request  PSR7 request
59
     * @param Response $response PSR7 response
60
     */
61
    public function pluginStatusProcess(Request $request, Response $response) : Response
62
    {
63
        // Get data from the request
64
        $post_data = $request->getParsedBody();
65
66
        $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...
67
        $custom_plugin_settings_file_content = Filesystem::read($custom_plugin_settings_file);
68
        $custom_plugin_settings_file_data = empty($custom_plugin_settings_file_content) ? [] : flextype('serializers')->yaml()->decode($custom_plugin_settings_file_content);
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

68
        $custom_plugin_settings_file_data = empty($custom_plugin_settings_file_content) ? [] : /** @scrutinizer ignore-call */ flextype('serializers')->yaml()->decode($custom_plugin_settings_file_content);
Loading history...
69
70
        $status = ($post_data['plugin-set-status'] == 'true') ? true : false;
71
72
        Arrays::set($custom_plugin_settings_file_data, 'enabled', $status);
73
74
        Filesystem::write($custom_plugin_settings_file, flextype('serializers')->yaml()->encode($custom_plugin_settings_file_data));
75
76
        // clear cache
77
        Filesystem::deleteDir(PATH['tmp'] . '/data');
78
79
        // Redirect to plugins index page
80
        return $response->withRedirect(flextype('router')->pathFor('admin.plugins.index'));
81
    }
82
83
    /**
84
     * Plugin 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 Plugin ID
92
        $id = $request->getQueryParams()['id'];
93
94
        // Set plugin custom manifest content
95
        $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...
96
97
        // Get plugin custom manifest content
98
        $custom_plugin_manifest_file_content = Filesystem::read($custom_plugin_manifest_file);
99
100
        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

100
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
101
            $response,
102
            'plugins/admin/templates/extends/plugins/information.html',
103
            [
104
                'menu_item' => 'plugins',
105
                'id' => $id,
106
                'plugin_manifest' => flextype('serializers')->yaml()->decode($custom_plugin_manifest_file_content),
107
                'links' =>  [
108
                    'plugins' => [
109
                        'link' => flextype('router')->pathFor('admin.plugins.index'),
110
                        '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

110
                        'title' => /** @scrutinizer ignore-call */ __('admin_plugins'),
Loading history...
111
112
                    ],
113
                    'plugins_information' => [
114
                        'link' => flextype('router')->pathFor('admin.plugins.information') . '?id=' . $request->getQueryParams()['id'],
115
                        'title' => __('admin_information'),
116
                        'active' => true
117
                    ],
118
                ],
119
            ]
120
        );
121
    }
122
123
    /**
124
     * Plugin settings
125
     *
126
     * @param Request  $request  PSR7 request
127
     * @param Response $response PSR7 response
128
     */
129
    public function settings(Request $request, Response $response) : Response
130
    {
131
        // Get Plugin ID
132
        $id = $request->getQueryParams()['id'];
133
134
        // Set plugin custom setting file
135
        $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...
136
137
        // Get plugin custom setting file content
138
        $custom_plugin_settings_file_content = Filesystem::read($custom_plugin_settings_file);
139
140
        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

140
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
141
            $response,
142
            'plugins/admin/templates/extends/plugins/settings.html',
143
            [
144
                'menu_item' => 'plugins',
145
                'id' => $id,
146
                'plugin_settings' => $custom_plugin_settings_file_content,
147
                'links' =>  [
148
                    'plugins' => [
149
                        'link' => flextype('router')->pathFor('admin.plugins.index'),
150
                        '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

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

186
            /** @scrutinizer ignore-call */ 
187
            flextype('flash')->addMessage('success', __('admin_message_plugin_settings_saved'));
Loading history...
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

186
            flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_plugin_settings_saved'));
Loading history...
187
        } else {
188
            flextype('flash')->addMessage('error', __('admin_message_plugin_settings_not_saved'));
189
        }
190
191
        return $response->withRedirect(flextype('router')->pathFor('admin.plugins.settings') . '?id=' . $id);
192
    }
193
}
194