CreatorBehavior   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 8
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 7
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A init() 0 10 2
1
<?php
2
namespace App\Behavior;
3
4
use Yii;
5
use yii\base\ModelEvent;
6
use yii\behaviors\AttributesBehavior;
7
use yii\db\ActiveRecord;
8
9
class CreatorBehavior extends AttributesBehavior
10
{
11
    public $creatorAttribute = 'creator';
12
13
    public function init()
14
    {
15
        parent::init();
16
17
        $this->attributes = [
18
            $this->creatorAttribute => [
19
                ActiveRecord::EVENT_BEFORE_VALIDATE => function (ModelEvent $event, $attribute) {
20
                    /** @var \yii\db\ActiveRecord $model */
21
                    $model = $event->sender;
22
                    return $model->getIsNewRecord() ? Yii::$app->getUser()->getId() : $model->$attribute;
23
                },
24
            ],
25
        ];
26
    }
27
}
28