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

Controller::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 16
rs 10
c 0
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\Controller;
20
21
use Alxarafe\Base\Database;
22
use Alxarafe\Base\Controller\Trait\DbTrait;
23
use Alxarafe\Lib\Auth;
24
25
/**
26
 * Class Controller. Controller is the general purpose controller and requires the user to be authenticated.
27
 *
28
 * @package Alxarafe\Base
29
 */
30
abstract class Controller extends ViewController
31
{
32
    use DbTrait;
33
34
    public $db = null;
35
    public $username;
36
37
    public function __construct()
38
    {
39
        parent::__construct();
40
41
        if (!static::connectDb($this->config->db)) {
42
            throw new \Exception('Cannot connect to database.');
43
        }
44
45
        $this->db = new Database($this->config->db);
46
47
        if (!Auth::isLogged()) {
48
            die('Usuario no identificado1');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
49
            header('Location: ' . BASE_URL . '/index.php?module=Admin&controller=Auth');
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...
Unused Code introduced by
header('Location: ' . Al...Admin&controller=Auth') is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
50
        }
51
52
        $this->username = Auth::$user->name;
53
    }
54
}
55