Completed
Push — master ( ee995a...0c9edb )
by Razon
02:23
created

VerifyEmailForm   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
c 1
b 0
f 0
dl 0
loc 42
ccs 0
cts 16
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 4
A verifyEmail() 0 5 2
1
<?php
2
namespace App\Http\Form;
3
4
use App\Model\User;
5
use yii\base\InvalidArgumentException;
6
use yii\base\Model;
7
8
class VerifyEmailForm extends Model
9
{
10
    /**
11
     * @var string
12
     */
13
    public $token;
14
15
    /**
16
     * @var User
17
     */
18
    private $user;
19
20
21
    /**
22
     * Creates a form model with given token.
23
     *
24
     * @param string $token
25
     * @param array $config name-value pairs that will be used to initialize the object properties
26
     * @throws InvalidArgumentException if token is empty or not valid
27
     */
28
    public function __construct($token, array $config = [])
29
    {
30
        if (empty($token) || !is_string($token)) {
31
            throw new InvalidArgumentException('Verify email token cannot be blank.');
32
        }
33
        $this->user = User::findByVerificationToken($token);
34
        if (!$this->user) {
35
            throw new InvalidArgumentException('Wrong verify email token.');
36
        }
37
        parent::__construct($config);
38
    }
39
40
    /**
41
     * Verify email
42
     *
43
     * @return User|null the saved model or null if saving fails
44
     */
45
    public function verifyEmail()
46
    {
47
        $user = $this->user;
48
        $user->status = User::STATUS_ACTIVE;
49
        return $user->save(false) ? $user : null;
50
    }
51
}
52