Test Failed
Push — main ( f24735...83b0dc )
by Rafael
11:53
created

EditConfig::main()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
ccs 0
cts 17
cp 0
crap 2
1
<?php
2
/**
3
 * Copyright (C) 2022-2023  Rafael San José Tovar   <[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
 * (at your option) 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\Controllers;
20
21
use Alxarafe\Core\Base\BasicController;
22
use Alxarafe\Core\Singletons\Config;
23
use Alxarafe\Core\Singletons\FlashMessages;
24
use Alxarafe\Core\Singletons\Render;
25
use Alxarafe\Core\Singletons\Translator;
26
use Alxarafe\Database\DB;
27
use Alxarafe\Database\Engine;
28
29
/**
30
 * Controller for editing database and skin settings
31
 *
32
 * @package Alxarafe\Controllers
33
 */
34
class EditConfig extends BasicController
35
{
36
    public $language;
37
38
    public $checkDebug;
39
40
    public function main(): void
41
    {
42
        $this->dbEngines = Engine::getEngines();
0 ignored issues
show
Bug Best Practice introduced by
The property dbEngines does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        $this->dbEngineName = Config::getVar('database', 'main', 'dbEngineName') ?? $this->dbEngines[0] ?? '';
0 ignored issues
show
Bug Best Practice introduced by
The property dbEngineName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
45
        $this->skins = Render::getSkins();
0 ignored issues
show
Bug Best Practice introduced by
The property skins does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        $this->skin = Config::getVar('templaterender', 'main', 'skin') ?? $this->skins[0] ?? '';
0 ignored issues
show
Bug Best Practice introduced by
The property skin does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
48
        $this->checkDebug = Config::getVar('constants', 'boolean', 'DEBUG') ?? false;
49
50
        $this->languages = Translator::getAvailableLanguages();
0 ignored issues
show
Bug Best Practice introduced by
The property languages does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
        $this->language = Config::getVar('translator', 'main', 'language') ?? key($this->languages) ?? 'es';
52
53
        $this->dbConfig['dbUser'] = Config::getVar('database', 'main', 'dbUser') ?? 'root';
0 ignored issues
show
Bug Best Practice introduced by
The property dbConfig does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
        $this->dbConfig['dbPass'] = Config::getVar('database', 'main', 'dbPass') ?? '';
55
        $this->dbConfig['dbName'] = Config::getVar('database', 'main', 'dbName') ?? 'alxarafe';
56
        $this->dbConfig['dbHost'] = Config::getVar('database', 'main', 'dbHost') ?? 'localhost';
57
        $this->dbConfig['dbPrefix'] = Config::getVar('database', 'main', 'dbPrefix') ?? 'tc_';
58
        $this->dbConfig['dbPort'] = Config::getVar('database', 'main', 'dbPort') ?? '';
59
60
        parent::main();
61
    }
62
63
    /**
64
     * Save the form changes in the configuration file
65
     *
66
     * @return bool
67
     */
68
    public function doSave(): bool
69
    {
70
        Config::setVar('constants', 'boolean', 'DEBUG', isset($_POST['debug']));
0 ignored issues
show
Bug introduced by
IssetNode of type boolean is incompatible with the type string expected by parameter $value of Alxarafe\Core\Singletons\Config::setVar(). ( Ignorable by Annotation )

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

70
        Config::setVar('constants', 'boolean', 'DEBUG', /** @scrutinizer ignore-type */ isset($_POST['debug']));
Loading history...
71
        Config::setVar('translator', 'main', 'language', $_POST['language'] ?? 'en');
72
        Config::setVar('templaterender', 'main', 'skin', $_POST['skin'] ?? 'default');
73
        Config::setVar('database', 'main', 'dbEngineName', $_POST['dbEngineName'] ?? '');
74
        Config::setVar('database', 'main', 'dbUser', $_POST['dbUser'] ?? '');
75
        Config::setVar('database', 'main', 'dbPass', $_POST['dbPass'] ?? '');
76
        Config::setVar('database', 'main', 'dbName', $_POST['dbName'] ?? '');
77
        Config::setVar('database', 'main', 'dbHost', $_POST['dbHost'] ?? '');
78
        Config::setVar('database', 'main', 'dbPort', $_POST['dbPort'] ?? '');
79
        Config::setVar('database', 'main', 'dbPrefix', $_POST['dbPrefix'] ?? '');
80
81
        $result = DB::connectToDatabase();
82
        if (!$result) {
83
            FlashMessages::setError('database_not_found', 'next');
84
        }
85
86
        $result = Config::saveConfigFile();
87
        if ($result) {
88
            FlashMessages::setSuccess('saved', 'next');
89
            header('location: ' . self::url('Main', 'EditConfig'));
90
            die();
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
91
        }
92
93
        return $result;
94
    }
95
96
    public function setTemplate(): string
97
    {
98
        return 'config';
99
    }
100
}
101