Completed
Push — master ( e4197d...f733ed )
by Igor
11:05 queued 09:33
created

AuthItem   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 82
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 0 15 1
A attributeLabels() 0 9 1
A events() 0 6 1
A isSuperUser() 0 4 1
A beforeDelete() 0 4 1
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
7
/**
8
 * This is the model class for authManager.itemTable
9
 *
10
 * @property string $name
11
 * @property integer $type
12
 * @property string $description
13
 * @property string $rule_name
14
 * @property string $data
15
 * @property integer $created_at
16
 * @property integer $updated_at
17
 */
18
class AuthItem extends \yii\db\ActiveRecord
19
{
20
    /**
21
     * @var array
22
     */
23
    public $roles;
24
    /**
25
     * @var array
26
     */
27
    public $permissions;
28
29
    /**
30
     * @inheritdoc
31
     */
32 30
    public static function tableName()
33
    {
34 30
        return Yii::$app->authManager->itemTable;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40 1
    public function rules()
41
    {
42
        return [
43
            [
44 1
                ['name', 'description'], 'required'
45
            ],
46
            [
47
                ['roles', 'permissions'], 'safe'
48
            ],
49
50
            ['name', 'unique'],
51
            ['name', 'string', 'max' => 64],
52
            ['name', 'match', 'pattern' => '/^[a-z]\w*$/i'],
53
        ];
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 4
    public function attributeLabels()
60
    {
61
        return [
62 4
            'name'        => Yii::t('app', 'Name'),
63 4
            'description' => Yii::t('app', 'Description'),
64 4
            'roles'       => Yii::t('app', 'Inherit role'),
65 4
            'permissions' => Yii::t('app', 'Permissions'),
66
        ];
67
    }
68
69
    /**
70
     * @inheritdoc
71
     * @codeCoverageIgnore
72
     */
73
    public function events()
74
    {
75
        return [
76
            ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete',
77
        ];
78
    }
79
80
    /**
81
     * Is SuperUser?
82
     *
83
     * @return bool
84
     */
85 1
    public function isSuperUser(): bool
86
    {
87 1
        return $this->name === \app\models\User::ROLE_SUPERUSER;
88
    }
89
90
    /**
91
     * Deleting a file from the db and from the file system
92
     *
93
     * @return bool
94
     */
95 1
    public function beforeDelete()
96
    {
97 1
        return !$this->isSuperUser();
98
    }
99
}
100