Passed
Push — master ( 6084dc...68443e )
by Mihail
05:40
created

FormInstall   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 6
c 2
b 2
f 0
lcom 1
cbo 11
dl 0
loc 108
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A before() 0 5 1
A labels() 0 17 1
A rules() 0 14 1
B make() 0 35 1
A filterCheckDb() 0 12 2
1
<?php
2
3
namespace Apps\Model\Install\Main;
4
5
use Apps\ActiveRecord\Profile;
6
use Apps\ActiveRecord\User;
7
use Apps\Controller\Console\Db;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\Model;
10
use Ffcms\Core\Helper\FileSystem\File;
11
use Ffcms\Core\Helper\Type\Str;
12
13
class FormInstall extends Model
14
{
15
    public $db = [];
16
    public $email;
17
    public $multiLanguage;
18
    public $singleLanguage;
19
    public $user = [];
20
21
    public function before()
22
    {
23
        $this->db['charset'] = 'utf8';
24
        $this->db['collation'] = 'utf8_unicode_ci';
25
    }
26
27
    /**
28
    * Labels for installation form
29
    */
30
    public function labels()
31
    {
32
        return [
33
            'db.driver' => __('Database type'),
34
            'db.host' => __('Host'),
35
            'db.username' => __('User name'),
36
            'db.password' => __('User password'),
37
            'db.database' => __('Database name'),
38
            'db.prefix' => __('Table prefix'),
39
            'email' => __('SendFrom email'),
40
            'singleLanguage' => __('Default language'),
41
            'multiLanguage' => __('Multi language'),
42
            'user.login' => __('Login'),
43
            'user.email' => __('Email'),
44
            'user.password' => __('Password')
45
        ];
46
    }
47
48
    /**
49
    * Installation post data validation
50
    */
51
    public function rules()
52
    {
53
        return [
54
            [['db.driver', 'db.host', 'db.username', 'db.password', 'db.database', 'db.prefix', 'email', 'singleLanguage'], 'required'],
55
            [['user.login', 'user.email', 'user.password'], 'required'],
56
            [['user.login', 'user.password'], 'length_min', 4],
57
            ['user.email', 'email'],
58
            ['multiLanguage', 'used'],
59
            ['db.driver', 'in', ['mysql', 'pgsql', 'sqlite']],
60
            ['email', 'email'],
61
            ['singleLanguage', 'in', App::$Translate->getAvailableLangs()],
62
            ['db', 'Apps\Model\Install\Main\FormInstall::filterCheckDb']
63
        ];
64
    }
65
66
    public function make()
67
    {
68
        // prepare configurations to save
69
        $cfg = App::$Properties->getAll('default');
70
        $this->before();
71
        $cfg['database'] = $this->db;
72
        $cfg['adminEmail'] = $this->email;
73
        $cfg['singleLanguage'] = $this->singleLanguage;
74
        $cfg['multiLanguage'] = (bool)$this->multiLanguage;
75
        $cfg['passwordSalt'] = '$2a$07$' . Str::randomLatinNumeric(mt_rand(21, 30)) . '$';
76
        $cfg['debug']['cookie']['key'] = 'fdebug_' . Str::randomLatinNumeric(mt_rand(4, 16));
77
        $cfg['debug']['cookie']['value'] = Str::randomLatinNumeric(mt_rand(32, 128));
78
79
        // import database tables
80
        $connectName = 'install';
81
        include(root . '/Private/Database/install.php');
82
83
        // insert admin user
84
        $user = new User();
85
        $user->setConnection('install');
86
        $user->login = $this->user['login'];
87
        $user->email = $this->user['email'];
88
        $user->role_id = 4;
89
        $user->password = App::$Security->password_hash($this->user['password'], $cfg['passwordSalt']);
90
        $user->save();
91
92
        $profile = new Profile();
93
        $profile->setConnection('install');
94
        $profile->user_id = $user->id;
95
        $profile->save();
96
97
        // write config data
98
        App::$Properties->writeConfig('default', $cfg);
0 ignored issues
show
Bug introduced by
It seems like $cfg defined by \Ffcms\Core\App::$Properties->getAll('default') on line 69 can also be of type boolean; however, Ffcms\Core\Properties::writeConfig() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
99
        File::write('/Private/Install/install.lock', 'Installation is locked!');
100
    }
101
102
103
    /**
104
     * Check database connection filter
105
     * @param array $cfg
106
     * @return bool
107
     */
108
    public function filterCheckDb($cfg = [])
0 ignored issues
show
Unused Code introduced by
The parameter $cfg is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
    {
110
        App::$Database->addConnection($this->db, 'install');
111
112
        try {
113
            App::$Database->getConnection('install')->getDatabaseName();
0 ignored issues
show
Unused Code introduced by
The call to the method Illuminate\Database\Connection::getDatabaseName() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
114
        } catch (\Exception $e) {
115
            return false;
116
        }
117
118
        return true;
119
    }
120
}