InitialUserSettings   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 95
c 0
b 0
f 0
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUserStatus() 0 8 4
A setUserRole() 0 27 5
A run() 0 20 2
1
<?php
2
3
namespace app\helpers;
4
5
use Yii;
6
use yii\rbac\DbManager;
7
use yii\db\TableSchema;
8
use yii\web\IdentityInterface;
9
10
/**
11
 * Class InitialUserSettings
12
 *
13
 * This class for setting initial user settings, such as status and role after registration.
14
 */
15
class InitialUserSettings
16
{
17
    /**
18
     * Initial constants.
19
     */
20
    const INIT_USER_STATUS = 1;
21
    const INIT_USER_ROLE = 'admin';
22
23
    const AUTH_MANAGER_CLASS = DbManager::class;
24
25
    /**
26
     * Set initial user settings.
27
     *
28
     * @param IdentityInterface $user
29
     *
30
     * @return bool
31
     */
32
    public static function run(IdentityInterface $user): bool
33
    {
34
        $db = Yii::$app->db;
35
36
        /* @var $settingsTableSchema TableSchema */
37
        $settingsTableSchema = $db->getTableSchema('settings');
38
39
        if (null === $settingsTableSchema) {
40
            Yii::error('Table "settings" does not exists for initial user properties.');
41
            return false;
42
        }
43
44
        $settings = $db->createCommand('SELECT * FROM settings')
45
            ->queryOne();
46
47
        self::setUserStatus($settingsTableSchema, $settings, $user);
48
49
        self::setUserRole($settingsTableSchema, $settings, $user);
50
51
        return true;
52
    }
53
54
    /**
55
     * Set initial user status.
56
     *
57
     * @param TableSchema $settingsTableSchema
58
     * @param array|bool $settings
59
     * @param IdentityInterface $user
60
     *
61
     * @return void
62
     */
63
    private static function setUserStatus(TableSchema $settingsTableSchema, $settings, IdentityInterface $user): void
64
    {
65
        if (in_array('initUserStatus', $settingsTableSchema->columnNames)) {
66
            $user->status = is_array($settings) && null !== $settings['initUserStatus'] ?
0 ignored issues
show
Bug introduced by
Accessing status on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
67
                $settings['initUserStatus'] : self::INIT_USER_STATUS;
68
            $user->save();
0 ignored issues
show
Bug introduced by
The method save() does not exist on yii\web\IdentityInterface. It seems like you code against a sub-type of yii\web\IdentityInterface such as Itstructure\RbacModule\i...s\RbacIdentityInterface. ( Ignorable by Annotation )

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

68
            $user->/** @scrutinizer ignore-call */ 
69
                   save();
Loading history...
69
        } else {
70
            Yii::error('Field "initUserStatus" does not exists in "settings" table to set initial user status.');
71
        }
72
    }
73
74
    /**
75
     * Set initial user role.
76
     *
77
     * @param TableSchema $settingsTableSchema
78
     * @param array|bool $settings
79
     * @param IdentityInterface $user
80
     *
81
     * @return void
82
     */
83
    private static function setUserRole(TableSchema $settingsTableSchema, $settings, IdentityInterface $user): void
84
    {
85
        if (in_array('initUserRole', $settingsTableSchema->columnNames)) {
86
87
            /* @var $authManager \yii\rbac\BaseManager */
88
            $authManager = Yii::$app->get('authManager');
89
90
            if (null === $authManager) {
91
                $authManager = Yii::createObject([
92
                    'class' => self::AUTH_MANAGER_CLASS,
93
                ]);
94
            }
95
96
            if (!is_array($settings) || null === $settings['initUserRole']) {
97
                $initUserRole = self::INIT_USER_ROLE;
98
                Yii::error('Can not get field value of "initUserRole" from "settings" table to set initial user role.');
99
            } else {
100
                $initUserRole = $settings['initUserRole'];
101
            }
102
103
            $authManager->assign(
104
                $authManager->getRole($initUserRole),
105
                $user->getId()
106
            );
107
108
        } else {
109
            Yii::error('Field "initUserRole" does not exists in "settings" table to set initial user role.');
110
        }
111
    }
112
}
113