LastVisitBehavior::events()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace modules\users\behavior;
4
5
use Yii;
6
use yii\base\Behavior;
7
use yii\behaviors\TimestampBehavior;
8
use yii\web\Controller;
9
use yii\web\IdentityInterface;
10
use modules\users\models\User;
11
12
/**
13
 * Class LastVisitBehavior
14
 * @package modules\users\behavior
15
 */
16
class LastVisitBehavior extends Behavior
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public function events()
22
    {
23
        return [
24
            Controller::EVENT_AFTER_ACTION => 'afterAction'
25
        ];
26
    }
27
28
    /**
29
     * @return bool
30
     */
31
    public function afterAction()
32
    {
33
        if (!Yii::$app->user->isGuest) {
34
            /** @var IdentityInterface $model */
35
            $model = Yii::$app->user->identity;
36
            /** @var TimestampBehavior|User $model Updates a timestamp attribute to the current timestamp. */
37
            if ($model->profile !== null) {
38
                $model->profile->touch('last_visit');
39
            }
40
        }
41
        return true;
42
    }
43
}
44