VerifyEmailForm::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 10
cc 4
nc 3
nop 2
crap 20
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