Issues (81)

models/BizRule.php (4 issues)

1
<?php
2
3
namespace toir427\admin\models;
4
5
use Yii;
6
use yii\rbac\Rule;
7
use toir427\admin\components\Configs;
8
9
/**
10
 * BizRule
11
 *
12
 * @author Misbahul D Munir <[email protected]>
13
 * @since 1.0
14
 */
15
class BizRule extends \yii\base\Model
16
{
17
    /**
18
     * @var string name of the rule
19
     */
20
    public $name;
21
22
    /**
23
     * @var integer UNIX timestamp representing the rule creation time
24
     */
25
    public $createdAt;
26
27
    /**
28
     * @var integer UNIX timestamp representing the rule updating time
29
     */
30
    public $updatedAt;
31
32
    /**
33
     * @var string Rule classname.
34
     */
35
    public $className;
36
37
    /**
38
     * @var Rule
39
     */
40
    private $_item;
41
42
    /**
43
     * Initilaize object
44
     * @param \yii\rbac\Rule $item
45
     * @param array $config
46
     */
47
    public function __construct($item, $config = [])
48
    {
49
        $this->_item = $item;
50
        if ($item !== null) {
51
            $this->name = $item->name;
52
            $this->className = get_class($item);
53
        }
54
        parent::__construct($config);
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function rules()
61
    {
62
        return [
63
            [['name', 'className'], 'required'],
64
            [['className'], 'string'],
65
            [['className'], 'classExists']
66
        ];
67
    }
68
69
    /**
70
     * Validate class exists
71
     */
72
    public function classExists()
73
    {
74
        if (!class_exists($this->className)) {
75
            $message = Yii::t('rbac-admin', "Unknown class '{class}'", ['class' => $this->className]);
76
            $this->addError('className', $message);
77
            return;
78
        }
79
        if (!is_subclass_of($this->className, Rule::className())) {
80
            $message = Yii::t('rbac-admin', "'{class}' must extend from 'yii\rbac\Rule' or its child class", [
81
                    'class' => $this->className]);
82
            $this->addError('className', $message);
83
        }
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function attributeLabels()
90
    {
91
        return [
92
            'name' => Yii::t('rbac-admin', 'Name'),
93
            'className' => Yii::t('rbac-admin', 'Class Name'),
94
        ];
95
    }
96
97
    /**
98
     * Check if new record.
99
     * @return boolean
100
     */
101
    public function getIsNewRecord()
102
    {
103
        return $this->_item === null;
104
    }
105
106
    /**
107
     * Find model by id
108
     * @param type $id
0 ignored issues
show
The type toir427\admin\models\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
109
     * @return null|static
110
     */
111
    public static function find($id)
112
    {
113
        $item = Configs::authManager()->getRule($id);
114
        if ($item !== null) {
115
            return new static($item);
116
        }
117
118
        return null;
119
    }
120
121
    /**
122
     * Save model to authManager
123
     * @return boolean
124
     */
125
    public function save()
126
    {
127
        if ($this->validate()) {
128
            $manager = Configs::authManager();
129
            $class = $this->className;
130
            if ($this->_item === null) {
131
                $this->_item = new $class();
132
                $isNew = true;
133
            } else {
134
                $isNew = false;
135
                $oldName = $this->_item->name;
136
            }
137
            $this->_item->name = $this->name;
138
139
            if ($isNew) {
140
                $manager->add($this->_item);
141
            } else {
142
                $manager->update($oldName, $this->_item);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $oldName does not seem to be defined for all execution paths leading up to this point.
Loading history...
143
            }
144
145
            return true;
146
        } else {
147
            return false;
148
        }
149
    }
150
151
    /**
152
     * Get item
153
     * @return Item
0 ignored issues
show
The type toir427\admin\models\Item was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
154
     */
155
    public function getItem()
156
    {
157
        return $this->_item;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_item returns the type yii\rbac\Rule which is incompatible with the documented return type toir427\admin\models\Item.
Loading history...
158
    }
159
}
160