FormInstall   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 103
dl 0
loc 174
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A before() 0 6 1
A labels() 0 21 1
A filterCheckDb() 0 11 2
A rules() 0 19 1
A make() 0 75 3
1
<?php
2
3
namespace Apps\Model\Install\Main;
4
5
use Apps\ActiveRecord\Profile;
6
use Apps\ActiveRecord\System;
7
use Apps\ActiveRecord\User;
8
use Extend\Version;
9
use Ffcms\Core\App;
10
use Ffcms\Core\Arch\Model;
11
use Ffcms\Core\Helper\Crypt;
12
use Ffcms\Core\Helper\FileSystem\File;
13
use Ffcms\Core\Managers\MigrationsManager;
14
15
/**
16
 * Class FormInstall. System installation business logic model
17
 * @package Apps\Model\Install\Main
18
 */
19
class FormInstall extends Model
20
{
21
    public $db = [];
22
    public $mail = [];
23
    public $multiLanguage;
24
    public $singleLanguage;
25
    public $user = [];
26
    public $mainpage;
27
28
    public $baseDomain;
29
30
    public $_name = 'formInstall';
31
32
    /**
33
     * Set default data
34
     */
35
    public function before()
36
    {
37
        $this->db['charset'] = 'utf8';
38
        $this->db['collation'] = 'utf8_unicode_ci';
39
40
        $this->baseDomain = App::$Request->getHttpHost();
41
    }
42
43
    /**
44
     * Labels for installation form
45
     * @return array
46
     */
47
    public function labels(): array
48
    {
49
        return [
50
            'db.driver' => __('Database type'),
51
            'db.host' => __('Host'),
52
            'db.username' => __('User name'),
53
            'db.password' => __('User password'),
54
            'db.database' => __('Database name'),
55
            'db.prefix' => __('Table prefix'),
56
            'mail.enable' => __('Use mail features'),
57
            'mail.host' => __('Host'),
58
            'mail.port' => __('Port'),
59
            'mail.encrypt' => __('Encryption'),
60
            'mail.user' => __('User'),
61
            'mail.password' => __('Password'),
62
            'singleLanguage' => __('Default language'),
63
            'multiLanguage' => __('Multi language'),
64
            'user.email' => __('Email'),
65
            'user.password' => __('Password'),
66
            'user.repassword' => __('Repeat password'),
67
            'mainpage' => __('Main page')
68
        ];
69
    }
70
71
    /**
72
     * Installation post data validation rules
73
     * @return array
74
     */
75
    public function rules(): array
76
    {
77
        return [
78
            [['db.driver', 'db.host', 'db.username', 'db.password', 'db.database', 'db.prefix', 'singleLanguage', 'mainpage'], 'required'],
79
            [['user.email', 'user.password', 'user.repassword'], 'required'],
80
            ['mail.enable', 'required'],
81
            ['mail.enable', 'int'],
82
            [['mail.host', 'mail.port', 'mail.user', 'main.encrypt', 'mail.password'], 'used'],
83
            ['mail.user', 'email'],
84
            ['mail.port', 'int'],
85
            ['mail.encrypt', 'in', ['ssl', 'tls', 'none']],
86
            ['mainpage', 'in', ['none', 'news', 'about']],
87
            ['user.password', 'length_min', 4],
88
            ['user.repassword', 'equal', $this->getRequest('user.password', $this->getSubmitMethod())],
89
            ['user.email', 'email'],
90
            ['multiLanguage', 'used'],
91
            ['db.driver', 'in', ['mysql', 'pgsql', 'sqlite']],
92
            ['singleLanguage', 'in', App::$Translate->getAvailableLangs()],
93
            ['db', 'Apps\Model\Install\Main\FormInstall::filterCheckDb']
94
        ];
95
    }
96
97
    /**
98
     * Save configurations build by installer interface
99
     */
100
    public function make()
101
    {
102
        // prepare configurations to save
103
        /** @var array $cfg */
104
        $cfg = App::$Properties->getAll('default');
105
        $this->before();
106
        $cfg['baseDomain'] = $this->baseDomain;
107
        $cfg['database'] = $this->db;
108
        $cfg['singleLanguage'] = $this->singleLanguage;
109
        $cfg['multiLanguage'] = (bool)$this->multiLanguage;
110
        $cfg['debug']['cookie']['key'] = 'fdebug_' . Crypt::randomString(mt_rand(4, 16));
111
        $cfg['debug']['cookie']['value'] = Crypt::randomString(mt_rand(32, 128));
112
        $cfg['mail'] = $this->mail;
113
114
        // initialize migrations table
115
        App::$Database->getConnection('install')->getSchemaBuilder()->create('migrations', function ($table) {
116
            $table->increments('id');
117
            $table->string('migration', 128)->unique();
118
            $table->timestamps();
119
        });
120
121
        // import migrations
122
        $manager = new MigrationsManager(null, 'install');
123
        $search = $manager->search(null, false);
124
        $manager->makeUp($search);
0 ignored issues
show
Bug introduced by
$search of type false is incompatible with the type array|string expected by parameter $file of Ffcms\Core\Managers\MigrationsManager::makeUp(). ( Ignorable by Annotation )

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

124
        $manager->makeUp(/** @scrutinizer ignore-type */ $search);
Loading history...
125
126
        // insert admin user
127
        $user = new User();
128
        $user->setConnection('install');
129
        $user->email = $this->user['email'];
130
        $user->role_id = 4;
131
        $user->password = Crypt::passwordHash($this->user['password']);
132
        $user->save();
133
134
        $profile = new Profile();
135
        $profile->setConnection('install');
136
        $profile->user_id = $user->id;
137
        $profile->save();
138
139
        // set installation version
140
        $system = new System();
141
        $system->setConnection('install');
142
        $system->var = 'version';
143
        $system->data = Version::VERSION;
144
        $system->save();
145
146
        // write config data
147
        App::$Properties->writeConfig('default', $cfg);
148
        // make routing configs based on preset property
149
        $routing = [];
150
        switch ($this->mainpage) {
151
            case 'news':
152
                $routing = [
153
                    'Alias' => [
154
                        'Front' => [
155
                            '/' => '/content/list/news',
156
                            '/about' => '/content/read/page/about-page'
157
                        ]
158
                    ]
159
                ];
160
                break;
161
            case 'about':
162
                $routing = [
163
                    'Alias' => [
164
                        'Front' => [
165
                            '/' => '/content/read/page/about-page'
166
                        ]
167
                    ]
168
                ];
169
                break;
170
        }
171
        // write routing configurations
172
        App::$Properties->writeConfig('routing', $routing);
173
        // write installer lock
174
        File::write('/Private/Install/install.lock', 'Installation is locked!');
175
    }
176
177
178
    /**
179
     * Check database connection filter
180
     * @return bool
181
     */
182
    public function filterCheckDb()
183
    {
184
        App::$Database->addConnection($this->db, 'install');
185
186
        try {
187
            App::$Database->getConnection('install')->getDatabaseName();
188
        } catch (\Exception $e) {
189
            return false;
190
        }
191
192
        return true;
193
    }
194
}
195