UserRegisterService::run()   B
last analyzed

Complexity

Conditions 9
Paths 155

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 12.5599

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 22
cts 34
cp 0.6471
rs 7.0595
c 0
b 0
f 0
cc 9
nc 155
nop 0
crap 12.5599

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\Service;
13
14
use Da\User\Contracts\ServiceInterface;
15
use Da\User\Event\UserEvent;
16
use Da\User\Factory\TokenFactory;
17
use Da\User\Helper\SecurityHelper;
18
use Da\User\Model\User;
19
use Da\User\Traits\MailAwareTrait;
20
use Da\User\Traits\ModuleAwareTrait;
21
use Exception;
22
use Yii;
23
use yii\base\InvalidCallException;
24
25
class UserRegisterService implements ServiceInterface
26
{
27
    use ModuleAwareTrait;
28
    use MailAwareTrait;
29
30
    protected $model;
31
    protected $securityHelper;
32
    protected $mailService;
33
34 6
    public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper)
35
    {
36 6
        $this->model = $model;
37 6
        $this->mailService = $mailService;
38 6
        $this->securityHelper = $securityHelper;
39 6
    }
40
41 6
    public function run()
42
    {
43 6
        $model = $this->model;
44
45 6
        if ($model->getIsNewRecord() === false) {
46
            throw new InvalidCallException('Cannot register user from an existing one.');
47
        }
48
49 6
        $transaction = $model::getDb()->beginTransaction();
50
51
        try {
52 6
            $model->confirmed_at = $this->getModule()->enableEmailConfirmation ? null : time();
53 6
            $model->password = $this->getModule()->generatePasswords
54 2
                ? $this->securityHelper->generatePassword(8)
55 4
                : $model->password;
56
57 6
            $event = $this->make(UserEvent::class, [$model]);
58 6
            $model->trigger(UserEvent::EVENT_BEFORE_REGISTER, $event);
59
60 6
            if (!$model->save()) {
61
                $transaction->rollBack();
62
                return false;
63
            }
64
65 6
            if ($this->getModule()->enableEmailConfirmation) {
66 2
                $token = TokenFactory::makeConfirmationToken($model->id);
67
            }
68
69 6
            if (isset($token)) {
70 2
                $this->mailService->setViewParam('token', $token);
71
            }
72 6
            if (!$this->sendMail($model)) {
73
                Yii::$app->session->setFlash(
74
                    'warning',
75
                    Yii::t(
76
                        'usuario',
77
                        'Error sending registration message to "{email}". Please try again later.',
78
                        ['email' => $model->email]
79
                    )
80
                );
81
                $transaction->rollBack();
82
                return false;
83
            }
84 6
            $model->trigger(UserEvent::EVENT_AFTER_REGISTER, $event);
85
86 6
            $transaction->commit();
87
88
            return true;
89 6
        } catch (Exception $e) {
90 6
            $transaction->rollBack();
91 6
            Yii::error($e->getMessage(), 'usuario');
92
93 6
            return false;
94
        }
95
    }
96
}
97