Test Failed
Push — main ( 82933d...ebe982 )
by Rafael
02:28
created

GenericController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 17 3
A beforeAction() 0 3 1
A __construct() 0 8 3
A index() 0 6 2
A executeAction() 0 8 4
A afterAction() 0 3 1
1
<?php
2
3
/* Copyright (C) 2024      Rafael San José      <[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
 * 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\Base\Controller;
20
21
use Alxarafe\Tools\Debug;
22
use Illuminate\Support\Str;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Str 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
use stdClass;
24
25
/**
26
 * Class GenericController. The generic controller contains what is necessary for any controller
27
 *
28
 * @package Alxarafe\Base
29
 */
30
abstract class GenericController
31
{
32
    /**
33
     * Contains the action to execute.
34
     *
35
     * @var string|null
36
     */
37
    public ?string $action;
38
39
    private $path;
0 ignored issues
show
introduced by
The private property $path is not used, and could be removed.
Loading history...
40
41
    /**
42
     * GenericController constructor.
43
     */
44
    public function __construct()
45
    {
46
        $this->action = filter_input(INPUT_POST, 'action');
47
        if ($this->action === null) {
48
            $this->action = filter_input(INPUT_GET, 'action');
49
        }
50
        if ($this->action === null) {
51
            $this->action = 'index';
52
        }
53
    }
54
55
    /**
56
     * Returns the generic url of the controller;
57
     *
58
     * @param $full
59
     *
60
     * @return string
61
     */
62
    public static function url($full = true)
63
    {
64
        $url = '';
65
        if ($full) {
66
            $url .= BASE_URL . '/index.php';
0 ignored issues
show
Bug introduced by
The constant Alxarafe\Base\Controller\BASE_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67
        }
68
69
        $url .=
70
            '?module=' . filter_input(INPUT_GET, 'module') .
71
            '&controller=' . filter_input(INPUT_GET, 'controller');
72
73
        $action = filter_input(INPUT_GET, 'action');
74
        if ($action) {
75
            $url .= '&action=' . $action;
76
        }
77
78
        return $url;
79
    }
80
81
    /**
82
     * Execute the selected action, returning true if successful.
83
     *
84
     * @param bool $executeActions
85
     *
86
     * @return bool
87
     */
88
    public function index(bool $executeActions = true): bool
89
    {
90
        if (!$executeActions) {
91
            return false;
92
        }
93
        return $this->executeAction();
94
    }
95
96
    /**
97
     * Execute the selected action, returning true if successful.
98
     *
99
     * @return bool
100
     */
101
    private function executeAction(): bool
102
    {
103
        $actionMethod = 'do' . ucfirst(Str::camel($this->action ?? 'index'));
104
        if (!method_exists($this, $actionMethod)) {
105
            Debug::message('Does not exist the method ' . $actionMethod);
106
            return false;
107
        }
108
        return $this->beforeAction() && $this->$actionMethod() && $this->afterAction();
109
    }
110
111
112
    /**
113
     * You can include code here that is common to call all controller actions.
114
     * If you need to do something, override this method.
115
     *
116
     * @return bool
117
     */
118
    public function beforeAction(): bool
119
    {
120
        return true;
121
    }
122
123
124
    /**
125
     * You can include code here common to calling all controller actions, which will be executed after the action.
126
     * If you need to do something, override this method.
127
     *
128
     * @return bool
129
     */
130
    public function afterAction(): bool
131
    {
132
        return true;
133
    }
134
}
135