EmailConfirmForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 16
c 1
b 0
f 1
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A confirmEmail() 0 9 2
A __construct() 0 13 4
1
<?php
2
3
namespace modules\users\models;
4
5
use Exception;
6
use yii\base\InvalidArgumentException;
7
use yii\base\Model;
8
use modules\users\Module;
9
use yii\rbac\Assignment;
10
11
/**
12
 * Class EmailConfirmForm
13
 * @package modules\users\models\frontend
14
 */
15
class EmailConfirmForm extends Model
16
{
17
    /**
18
     * @var User|bool
19
     */
20
    private $user;
21
22
    /**
23
     * Creates a form model given a token.
24
     *
25
     * @param  mixed $token
26
     * @param  array $config
27
     * @throws InvalidArgumentException if token is empty or not valid
28
     */
29
    public function __construct($token = '', $config = [])
30
    {
31
        if (empty($token) || !is_string($token)) {
32
            throw new InvalidArgumentException(Module::t(
33
                'module',
34
                'Email confirm token cannot be blank.'
35
            ));
36
        }
37
        $this->user = User::findByEmailConfirmToken($token);
38
        if (!$this->user) {
39
            throw new InvalidArgumentException(Module::t('module', 'Wrong Email confirm token.'));
40
        }
41
        parent::__construct($config);
42
    }
43
44
    /**
45
     * Confirm email.
46
     *
47
     * @return bool|Assignment if email was confirmed.
48
     * @throws Exception
49
     */
50
    public function confirmEmail()
51
    {
52
        $user = $this->user;
53
        $user->status = User::STATUS_ACTIVE;
54
        $user->removeEmailConfirmToken();
55
        if ($user->save(false)) {
56
            return true;
57
        }
58
        return false;
59
    }
60
}
61