Completed
Push — master ( d106c9...c78ba5 )
by Igor
05:11
created

SignupForm::sendEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 0
crap 2
1
<?php
2
3
namespace app\models\forms;
4
5
use Yii;
6
use app\models\User;
7
use app\models\UserProfile;
8
9
class SignupForm extends \yii\base\Model
10
{
11
    /**
12
     * @var string
13
     */
14
    public $email;
15
    /**
16
     * @var string
17
     */
18
    public $password;
19
    /**
20
     * @var string
21
     */
22
    public $fullName;
23
    /**
24
     * @var \app\models\User
25
     */
26
    private $user;
27
28
    /**
29
     * @inheritdoc
30
     */
31 22
    public function rules()
32
    {
33
        return [
34 22
            ['fullName', 'required'],
35
            ['fullName', 'string', 'max' => 40],
36
37
            ['password', 'required'],
38
            ['password', 'string', 'min' => 6],
39
40
            ['email', 'filter', 'filter' => 'trim'],
41
            ['email', 'required'],
42
            ['email', 'string', 'max' => 255],
43
            ['email', 'email'],
44 22
            ['email', 'unique',
45 22
                'targetClass' => '\app\models\User',
46 22
                'message' => Yii::t('app.validators', 'This email address has already been taken')
47
            ],
48
        ];
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 21
    public function attributeLabels()
55
    {
56 21
        return array_merge((new User())->attributeLabels(), ['fullName' => Yii::t('app', 'Full Name')]);
57
    }
58
59
    /**
60
     * Signs user up
61
     *
62
     * @return \app\models\User
63
     */
64 17
    public function signup()
65
    {
66 17
        if ($this->validate()) {
67 4
            $this->user = new User();
68 4
            $this->user->email = $this->email;
69 4
            $this->user->setPassword($this->password);
70 4
            $this->user->addProfile(['full_name' => $this->fullName]);
71 4
            if ($this->user->save()) {
72 4
                if ($this->user->authorize(true)) {
73 4
                    return $this->user;
74
                }
75
            } // @codeCoverageIgnore
76
        } // @codeCoverageIgnore
77
78 13
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by app\models\forms\SignupForm::signup of type app\models\User.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
79
    }
80
81
    /**
82
     * Sends an email with a link, for confirm the email
83
     *
84
     * @return boolean
85
     */
86 4
    public function sendEmail()
87
    {
88 4
        if (!User::isTokenValid($this->user->email_confirm_token)) {
89 4
            $this->user->generateEmailConfirmToken();
90 4
            $this->user->updateAttributes([
91 4
                'email_confirm_token' => $this->user->email_confirm_token,
92 4
                'date_confirm' => $this->user->date_confirm,
93
            ]);
94
        }
95
96 4
        return Yii::$app->notify->sendMessage(
97 4
            $this->email,
98 4
            Yii::t('app', 'Activate Your Account'),
99 4
            'emailConfirmToken',
100 4
            ['user' => $this->user]
101
        );
102
    }
103
}
104