Completed
Push — master ( 6ff6d1...70482c )
by Alexey
02:17
created

SignupAction::run()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 23
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\actions\ActionEvent;
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
     * @var
21
     */
22
    public $scenario;
23
24
    public function run()
25
    {
26
        $signupFormClass = Yii::$app->get('auth')->modelMap['signupForm']['class'];
27
        $userClass = Yii::$app->get('auth')->modelMap['user']['class'];
28
        $model = new $signupFormClass($this->scenario);
29
        $user = new $userClass($this->scenario);
30
31
        $isLoad = $model->load(Yii::$app->request->post());
32
33
        $this->trigger(static::EVENT_BEFORE_SIGNUP, new ActionEvent($this, [
34
            'params' => [
35
                'model' => $model,
36
                'user' => $user,
37
            ]
38
        ]));
39
40
        if ($isLoad) {
41
            if ($user = $model->signup($user)) {
42
                $this->trigger(static::EVENT_AFTER_SIGNUP, new ActionEvent($this, [
43
                    'params' => [
44
                        'model' => $model,
45
                        'user' => $user
46
                    ]
47
                ]));
48
            } else {
49
                $this->trigger(static::EVENT_ERROR_SIGNUP, new ActionEvent($this, [
50
                    'params' => [
51
                        'model' => $model,
52
                        'user' => $user
53
                    ]
54
                ]));
55
            }
56
        }
57
58
        return $this->controller->render($this->view, [
59
            'model' => $model,
60
        ]);
61
    }
62
63
    public function trigger($name, Event $event = null)
64
    {
65
        Yii::$app->trigger(sprintf('yiicod.auth.actions.webUser.SignupAction.%s', $name), $event);
66
        return parent::trigger($name, $event);
67
    }
68
}
69