Completed
Push — master ( 1d9a1d...831484 )
by Andrey
01:26
created

ProfileValidateComponent::setModel()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 19

Duplication

Lines 5
Ratio 17.86 %

Importance

Changes 0
Metric Value
dl 5
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 3
nop 1
1
<?php
2
3
namespace Itstructure\UsersModule\components;
4
5
use Yii;
6
use yii\db\ActiveRecordInterface;
7
use yii\web\IdentityInterface;
8
use yii\base\{Component, InvalidConfigException};
9
use yii\rbac\ManagerInterface;
10
use Itstructure\UsersModule\{
11
    interfaces\ModelInterface,
12
    interfaces\ValidateComponentInterface,
13
    models\ProfileValidate
14
};
15
16
/**
17
 * Class ProfileValidateComponent
18
 * Component class for validation user fields.
19
 *
20
 * @property array $rules
21
 * @property array $attributes
22
 * @property array $attributeLabels
23
 * @property bool  $rbacManage
24
 * @property bool  $customRewrite
25
 * @property array $formFields
26
 * @property array $indexViewColumns
27
 * @property array $detailViewAttributes
28
 * @property ManagerInterface $authManager
29
 *
30
 * @package Itstructure\UsersModule\components
31
 */
32
class ProfileValidateComponent extends Component implements ValidateComponentInterface
33
{
34
    /**
35
     * Validate fields with rules.
36
     *
37
     * @var array
38
     */
39
    private $rules = [];
40
41
    /**
42
     * Attributes.
43
     *
44
     * @var array
45
     */
46
    private $attributes = [];
47
48
    /**
49
     * Attribute labels.
50
     *
51
     * @var array
52
     */
53
    private $attributeLabels = [];
54
55
    /**
56
     * Set manage for roles.
57
     *
58
     * @var bool
59
     */
60
    private $rbacManage = false;
61
62
    /**
63
     * Rewrite rules, labels, attributes by custom.
64
     *
65
     * @var bool
66
     */
67
    private $customRewrite = false;
68
69
    /**
70
     * Form fields for the template.
71
     *
72
     * @var array
73
     */
74
    private $formFields = [];
75
76
    /**
77
     * Columns for GridView widget in index template file.
78
     *
79
     * @var array
80
     */
81
    private $indexViewColumns = [];
82
83
    /**
84
     * Attributes for DetailView widget in view template file.
85
     *
86
     * @var array
87
     */
88
    private $detailViewAttributes = [];
89
90
    /**
91
     * Auth manager.
92
     *
93
     * @var ManagerInterface
94
     */
95
    private $authManager;
96
97
    /**
98
     * Initialize.
99
     */
100
    public function init()
101
    {
102
        if ($this->rbacManage && null === $this->authManager){
103
            throw new InvalidConfigException('The authManager is not defined.');
104
        }
105
    }
106
107
    /**
108
     * Set validate fields with rules.
109
     *
110
     * @param array $rules
111
     */
112
    public function setRules(array $rules): void
113
    {
114
        $this->rules = $rules;
115
    }
116
117
    /**
118
     * Get validate fields with rules.
119
     *
120
     * @return array
121
     */
122
    public function getRules(): array
123
    {
124
        return $this->rules;
125
    }
126
127
    /**
128
     * Set attributes.
129
     *
130
     * @param array $attributes
131
     */
132
    public function setAttributes(array $attributes): void
133
    {
134
        $this->attributes = $attributes;
135
    }
136
137
    /**
138
     * Get attributes.
139
     *
140
     * @return array
141
     */
142
    public function getAttributes(): array
143
    {
144
        return $this->attributes;
145
    }
146
147
    /**
148
     * Set attribute labels.
149
     *
150
     * @param array $attributeLabels
151
     */
152
    public function setAttributeLabels(array $attributeLabels): void
153
    {
154
        $this->attributeLabels = $attributeLabels;
155
    }
156
157
    /**
158
     * Get attribute labels.
159
     *
160
     * @return array
161
     */
162
    public function getAttributeLabels(): array
163
    {
164
        return $this->attributeLabels;
165
    }
166
167
    /**
168
     * Set manage for roles.
169
     *
170
     * @param bool $rbacManage
171
     */
172
    public function setRbacManage(bool $rbacManage): void
173
    {
174
        $this->rbacManage = $rbacManage;
175
    }
176
177
    /**
178
     * Is manage for roles.
179
     *
180
     * @return bool
181
     */
182
    public function getRbacManage(): bool
183
    {
184
        return $this->rbacManage;
185
    }
186
187
    /**
188
     * Set rewrite rules, labels, attributes by custom.
189
     *
190
     * @param bool $customRewrite
191
     */
192
    public function setCustomRewrite(bool $customRewrite): void
193
    {
194
        $this->customRewrite = $customRewrite;
195
    }
196
197
    /**
198
     * Get rewrite rules, labels, attributes by custom.
199
     *
200
     * @return bool
201
     */
202
    public function getCustomRewrite(): bool
203
    {
204
        return $this->customRewrite;
205
    }
206
207
    /**
208
     * Set form fields for the template.
209
     *
210
     * @param array $formFields
211
     */
212
    public function setFormFields(array $formFields): void
213
    {
214
        $this->formFields = $formFields;
215
    }
216
217
    /**
218
     * Get Form fields for the template.
219
     *
220
     * @return array
221
     */
222
    public function getFormFields(): array
223
    {
224
        return $this->formFields;
225
    }
226
227
    /**
228
     * Set columns for GridView widget in index template file.
229
     *
230
     * @param array $indexViewColumns
231
     */
232
    public function setIndexViewColumns(array $indexViewColumns): void
233
    {
234
        $this->indexViewColumns = $indexViewColumns;
235
    }
236
237
    /**
238
     * Get columns for GridView widget in index template file.
239
     *
240
     * @return array
241
     */
242
    public function getIndexViewColumns(): array
243
    {
244
        return $this->indexViewColumns;
245
    }
246
247
    /**
248
     * Set attributes for DetailView widget in view template file.
249
     *
250
     * @param array $detailViewAttributes
251
     */
252
    public function setDetailViewAttributes(array $detailViewAttributes): void
253
    {
254
        $this->detailViewAttributes = $detailViewAttributes;
255
    }
256
257
    /**
258
     * Get attributes for DetailView widget in view template file.
259
     *
260
     * @return array
261
     */
262
    public function getDetailViewAttributes(): array
263
    {
264
        return $this->detailViewAttributes;
265
    }
266
267
    /**
268
     * Set authManager (RBAC).
269
     *
270
     * @param ManagerInterface $authManager
271
     */
272
    public function setAuthManager(ManagerInterface $authManager): void
273
    {
274
        $this->authManager = $authManager;
275
    }
276
277
    /**
278
     * Get authManager (RBAC).
279
     *
280
     * @return ManagerInterface
281
     */
282
    public function getAuthManager(): ManagerInterface
283
    {
284
        return $this->authManager;
285
    }
286
287
    /**
288
     * Set a user model for ProfileValidateComponent validation model.
289
     *
290
     * @param $model
291
     *
292
     * @throws InvalidConfigException
293
     *
294
     * @return ModelInterface
295
     */
296
    public function setModel($model): ModelInterface
297
    {
298
        if (!($model instanceof ActiveRecordInterface)){
299
            $modelClass = (new\ReflectionClass($model));
300
            throw new InvalidConfigException($modelClass->getNamespaceName() .
301
                '\\' . $modelClass->getShortName().' class  must be implemented from yii\db\ActiveRecordInterface.');
302
        }
303
304 View Code Duplication
        if (!($model instanceof IdentityInterface)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
305
            $modelClass = (new\ReflectionClass($model));
306
            throw new InvalidConfigException($modelClass->getNamespaceName() .
307
                '\\' . $modelClass->getShortName().' class  must be implemented from yii\web\IdentityInterface.');
308
        }
309
310
        /** @var ModelInterface $object */
311
        $object = Yii::createObject([
312
            'class' => ProfileValidate::class,
313
            'profileModel' => $model,
314
            'rules' => $this->rules,
315
            'attributes' => $this->attributes,
316
            'attributeLabels' => $this->attributeLabels,
317
            'rbacManage' => $this->rbacManage,
318
            'customRewrite' => $this->customRewrite,
319
            'authManager' => $this->authManager,
320
        ]);
321
322
        return $object;
323
    }
324
}
325