Passed
Push — master ( 57f692...abb7a8 )
by Mihail
04:20
created

FormInstall::before()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
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
        $cfg['database'] = $this->db;
71
        $cfg['adminEmail'] = $this->email;
72
        $cfg['singleLanguage'] = $this->singleLanguage;
73
        $cfg['multiLanguage'] = (bool)$this->multiLanguage;
74
        $cfg['passwordSalt'] = '$2a$07$' . Str::randomLatinNumeric(mt_rand(21, 30)) . '$';
75
        $cfg['debug']['cookie']['key'] = 'fdebug_' . Str::randomLatinNumeric(mt_rand(4, 16));
76
        $cfg['debug']['cookie']['value'] = Str::randomLatinNumeric(mt_rand(32, 128));
77
78
        // import database tables
79
        $connectName = 'install';
80
        include(root . '/Private/Database/install.php');
81
82
        // insert admin user
83
        $user = new User();
84
        $user->setConnection('install');
85
        $user->login = $this->user['login'];
0 ignored issues
show
Documentation introduced by
The property login does not exist on object<Apps\ActiveRecord\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
86
        $user->email = $this->user['email'];
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<Apps\ActiveRecord\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
87
        $user->role_id = 3;
0 ignored issues
show
Documentation introduced by
The property role_id does not exist on object<Apps\ActiveRecord\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88
        $user->password = App::$Security->password_hash($this->user['password'], $cfg['passwordSalt']);
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<Apps\ActiveRecord\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Unused Code introduced by
The call to Security::password_hash() has too many arguments starting with $cfg['passwordSalt'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
89
        $user->save();
90
91
        $profile = new Profile();
92
        $profile->setConnection('install');
93
        $profile->user_id = $user->id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<Apps\ActiveRecord\Profile>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property id does not exist on object<Apps\ActiveRecord\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
        $profile->save();
95
96
        // write config data
97
        App::$Properties->writeConfig('default', $cfg);
0 ignored issues
show
Bug introduced by
The method writeConfig() does not seem to exist on object<Ffcms\Core\Properties>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
        File::write('/Private/Install/install.lock', 'Installation is locked!');
99
    }
100
101
102
    /**
103
     * Check database connection filter
104
     * @param array $cfg
105
     * @return bool
106
     */
107
    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...
108
    {
109
        App::$Database->addConnection($this->db, 'install');
110
111
        try {
112
            App::$Database->connection('install')->getDatabaseName();
113
        } catch (\Exception $e) {
114
            return false;
115
        }
116
117
        return true;
118
    }
119
}