SignupAction::run()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 15
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\SignupEvent;
9
10
class SignupAction extends Action
11
{
12
    const EVENT_BEFORE_SIGNUP = 'beforeSignup';
13
    const EVENT_AFTER_SIGNUP = 'afterSignup';
14
    const EVENT_ERROR_SIGNUP = 'errorSignup';
15
16
    public $view = '@yiicod/yii2-auth/views/webUser/signup';
17
18
    /**
19
     * Model scenario
20
     *
21
     * @var
22
     */
23
    public $scenario;
24
25
    public function run()
26
    {
27
        $signupFormClass = Yii::$app->get('auth')->modelMap['signupForm']['class'];
28
        $userClass = Yii::$app->get('auth')->modelMap['user']['class'];
29
        $model = new $signupFormClass($this->scenario);
30
        $user = new $userClass($this->scenario);
31
32
        $isLoad = $model->load(Yii::$app->request->post());
33
34
        Event::trigger(self::class, static::EVENT_BEFORE_SIGNUP, new SignupEvent($this, $user, $model, ['sender' => $this]));
35
36
        if ($isLoad) {
37
            if ($user = $model->signup($user)) {
38
                Event::trigger(self::class, static::EVENT_AFTER_SIGNUP, new SignupEvent($this, $user, $model, ['sender' => $this]));
39
            } else {
40
                Event::trigger(self::class, static::EVENT_ERROR_SIGNUP, new SignupEvent($this, $user, $model, ['sender' => $this]));
41
            }
42
        }
43
44
        return $this->controller->render($this->view, [
45
            'model' => $model,
46
            'user' => $user,
47
        ]);
48
    }
49
}
50