Completed
Pull Request — master (#351)
by
unknown
03:24
created

RegistrationController::actionResend()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 9.002

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 33
cts 34
cp 0.9706
rs 7.3826
c 0
b 0
f 0
cc 9
nc 8
nop 0
crap 9.002

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Controller;
13
14
use Da\User\Event\FormEvent;
15
use Da\User\Event\SocialNetworkConnectEvent;
16
use Da\User\Event\UserEvent;
17
use Da\User\Factory\MailFactory;
18
use Da\User\Form\RegistrationForm;
19
use Da\User\Form\ResendForm;
20
use Da\User\Model\SocialNetworkAccount;
21
use Da\User\Model\User;
22
use Da\User\Query\SocialNetworkAccountQuery;
23
use Da\User\Query\UserQuery;
24
use Da\User\Service\AccountConfirmationService;
25
use Da\User\Service\ResendConfirmationService;
26
use Da\User\Service\UserConfirmationService;
27
use Da\User\Service\UserCreateService;
28
use Da\User\Service\UserRegisterService;
29
use Da\User\Traits\ContainerAwareTrait;
30
use Da\User\Traits\ModuleAwareTrait;
31
use Da\User\Validator\AjaxRequestModelValidator;
32
use Yii;
33
use yii\base\Module;
34
use yii\filters\AccessControl;
35
use yii\web\Controller;
36
use yii\web\NotFoundHttpException;
37
38
class RegistrationController extends Controller
39
{
40
    use ContainerAwareTrait;
41
    use ModuleAwareTrait;
42
43
    protected $userQuery;
44
    protected $socialNetworkAccountQuery;
45
46
    /**
47
     * RegistrationController constructor.
48
     *
49
     * @param string                    $id
50
     * @param Module                    $module
51
     * @param UserQuery                 $userQuery
52
     * @param SocialNetworkAccountQuery $socialNetworkAccountQuery
53
     * @param array                     $config
54
     */
55 8
    public function __construct(
56
        $id,
57
        Module $module,
58
        UserQuery $userQuery,
59
        SocialNetworkAccountQuery $socialNetworkAccountQuery,
60
        array $config = []
61
    ) {
62 8
        $this->userQuery = $userQuery;
63 8
        $this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
64 8
        parent::__construct($id, $module, $config);
65 8
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 8
    public function behaviors()
71
    {
72
        return [
73 8
            'access' => [
74
                'class' => AccessControl::class,
75
                'rules' => [
76
                    [
77
                        'allow' => true,
78
                        'actions' => ['register', 'connect'],
79
                        'roles' => ['?'],
80
                    ],
81
                    [
82
                        'allow' => true,
83
                        'actions' => ['confirm', 'resend'],
84
                        'roles' => ['?', '@'],
85
                    ],
86
                ],
87
            ],
88
        ];
89
    }
90
91 6
    public function actionRegister()
92
    {
93 6
        if (!$this->module->enableRegistration) {
94
            throw new NotFoundHttpException();
95
        }
96
        /** @var RegistrationForm $form */
97 6
        $form = $this->make(RegistrationForm::class);
98
        /** @var FormEvent $event */
99 6
        $event = $this->make(FormEvent::class, [$form]);
100
101 6
        $this->make(AjaxRequestModelValidator::class, [$form])->validate();
102
103 6
        if ($form->load(Yii::$app->request->post()) && $form->validate()) {
104 6
            $this->trigger(FormEvent::EVENT_BEFORE_REGISTER, $event);
105
106
            /** @var User $user */
107 6
            $user = $this->make(User::class, [],
108 6
                  [ 'email'    => $form->attributes['email'],
109 6
                    'username' => $form->attributes['username'],
110 6
                    'password' => $form->attributes['password']
111 6
                  ]);            $user->setScenario('register');
112
113 6
            $mailService = MailFactory::makeWelcomeMailerService($user);
114
115 6
            if ($this->make(UserRegisterService::class, [$user, $mailService])->run()) {
116 6
                if ($this->module->enableEmailConfirmation) {
117 2
                    Yii::$app->session->setFlash(
118 2
                        'info',
119 2
                        Yii::t(
120 2
                            'usuario',
121 2
                            'Your account has been created and a message with further instructions has been sent to your email'
122
                        )
123
                    );
124
                } else {
125 4
                    Yii::$app->session->setFlash('info', Yii::t('usuario', 'Your account has been created'));
126
                }
127 6
                $this->trigger(FormEvent::EVENT_AFTER_REGISTER, $event);
128 6
                return $this->render(
129 6
                    '/shared/message',
130
                    [
131 6
                        'title' => Yii::t('usuario', 'Your account has been created'),
132 6
                        'module' => $this->module,
133
                    ]
134
                );
135
            }
136
            Yii::$app->session->setFlash('danger', Yii::t('usuario', 'User could not be registered.'));
137
        }
138 6
        return $this->render('register', ['model' => $form, 'module' => $this->module]);
139
    }
140
141
    public function actionConnect($code)
142
    {
143
        /** @var SocialNetworkAccount $account */
144
        $account = $this->socialNetworkAccountQuery->whereCode($code)->one();
145
        if ($account === null || $account->getIsConnected()) {
146
            throw new NotFoundHttpException();
147
        }
148
149
        /** @var User $user */
150
        $user = $this->make(
151
            User::class,
152
            [],
153
            ['scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]
154
        );
155
        $event = $this->make(SocialNetworkConnectEvent::class, [$user, $account]);
156
157
        $this->make(AjaxRequestModelValidator::class, [$user])->validate();
158
159
        if ($user->load(Yii::$app->request->post()) && $user->validate()) {
160
            $this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_CONNECT, $event);
161
162
            $mailService = MailFactory::makeWelcomeMailerService($user);
163
            if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
164
                $account->connect($user);
165
                $this->trigger(SocialNetworkConnectEvent::EVENT_AFTER_CONNECT, $event);
166
167
                Yii::$app->user->login($user, $this->module->rememberLoginLifespan);
168
169
                return $this->goBack();
170
            }
171
        }
172
173
        return $this->render(
174
            'connect',
175
            [
176
                'model' => $user,
177
                'account' => $account,
178
            ]
179
        );
180
    }
181
182 1
    public function actionConfirm($id, $code)
183
    {
184
        /** @var User $user */
185 1
        $user = $this->userQuery->whereId($id)->one();
186
187 1
        if ($user === null || $this->module->enableEmailConfirmation === false) {
188
            throw new NotFoundHttpException();
189
        }
190
191
        /** @var UserEvent $event */
192 1
        $event = $this->make(UserEvent::class, [$user]);
193 1
        $userConfirmationService = $this->make(UserConfirmationService::class, [$user]);
194
195 1
        $this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event);
196
197 1
        if ($this->make(AccountConfirmationService::class, [$code, $user, $userConfirmationService])->run()) {
198 1
            Yii::$app->user->login($user, $this->module->rememberLoginLifespan);
199 1
            Yii::$app->session->setFlash('success', Yii::t('usuario', 'Thank you, registration is now complete.'));
200
201 1
            $this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
202
        } else {
203 1
            Yii::$app->session->setFlash(
204 1
                'danger',
205 1
                Yii::t('usuario', 'The confirmation link is invalid or expired. Please try requesting a new one.')
206
            );
207
        }
208
209 1
        return $this->render(
210 1
            '/shared/message',
211
            [
212 1
                'title' => Yii::t('usuario', 'Account confirmation'),
213 1
                'module' => $this->module,
214
            ]
215
        );
216
    }
217
218 1
    public function actionResend()
219
    {
220 1
        if ($this->module->enableEmailConfirmation === false) {
221
            throw new NotFoundHttpException();
222
        }
223
        /** @var ResendForm $form */
224 1
        $form = $this->make(ResendForm::class);
225 1
        $event = $this->make(FormEvent::class, [$form]);
226
227 1
        $this->make(AjaxRequestModelValidator::class, [$form])->validate();
228
229 1
        if ($form->load(Yii::$app->request->post()) && $form->validate()) {
230
            /** @var User $user */
231 1
            $user = $this->userQuery->whereEmail($form->email)->one();
232 1
            $success = true;
233 1
            if ($user !== null) {
234 1
                $this->trigger(FormEvent::EVENT_BEFORE_RESEND, $event);
235 1
                $mailService = MailFactory::makeConfirmationMailerService($user);
236 1
                if ($success = $this->make(ResendConfirmationService::class, [$user, $mailService])->run()) {
237 1
                    $this->trigger(FormEvent::EVENT_AFTER_RESEND, $event);
238 1
                    Yii::$app->session->setFlash(
239 1
                        'info',
240 1
                        Yii::t(
241 1
                            'usuario',
242 1
                            'A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.'
243
                        )
244
                    );
245
                }
246
            }
247 1
            if ($user === null || $success === false) {
248 1
                Yii::$app->session->setFlash(
249 1
                    'danger',
250 1
                    Yii::t(
251 1
                        'usuario',
252 1
                        'We couldn\'t re-send the mail to confirm your address. Please, verify is the correct email or if it has been confirmed already.'
253
                    )
254
                );
255
            }
256
257 1
            return $this->render(
258 1
                '/shared/message',
259
                [
260 1
                    'title' => $success
261 1
                        ? Yii::t('usuario', 'A new confirmation link has been sent')
262 1
                        : Yii::t('usuario', 'Unable to send confirmation link'),
263 1
                    'module' => $this->module,
264
                ]
265
            );
266
        }
267
268 1
        return $this->render(
269 1
            'resend',
270
            [
271 1
                'model' => $form,
272
            ]
273
        );
274
    }
275
}
276