Completed
Push — master ( 2facfc...70483f )
by Alexey
11:01
created

EmailConfirmForm   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A confirmEmail() 0 14 2
B __construct() 0 11 5
1
<?php
2
namespace modules\users\models;
3
4
use Yii;
5
use yii\base\InvalidParamException;
6
use yii\base\Model;
7
use modules\users\Module;
8
9
/**
10
 * Class EmailConfirmForm
11
 * @package modules\users\models\frontend
12
 */
13
class EmailConfirmForm extends Model
14
{
15
    /**
16
     * @var null|static
17
     */
18
    private $_user = null;
19
20
    /**
21
     * Creates a form model given a token.
22
     *
23
     * @param  string $token
24
     * @param  array $config
25
     * @throws \yii\base\InvalidParamException if token is empty or not valid
26
     */
27
    public function __construct($token, $config = [])
28
    {
29
        if (empty($token) || !is_string($token)) {
30
            throw new InvalidParamException(Module::t('module', 'Email confirm token cannot be blank.'));
31
        }
32
        $this->_user = BaseUser::findByEmailConfirmToken($token);
33
        if (!$this->_user && $this->_user === null) {
34
            throw new InvalidParamException(Module::t('module', 'Wrong Email confirm token.'));
35
        }
36
        parent::__construct($config);
37
    }
38
39
    /**
40
     * Confirm email.
41
     *
42
     * @return boolean if email was confirmed.
43
     */
44
    public function confirmEmail()
45
    {
46
        /** @var BaseUser $user */
47
        $user = $this->_user;
48
        $user->status = User::STATUS_ACTIVE;
49
        $user->removeEmailConfirmToken();
50
        if ($user->save()) {
51
            // Даём роль по умолчанию
52
            $authManager = Yii::$app->getAuthManager();
53
            $role = $authManager->getRole(\modules\rbac\models\Role::ROLE_DEFAULT);
54
            return $authManager->assign($role, $user->id);
55
        }
56
        return false;
57
    }
58
}
59