Completed
Push — master ( 2dbc7f...8c4f76 )
by Antonio
04:11
created

EmailChangeService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Da\User\Service;
4
5
use Da\User\Contracts\MailChangeStrategyInterface;
6
use Da\User\Contracts\ServiceInterface;
7
use Da\User\Model\Token;
8
use Da\User\Model\User;
9
use Da\User\Query\TokenQuery;
10
use Da\User\Query\UserQuery;
11
use Da\User\Traits\ModuleAwareTrait;
12
use Yii;
13
14
class EmailChangeService implements ServiceInterface
15
{
16
    use ModuleAwareTrait;
17
18
    protected $code;
19
    protected $model;
20
    protected $tokenQuery;
21
    protected $userQuery;
22
23 1
    public function __construct($code, User $model, TokenQuery $tokenQuery, UserQuery $userQuery)
24
    {
25 1
        $this->code = $code;
26 1
        $this->model = $model;
27 1
        $this->tokenQuery = $tokenQuery;
28 1
        $this->userQuery = $userQuery;
29 1
    }
30
31 1
    public function run()
32
    {
33
        /** @var Token $token */
34 1
        $token = $this->tokenQuery
35 1
            ->whereUserId($this->model->id)
36 1
            ->whereCode($this->code)
37 1
            ->whereIsTypes([Token::TYPE_CONFIRM_NEW_EMAIL, Token::TYPE_CONFIRM_OLD_EMAIL])
38 1
            ->one();
39
40 1
        if ($token === null || $token->getIsExpired()) {
41
            Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Your confirmation token is invalid or expired'));
42
43
            return false;
44
        }
45 1
        $token->delete();
46 1
        if (empty($this->model->unconfirmed_email)) {
47
            Yii::$app->session->setFlash('danger', Yii::t('usuario', 'An error occurred processing your request'));
48 1
        } elseif ($this->userQuery->whereEmail($this->model->unconfirmed_email)->exists() === false) {
49 1
            if ($this->getModule()->emailChangeStrategy === MailChangeStrategyInterface::TYPE_SECURE) {
50
                if ($token->type === Token::TYPE_CONFIRM_NEW_EMAIL) {
51
                    $this->model->flags |= User::NEW_EMAIL_CONFIRMED;
52
                    Yii::$app->session->setFlash(
53
                        'success',
54
                        Yii::t(
55
                            'usuario',
56
                            'Awesome, almost there. ' .
57
                            'Now you need to click the confirmation link sent to your old email address.'
58
                        )
59
                    );
60
                } elseif ($token->type === Token::TYPE_CONFIRM_OLD_EMAIL) {
61
                    $this->model->flags |= User::OLD_EMAIL_CONFIRMED;
62
                    Yii::$app->session->setFlash(
63
                        'success',
64
                        Yii::t(
65
                            'usuario',
66
                            'Awesome, almost there. ' .
67
                            'Now you need to click the confirmation link sent to your new email address.'
68
                        )
69
                    );
70
                }
71
            }
72 1
            if ($this->getModule()->emailChangeStrategy === MailChangeStrategyInterface::TYPE_DEFAULT
73 1
                || ($this->model->flags & User::NEW_EMAIL_CONFIRMED & $this->model->flags & User::OLD_EMAIL_CONFIRMED)
74
            ) {
75 1
                $this->model->email = $this->model->unconfirmed_email;
76 1
                $this->model->unconfirmed_email = null;
77 1
                Yii::$app->session->setFlash('success', Yii::t('usuario', 'Your email address has been changed'));
78
            }
79
80 1
            return $this->model->save(false);
81
        }
82
83
84
        return false;
85
    }
86
}
87