LoginAction::run()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 12
nc 3
nop 0
1
<?php
2
3
namespace yiicod\auth\actions\webUser;
4
5
use Yii;
6
use yii\base\Action;
7
use yii\base\Event;
8
use yiicod\auth\events\LoginEvent;
9
10
class LoginAction extends Action
11
{
12
    const EVENT_BEFORE_LOGIN = 'beforeLogin';
13
    const EVENT_AFTER_LOGIN = 'afterLogin';
14
    const EVENT_ERROR_LOGIN = 'errorLogin';
15
16
    public $view = '@yiicod/yii2-auth/views/webUser/login';
17
18
    /**
19
     * Model scenario
20
     *
21
     * @var
22
     */
23
    public $scenario;
24
25
    public function run()
26
    {
27
        $loginFormClass = Yii::$app->get('auth')->modelMap['loginForm']['class'];
28
        $model = new $loginFormClass($this->scenario);
29
30
        $isLoad = $model->load(Yii::$app->request->post());
31
32
        Event::trigger(self::class, static::EVENT_BEFORE_LOGIN, new LoginEvent($this, $model, ['sender' => $this]));
33
34
        if ($isLoad) {
35
            if ($model->login()) {
36
                Event::trigger(self::class, static::EVENT_AFTER_LOGIN, new LoginEvent($this, $model, ['sender' => $this]));
37
            } else {
38
                Event::trigger(self::class, static::EVENT_ERROR_LOGIN, new LoginEvent($this, $model, ['sender' => $this]));
39
            }
40
        }
41
42
        return $this->controller->render($this->view, [
43
            'model' => $model,
44
        ]);
45
    }
46
}
47