Completed
Push — master ( ccfeeb...7d9725 )
by Alexey
02:50 queued 46s
created

EmailConfirmForm   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A confirmEmail() 0 9 2
A __construct() 0 10 4
1
<?php
2
3
namespace modules\users\models;
4
5
use yii\base\InvalidArgumentException;
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 \modules\users\models\User|bool
17
     */
18
    private $_user;
19
20
    /**
21
     * Creates a form model given a token.
22
     *
23
     * @param  mixed $token
24
     * @param  array $config
25
     * @throws \yii\base\InvalidArgumentException 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 InvalidArgumentException(Module::t('module', 'Email confirm token cannot be blank.'));
31
        }
32
        $this->_user = User::findByEmailConfirmToken($token);
33
        if (!$this->_user) {
34
            throw new InvalidArgumentException(Module::t('module', 'Wrong Email confirm token.'));
35
        }
36
        parent::__construct($config);
37
    }
38
39
    /**
40
     * Confirm email.
41
     *
42
     * @return bool|\yii\rbac\Assignment if email was confirmed.
43
     * @throws \Exception
44
     */
45
    public function confirmEmail()
46
    {
47
        $user = $this->_user;
48
        $user->status = User::STATUS_ACTIVE;
49
        $user->removeEmailConfirmToken();
50
        if ($user->save()) {
51
            return true;
52
        }
53
        return false;
54
    }
55
}
56