Completed
Push — master ( 831b77...b56796 )
by Antonio
02:41
created

RegistrationController::actionResend()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 9.002

Importance

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