Test Failed
Push — main ( f24735...83b0dc )
by Rafael
11:53
created

Modules::enableMethod()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 0
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 20
1
<?php
2
/**
3
 * Copyright (C) 2022-2023  Rafael San José Tovar   <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Alxarafe\Controllers;
20
21
use Alxarafe\Core\Base\Controller;
22
use Alxarafe\Core\Base\View;
23
use Alxarafe\Core\Helpers\FormatUtils;
0 ignored issues
show
Bug introduced by
The type Alxarafe\Core\Helpers\FormatUtils 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...
24
use Alxarafe\Core\Helpers\Utils\FileSystemUtils;
0 ignored issues
show
Bug introduced by
The type Alxarafe\Core\Helpers\Utils\FileSystemUtils 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
use Alxarafe\Core\Models\Module;
0 ignored issues
show
Bug introduced by
The type Alxarafe\Core\Models\Module 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...
26
use Alxarafe\Core\Providers\FlashMessages;
0 ignored issues
show
Bug introduced by
The type Alxarafe\Core\Providers\FlashMessages 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...
27
use Alxarafe\Core\Providers\ModuleManager;
0 ignored issues
show
Bug introduced by
The type Alxarafe\Core\Providers\ModuleManager 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...
28
use Alxarafe\Views\ModulesView;
29
use Symfony\Component\Finder\Finder;
30
use Symfony\Component\HttpFoundation\Response;
31
32
/**
33
 * Class Init
34
 *
35
 *
36
 *
37
 * @author  Rafael San José Tovar <[email protected]>
38
 * @version 08 2021
39
 *
40
 * @package Modules\Main
41
 */
42
class Modules extends Controller
43
{
44
    /**
45
     * Modules folder were are stored.
46
     *
47
     * @var string
48
     */
49
    private $modulesFolder;
50
51
    /**
52
     * @var array
53
     */
54
    private $modulesList = [];
55
56
    /**
57
     * Modules constructor.
58
     */
59
    public function __construct()
60
    {
61
        parent::__construct(new Module());
0 ignored issues
show
Unused Code introduced by
The call to Alxarafe\Core\Base\Controller::__construct() has too many arguments starting with new Alxarafe\Core\Models\Module(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        parent::/** @scrutinizer ignore-call */ 
62
                __construct(new Module());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
62
        $this->modulesFolder = basePath('src' . constant('DIRECTORY_SEPARATOR') . 'Modules');
0 ignored issues
show
Bug introduced by
The function basePath 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

62
        $this->modulesFolder = /** @scrutinizer ignore-call */ basePath('src' . constant('DIRECTORY_SEPARATOR') . 'Modules');
Loading history...
63
    }
64
65
    /**
66
     * The start point of the controller.
67
     *
68
     * @return Response
69
     */
70
    public function indexMethod(): Response
71
    {
72
        $this->modulesList = $this->getAvailableModules();
73
        $this->updateModulesData();
74
75
        return parent::indexMethod();
0 ignored issues
show
Bug introduced by
The method indexMethod() does not exist on Alxarafe\Core\Base\Controller. It seems like you code against a sub-type of Alxarafe\Core\Base\Controller such as Alxarafe\Controllers\Modules. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        return parent::/** @scrutinizer ignore-call */ indexMethod();
Loading history...
76
    }
77
78
    /**
79
     * Returns a list of availabe modules.
80
     *
81
     * @return array
82
     */
83
    private function getAvailableModules(): array
84
    {
85
        $modules = Finder::create()
86
            ->directories()
87
            ->depth(0)
88
            ->in($this->modulesFolder)
89
            ->sortByName();
90
        $modulesList = [];
91
        foreach ($modules as $module) {
92
            $modulesList[$module->getFileName()] = str_replace(basePath(DIRECTORY_SEPARATOR), '', $module->getPathName());
0 ignored issues
show
Bug introduced by
The function basePath 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

92
            $modulesList[$module->getFileName()] = str_replace(/** @scrutinizer ignore-call */ basePath(DIRECTORY_SEPARATOR), '', $module->getPathName());
Loading history...
93
        }
94
        return $modulesList;
95
    }
96
97
    /**
98
     * Updated all modules to database.
99
     */
100
    private function updateModulesData(): void
101
    {
102
        foreach ($this->modulesList as $name => $path) {
103
            $module = new Module();
104
            if (!$module->getBy('name', $name)) {
105
                $module->name = $name;
106
                $module->path = $path;
107
                $module->updated_date = FormatUtils::getFormatted(FormatUtils::getFormatDateTime());
108
                $module->save();
109
            }
110
        }
111
    }
112
113
    /**
114
     * Default create method for new registers.
115
     *
116
     * @return Response
117
     */
118
    public function createMethod(): Response
119
    {
120
        // TODO: Implement createMethod() method.
121
        // Require allow to submit a file
122
        return parent::createMethod();
0 ignored issues
show
Bug introduced by
The method createMethod() does not exist on Alxarafe\Core\Base\Controller. It seems like you code against a sub-type of Alxarafe\Core\Base\Controller such as Alxarafe\Controllers\Modules. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
        return parent::/** @scrutinizer ignore-call */ createMethod();
Loading history...
123
    }
124
125
    /**
126
     * Default read method for new registers.
127
     *
128
     * @return Response
129
     */
130
    public function readMethod(): Response
131
    {
132
        // TODO: Implement readMethod() method.
133
        // The data can be showed from table.
134
        return parent::readMethod();
0 ignored issues
show
Bug introduced by
The method readMethod() does not exist on Alxarafe\Core\Base\Controller. It seems like you code against a sub-type of Alxarafe\Core\Base\Controller such as Alxarafe\Controllers\Modules. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
        return parent::/** @scrutinizer ignore-call */ readMethod();
Loading history...
135
    }
136
137
    /**
138
     * Default update method for update an individual register.
139
     *
140
     * @return Response
141
     */
142
    public function updateMethod(): Response
143
    {
144
        // TODO: Implement updateMethod() method.
145
        // The data must be updated from each module.
146
        return parent::updateMethod();
0 ignored issues
show
Bug introduced by
The method updateMethod() does not exist on Alxarafe\Core\Base\Controller. It seems like you code against a sub-type of Alxarafe\Core\Base\Controller such as Alxarafe\Controllers\Modules. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
        return parent::/** @scrutinizer ignore-call */ updateMethod();
Loading history...
147
    }
148
149
    /**
150
     * Default delete method for delete an individual register.
151
     *
152
     * @return Response
153
     */
154
    public function deleteMethod(): Response
155
    {
156
        if (!$this->canAccess || !$this->canDelete) {
0 ignored issues
show
Bug Best Practice introduced by
The property canDelete does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property canAccess does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
157
            $this->renderer->setTemplate('master/noaccess');
0 ignored issues
show
Bug Best Practice introduced by
The property renderer does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
158
            return $this->sendResponseTemplate();
0 ignored issues
show
Bug introduced by
The method sendResponseTemplate() does not exist on Alxarafe\Controllers\Modules. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
            return $this->/** @scrutinizer ignore-call */ sendResponseTemplate();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
159
        }
160
        $id = $this->request->query->get($this->model->getIdField());
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
161
        $this->model = new Module();
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
162
        if ($this->model->load($id)) {
163
            $this->model->enabled = 0;
164
            if ($this->model->save()) {
165
                FlashMessages::getInstance()::setSuccess(
166
                    $this->translator->trans('module-disabled', ['%moduleName%' => $this->model->{$this->model->getNameField()}])
0 ignored issues
show
Bug Best Practice introduced by
The property translator does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
167
                );
168
            }
169
        }
170
171
        FileSystemUtils::rrmdir(basePath($this->model->path));
0 ignored issues
show
Bug introduced by
The function basePath 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

171
        FileSystemUtils::rrmdir(/** @scrutinizer ignore-call */ basePath($this->model->path));
Loading history...
172
        return parent::deleteMethod();
0 ignored issues
show
Bug introduced by
The method deleteMethod() does not exist on Alxarafe\Core\Base\Controller. It seems like you code against a sub-type of Alxarafe\Core\Base\Controller such as Alxarafe\Controllers\Modules. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
        return parent::/** @scrutinizer ignore-call */ deleteMethod();
Loading history...
173
    }
174
175
    /**
176
     * Default enable method for enable an individual register.
177
     *
178
     * @return Response
179
     */
180
    public function enableMethod(): Response
181
    {
182
        if (!$this->canAccess || !$this->canUpdate) {
0 ignored issues
show
Bug Best Practice introduced by
The property canAccess does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property canUpdate does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
183
            $this->renderer->setTemplate('master/noaccess');
0 ignored issues
show
Bug Best Practice introduced by
The property renderer does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
184
            return $this->sendResponseTemplate();
185
        }
186
        $id = $this->request->query->get($this->model->getIdField());
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
187
        $this->model = new Module();
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
188
        if ($this->model->load($id)) {
189
            $modelName = $this->model->{$this->model->getNameField()};
190
            ModuleManager::getInstance()::enableModule($modelName);
191
        }
192
193
        return $this->redirect(baseUrl('index.php?' . constant('CALL_CONTROLLER') . '=' . $this->shortName));
0 ignored issues
show
Bug introduced by
The method redirect() does not exist on Alxarafe\Controllers\Modules. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
        return $this->/** @scrutinizer ignore-call */ redirect(baseUrl('index.php?' . constant('CALL_CONTROLLER') . '=' . $this->shortName));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The function baseUrl 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

193
        return $this->redirect(/** @scrutinizer ignore-call */ baseUrl('index.php?' . constant('CALL_CONTROLLER') . '=' . $this->shortName));
Loading history...
Bug Best Practice introduced by
The property shortName does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
194
    }
195
196
    /**
197
     * Default disable method for disable an individual register.
198
     *
199
     * @return Response
200
     */
201
    public function disableMethod(): Response
202
    {
203
        if (!$this->canAccess || !$this->canUpdate) {
0 ignored issues
show
Bug Best Practice introduced by
The property canAccess does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property canUpdate does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
204
            $this->renderer->setTemplate('master/noaccess');
0 ignored issues
show
Bug Best Practice introduced by
The property renderer does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
205
            return $this->sendResponseTemplate();
206
        }
207
        $id = $this->request->query->get($this->model->getIdField());
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
208
        $this->model = new Module();
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
209
        if ($this->model->load($id)) {
210
            $modelName = $this->model->{$this->model->getNameField()};
211
            ModuleManager::getInstance()::disableModule($modelName);
212
        }
213
214
        return $this->redirect(baseUrl('index.php?' . constant('CALL_CONTROLLER') . '=' . $this->shortName));
0 ignored issues
show
Bug introduced by
The function baseUrl 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

214
        return $this->redirect(/** @scrutinizer ignore-call */ baseUrl('index.php?' . constant('CALL_CONTROLLER') . '=' . $this->shortName));
Loading history...
Bug Best Practice introduced by
The property shortName does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
215
    }
216
217
    /**.
218
     * Returns the page details.
219
     *
220
     * @return array
221
     */
222
    public function pageDetails(): array
223
    {
224
        $details = [
225
            'title' => 'modules-title',
226
            'icon' => '<i class="fas fa-cogs"></i>',
227
            'description' => 'modules-description',
228
            'menu' => 'admin',
229
        ];
230
        return $details;
231
    }
232
233
    /**
234
     * Returns a list of actions buttons. By default returns Read/Update/Delete actions.
235
     * If some needs to be replace, replace it on final class.
236
     *
237
     * @param string $id
238
     *
239
     * @return array
240
     */
241
    public function getActionButtons(string $id = ''): array
242
    {
243
        $actionButtons = [];
244
        $actionButtons['enable'] = [
245
            'class' => 'btn btn-success btn-sm',
246
            'type' => 'button',
247
            'link' => $this->url . '&' . constant('METHOD_CONTROLLER') . '=enable&id=' . $id,
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
248
            'icon' => '<i class="far fa-check-square"></i>',
249
            'text' => $this->translator->trans('enable'),
0 ignored issues
show
Bug Best Practice introduced by
The property translator does not exist on Alxarafe\Controllers\Modules. Did you maybe forget to declare it?
Loading history...
250
        ];
251
        $actionButtons['disable'] = [
252
            'class' => 'btn btn-warning btn-sm',
253
            'type' => 'button',
254
            'link' => $this->url . '&' . constant('METHOD_CONTROLLER') . '=disable&id=' . $id,
255
            'icon' => '<i class="far fa-square"></i>',
256
            'text' => $this->translator->trans('disable'),
257
        ];
258
259
        $actionButtons = array_merge($actionButtons, parent::getActionButtons($id));
0 ignored issues
show
Bug introduced by
The method getActionButtons() does not exist on Alxarafe\Core\Base\Controller. It seems like you code against a sub-type of Alxarafe\Core\Base\Controller such as Alxarafe\Controllers\Modules. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

259
        $actionButtons = array_merge($actionButtons, parent::/** @scrutinizer ignore-call */ getActionButtons($id));
Loading history...
260
        unset($actionButtons['read'], $actionButtons['update']);
261
262
        return $actionButtons;
263
    }
264
265
    public function setTemplate(): string
266
    {
267
        return 'modules';
268
    }
269
}