Passed
Push — main ( 140736...2889d1 )
by Rafael
04:25
created

ConfigController::getPost()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 9.6111
c 0
b 0
f 0
cc 5
nc 10
nop 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 CoreModules\Admin\Controller;
20
21
use Alxarafe\Base\Config;
22
use Alxarafe\Base\Controller\ViewController;
23
use Alxarafe\Lib\Functions;
24
use stdClass;
25
26
class ConfigController extends ViewController
27
{
28
    public $data;
29
30
    public function doIndex(): bool
31
    {
32
        /**
33
         * TODO: The value of this variable will be filled in when the roles
34
         *       are correctly implemented.
35
         */
36
        $restricted_access = false;
37
38
        $this->template = 'page/config';
39
        if (isset($this->config) && $restricted_access) {
0 ignored issues
show
introduced by
The condition $restricted_access is always false.
Loading history...
40
            $this->template = 'page/forbidden';
41
        }
42
43
        $this->getPost();
44
45
        return true;
46
    }
47
48
    private function getPost()
49
    {
50
        if (!isset($this->data)) {
51
            $this->data = new stdClass();
52
        }
53
        foreach (Config::getConfig() as $section => $values) {
54
            if (!isset($this->data->{$section})) {
55
                $this->data->{$section} = new stdClass();
56
            }
57
            foreach ($values as $variable => $value) {
58
                $this->data->{$section}->{$variable} = Functions::getIfIsset($variable, $value);
59
            }
60
        }
61
    }
62
63
    public function doLogin()
64
    {
65
        $this->template = 'page/admin/login';
66
        $login = filter_input(INPUT_POST, 'login');
67
        if (!$login) {
68
            return true;
69
        }
70
71
        $username = filter_input(INPUT_POST, 'username');
72
        $password = filter_input(INPUT_POST, 'password');
73
        if (!Auth::login($username, $password)) {
74
            $this->advice[] = $this->langs->trans('ErrorBadLoginPassword');
0 ignored issues
show
Bug Best Practice introduced by
The property advice does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The property langs does not exist on CoreModules\Admin\Controller\ConfigController. Did you mean lang?
Loading history...
75
            dump([
76
                'ConfigController' => $this
77
            ]);
78
            return true;
79
        }
80
        $this->template = 'page/admin/info';
81
    }
82
83
    public function doLogout()
84
    {
85
        Auth::logout(true);
86
        return true;
87
    }
88
89
    public function doCheckConnection()
90
    {
91
        $this->template = 'page/config';
92
93
        static::getPost();
0 ignored issues
show
Bug Best Practice introduced by
The method CoreModules\Admin\Contro...igController::getPost() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        static::/** @scrutinizer ignore-call */ 
94
                getPost();
Loading history...
94
        var_dump($this->data->db);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->data->db) looks like debug code. Are you sure you do not want to remove it?
Loading history...
95
        $ok = Config::checkDatabaseConnection($this->data->db);
96
        if (!$ok) {
97
            static::addError('Error al conectar a la base de datos "' . $this->data->db->name . '".');
98
            return true;
99
        }
100
        static::addMessage('Conexión satisfactoria con la base de datos "' . $this->data->db->name . '".');
101
        return true;
102
    }
103
104
    public function doSave(): bool
105
    {
106
        static::getPost();
0 ignored issues
show
Bug Best Practice introduced by
The method CoreModules\Admin\Contro...igController::getPost() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        static::/** @scrutinizer ignore-call */ 
107
                getPost();
Loading history...
107
        /**
108
         * Converts the stdClass to an array
109
         */
110
        $data = json_decode(json_encode($this->data), true);
111
112
        $result = config::setConfig($data);
113
        if (!$result) {
114
            $this->template = 'page/config';
115
            static::addError('Error al guardar la configuración');
116
            return false;
117
        }
118
        /**
119
         * TODO: Loads public page
120
         */
121
        $this->template = 'page/public';
122
        static::addMessage('Configuración guardada correctamente');
123
        return true;
124
    }
125
}
126