Passed
Push — master ( 4d3a01...b26c9e )
by Mihail
12:24
created

Db::actionAdduser()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 42
rs 6.7273
cc 7
eloc 30
nc 7
nop 0
1
<?php
2
3
namespace Apps\Controller\Console;
4
5
use Apps\ActiveRecord\Profile;
6
use Apps\ActiveRecord\User;
7
use Ffcms\Console\App;
8
use Ffcms\Core\Exception\NativeException;
9
use Ffcms\Core\Helper\FileSystem\File;
10
use Ffcms\Core\Helper\Security;
11
use Ffcms\Core\Helper\Type\Arr;
12
use Ffcms\Core\Helper\Type\Str;
13
14
class Db
15
{
16
    public function actionImport($activeRecord)
17
    {
18
        $importFile = root . '/Private/Database/Tables/' . ucfirst(strtolower($activeRecord)) . '.php';
19
        if (!File::exist($importFile)) {
20
            return App::$Output->write('Database model table not founded: ' . $activeRecord);
21
        }
22
23
        @include($importFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
24
        return App::$Output->write('Database table import runned: ' . $activeRecord);
25
    }
26
27
    public function actionImportAll($connectName = 'default')
28
    {
29
        $importFile = root . '/Private/Database/install.php';
30
        if (!File::exist($importFile)) {
31
            return App::$Output->write('Import file is not exist: ' . $importFile);
32
        }
33
        @include($importFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
34
        return App::$Output->write('All database tables was imported!');
35
    }
36
37
    public function actionAdduser()
38
    {
39
        echo "Login:";
40
        $login = App::$Input->read();
41
        if (Str::length($login) < 2) {
42
            throw new NativeException('Login is bad');
43
        }
44
        echo "Email:";
45
        $email = App::$Input->read();
46
        if (!Str::isEmail($email)) {
47
            throw new NativeException('Email is bad');
48
        }
49
        echo "Password:";
50
        $pass = App::$Input->read();
51
        if (Str::length($pass) < 2) {
52
            throw new NativeException('Password is bad');
53
        }
54
        echo "RoleId (1 = user, 3 = admin):";
55
        $role = (int)App::$Input->read();
56
        if (!Arr::in($role, [1,2,3])) {
57
            $role = 1;
58
        }
59
60
        if (User::isMailExist($email) || User::isLoginExist($login)) {
61
            throw new NativeException('User with this email or login is always exist');
62
        }
63
64
        $salt = App::$Properties->get('passwordSalt');
65
66
        $user = new User();
67
        $user->login = $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...
68
        $user->email = $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...
69
        $user->password = Security::password_hash($pass, $salt);
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 $salt.

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...
70
        $user->role_id = $role;
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...
71
        $user->save();
72
73
        $profile = new Profile();
74
        $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...
75
        $profile->save();
76
77
        return App::$Output->write('User was successful added to database!');
78
    }
79
80
81
}