Passed
Push — master ( b21109...7068ce )
by Mihail
12:43
created

FormInstall::make()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 62
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
cc 3
eloc 41
c 3
b 3
f 0
nc 3
nop 0
dl 0
loc 62
rs 9.4743

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public $mainpage;
21
22
    public function before()
23
    {
24
        $this->db['charset'] = 'utf8';
25
        $this->db['collation'] = 'utf8_unicode_ci';
26
    }
27
28
    /**
29
    * Labels for installation form
30
    */
31
    public function labels()
32
    {
33
        return [
34
            'db.driver' => __('Database type'),
35
            'db.host' => __('Host'),
36
            'db.username' => __('User name'),
37
            'db.password' => __('User password'),
38
            'db.database' => __('Database name'),
39
            'db.prefix' => __('Table prefix'),
40
            'email' => __('SendFrom email'),
41
            'singleLanguage' => __('Default language'),
42
            'multiLanguage' => __('Multi language'),
43
            'user.login' => __('Login'),
44
            'user.email' => __('Email'),
45
            'user.password' => __('Password'),
46
            'user.repassword' => __('Repeat password'),
47
            'mainpage' => __('Main page')
48
        ];
49
    }
50
51
    /**
52
    * Installation post data validation
53
    */
54
    public function rules()
55
    {
56
        return [
57
            [['db.driver', 'db.host', 'db.username', 'db.password', 'db.database', 'db.prefix', 'email', 'singleLanguage', 'mainpage'], 'required'],
58
            [['user.login', 'user.email', 'user.password', 'user.repassword'], 'required'],
59
            ['mainpage', 'in', ['none', 'news', 'about']],
60
            [['user.login', 'user.password'], 'length_min', 4],
61
            ['user.repassword', 'equal', $this->getRequest('user.password', $this->getSubmitMethod())],
62
            ['user.email', 'email'],
63
            ['multiLanguage', 'used'],
64
            ['db.driver', 'in', ['mysql', 'pgsql', 'sqlite']],
65
            ['email', 'email'],
66
            ['singleLanguage', 'in', App::$Translate->getAvailableLangs()],
67
            ['db', 'Apps\Model\Install\Main\FormInstall::filterCheckDb']
68
        ];
69
    }
70
71
    /**
72
     * Save configurations build by installer interface
73
     */
74
    public function make()
75
    {
76
        // prepare configurations to save
77
        /** @var array $cfg */
78
        $cfg = App::$Properties->getAll('default');
79
        $this->before();
80
        $cfg['database'] = $this->db;
81
        $cfg['adminEmail'] = $this->email;
82
        $cfg['singleLanguage'] = $this->singleLanguage;
83
        $cfg['multiLanguage'] = (bool)$this->multiLanguage;
84
        $cfg['passwordSalt'] = '$2a$07$' . Str::randomLatinNumeric(mt_rand(21, 30)) . '$';
85
        $cfg['debug']['cookie']['key'] = 'fdebug_' . Str::randomLatinNumeric(mt_rand(4, 16));
86
        $cfg['debug']['cookie']['value'] = Str::randomLatinNumeric(mt_rand(32, 128));
87
88
        // import database tables
89
        $connectName = 'install';
90
        include(root . '/Private/Database/install.php');
91
92
        // insert admin user
93
        $user = new User();
94
        $user->setConnection('install');
95
        $user->login = $this->user['login'];
96
        $user->email = $this->user['email'];
97
        $user->role_id = 4;
98
        $user->password = App::$Security->password_hash($this->user['password'], $cfg['passwordSalt']);
99
        $user->save();
100
101
        $profile = new Profile();
102
        $profile->setConnection('install');
103
        $profile->user_id = $user->id;
104
        $profile->save();
105
106
        // write config data
107
        App::$Properties->writeConfig('default', $cfg);
108
        // make routing configs based on preset property
109
        $routing = [];
110
        switch ($this->mainpage) {
111
            case 'news':
112
                $routing = [
113
                    'Alias' => [
114
                        'Front' => [
115
                            '/' => '/content/list/news',
116
                            '/about' => '/content/read/page/about-page'
117
                        ]
118
                    ]
119
                ];
120
                break;
121
            case 'about':
122
                $routing = [
123
                    'Alias' => [
124
                        'Front' => [
125
                            '/' => '/content/read/page/about-page'
126
                        ]
127
                    ]
128
                ];
129
                break;
130
        }
131
        // write routing configurations
132
        App::$Properties->writeConfig('routing', $routing);
133
        // write installer lock
134
        File::write('/Private/Install/install.lock', 'Installation is locked!');
135
    }
136
137
138
    /**
139
     * Check database connection filter
140
     * @param array $cfg
141
     * @return bool
142
     */
143
    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...
144
    {
145
        App::$Database->addConnection($this->db, 'install');
146
147
        try {
148
            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...
149
        } catch (\Exception $e) {
150
            return false;
151
        }
152
153
        return true;
154
    }
155
}