Passed
Push — main ( 7af4bb...131f20 )
by Rafael
58:13
created

Controller   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 65
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isLogged() 0 6 2
A __construct() 0 8 1
A index() 0 12 3
A doLogout() 0 4 1
A checkLogin() 0 9 3
A doLogin() 0 11 2
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 Illuminate\Database\Capsule\Manager as DB;
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
    private static $config=null;
0 ignored issues
show
introduced by
The private property $config is not used, and could be removed.
Loading history...
34
35
    public function __construct()
36
    {
37
        static::connectDb();
38
39
        // $db = DB::connection()->getPdo();
40
41
        parent::__construct();
42
        $this->checkLogin();
43
    }
44
45
    public function checkLogin()
46
    {
47
        if ($this->action === 'logout') {
48
            return $this->doLogout();
49
        }
50
        if ($this->action !== 'login') {
51
            return true;
52
        }
53
        $this->doLogin();
54
    }
55
56
    public function doLogout()
57
    {
58
        Auth::logout();
59
        return true;
60
    }
61
62
    public function doLogin()
63
    {
64
        $username = $_POST['username'];
65
        $password = $_POST['password'];
66
        $ok = Auth::login($username, $password);
67
        if ($ok) {
68
            $this->message = 'Login ok';
69
        } else {
70
            $this->alert = 'Login KO';
71
        }
72
        return $ok;
73
    }
74
75
    public function index(bool $executeActions = true): bool
76
    {
77
        $log = $this->isLogged();
78
        if (!$log) {
79
            $login = $this->doLogin();
80
            if (!$login) {
81
                $this->template = 'auth/login';
82
                $this->action = 'index';
83
                $executeActions = false;
84
            }
85
        }
86
        return parent::index($executeActions);
87
    }
88
89
    private function isLogged()
90
    {
91
        if (!isset($this->username)) {
92
            $this->username = Auth::isLogged();
93
        }
94
        return isset($this->username);
95
    }
96
}
97