Test Failed
Push — main ( ac1cad...7178bf )
by Rafael
50:59
created

GenericController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 2
b 0
f 0
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;
20
21
use Alxarafe\Tools\Debug;
22
23
/**
24
 * Class GenericController. The generic controller contains what is necessary for any controller
25
 *
26
 * @package Alxarafe\Base
27
 */
28
abstract class GenericController
29
{
30
    /**
31
     * Contains the controller to execute.
32
     *
33
     * @var string
34
     */
35
    protected $controller;
36
37
    /**
38
     * Contains the action to execute.
39
     *
40
     * @var string
41
     */
42
    protected $action;
43
44
    /**
45
     * GenericController constructor.
46
     */
47
    public function __construct()
48
    {
49
        $this->action = filter_input(INPUT_POST, 'action');
50
    }
51
52
    /**
53
     * Execute the selected action, returning true if successful.
54
     *
55
     * @param bool $executeActions
56
     *
57
     * @return bool
58
     */
59
    public function index(bool $executeActions = true): bool
60
    {
61
        if (!$executeActions) {
62
            return false;
63
        }
64
        return $this->executeAction();
65
    }
66
67
    /**
68
     * Execute the selected action, returning true if successful.
69
     *
70
     * @return bool
71
     */
72
    private function executeAction(): bool
73
    {
74
        $actionMethod = 'do' . ucfirst($this->action);
75
        if (!method_exists($this, $actionMethod)) {
76
            Debug::message('Does not exist the method ' . $actionMethod);
77
            return false;
78
        }
79
        return $this->$actionMethod();
80
    }
81
82
    /**
83
     * Returns the generic url of the controller;
84
     *
85
     * @param $full
86
     *
87
     * @return string
88
     */
89
    public static function url($full = true)
90
    {
91
        $url = '';
92
        if ($full) {
93
            $url .= BASE_URL . '/index.php';
94
        }
95
96
        $url .=
97
            '?module=' . filter_input(INPUT_GET, 'module') .
98
            '&controller=' . filter_input(INPUT_GET, 'controller');
99
100
        return $url;
101
    }
102
}
103