Test Failed
Push — main ( 8e55b2...88c391 )
by Rafael
10:33
created

BasicController::main()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 2
eloc 5
c 1
b 1
f 1
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * Alxarafe. Development of PHP applications in a flash!
4
 * Copyright (C) 2018 Alxarafe <[email protected]>
5
 */
6
7
namespace Alxarafe\Core\Base;
8
9
use Alxarafe\Database\Engine;
10
use Alxarafe\Database\SqlHelper;
11
use DebugBar\DebugBarException;
12
13
/**
14
 * Class Controller
15
 *
16
 * The controller is who responds to the various events.
17
 *
18
 * @package Alxarafe\Core\Base
19
 */
20
abstract class BasicController extends Globals
21
{
22
    /**
23
     * Indicates whether the closing of the browser is protected
24
     * against accidental exit or closing
25
     *
26
     * @var bool
27
     */
28
    public bool $protectedClose;
29
30
    /**
31
     * Contains the action to execute or null if there is no action
32
     *
33
     * @var string|null
34
     */
35
    public ?string $action;
36
    /**
37
     * A BasicController does not have menu.
38
     *
39
     * @var bool
40
     */
41
    public bool $hasMenu = false;
42
    protected bool $configExists;
43
    /**
44
     * It contains an instance of the view class, or null if it is
45
     * not assigned, or does not have an associated view.
46
     *
47
     * @var View
48
     */
49
    private View $View;
0 ignored issues
show
introduced by
The private property $View is not used, and could be removed.
Loading history...
50
51
    /**
52
     * Controller constructor.
53
     *
54
     * @throws DebugBarException
55
     */
56
    public function __construct()
57
    {
58
        parent::__construct();
59
60
        $this->protectedClose = false;
61
        if (!$this->preLoad()) {
62
            trigger_error('preLoad fails!');
63
        }
64
    }
65
66
    /**
67
     * Initialization of variables required for all controllers
68
     *
69
     * @return bool
70
     * @throws DebugBarException
71
     */
72
    public function preLoad(): bool
73
    {
74
        $this->configExists = $this->config->loadConfig();
75
        $this->action = filter_input(INPUT_POST, 'action', FILTER_DEFAULT);
76
        return true;
77
    }
78
79
    /**
80
     * Returns an url to access to any controller of a specific module
81
     *
82
     * @param string $module
83
     * @param string $controller
84
     *
85
     * @return string
86
     */
87
    static public function url(string $module = self::DEFAULT_MODULE_NAME, string $controller = self::DEFAULT_CONTROLLER_NAME): string
88
    {
89
        return BASE_URI . '?' . self::MODULE_GET_VAR . '=' . $module . '&' . self::CONTROLLER_GET_VAR . '=' . $controller;
90
    }
91
92
    /**
93
     * Main is the entry point (execution) of the controller.
94
     *
95
     * @return bool
96
     */
97
    public function main(): bool
98
    {
99
        $result = true;
100
        if (isset($this->action)) {
101
            $result = $this->doAction();
102
        }
103
        $this->view = $this->setView();
0 ignored issues
show
Bug Best Practice introduced by
The property view does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
104
        return $result;
105
    }
106
107
    /**
108
     * Executes any action
109
     *
110
     * @return bool
111
     */
112
    public function doAction(): bool
113
    {
114
        switch ($this->action) {
115
            case 'save':
116
                return $this->doSave();
117
            case 'exit':
118
                $this->doExit();
119
                break;
120
            default:
121
                trigger_error("The '$this->action' action has not been defined!");
122
        }
123
        return true;
124
    }
125
126
    /**
127
     * Execute the Save action (overwrite it, if necessary!)
128
     *
129
     * @return bool
130
     */
131
    public function doSave(): bool
132
    {
133
        return true;
134
    }
135
136
    /**
137
     * Exit to the main route
138
     *
139
     * @return void
140
     */
141
    public function doExit(): void
142
    {
143
        header('Location: ' . BASE_URI);
144
        die();
145
    }
146
147
    /**
148
     * Return an instance to the corresponding View class
149
     *
150
     * @return View
151
     */
152
    abstract public function setView(): View;
153
154
}
155