Completed
Push — master ( f45cc5...2edd5b )
by Igor
05:32
created

SignupProviderForm   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 87.5%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 14
c 4
b 0
f 0
lcom 1
cbo 6
dl 0
loc 110
ccs 21
cts 24
cp 0.875
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A rules() 0 13 1
A attributeLabels() 0 4 1
A savePhoto() 0 9 2
B signup() 0 19 7
A sendEmail() 0 17 2
1
<?php
2
3
namespace app\models\forms;
4
5
use Yii;
6
use yii\base\DynamicModel;
7
use app\helpers\Upload;
8
use app\models\User;
9
10
class SignupProviderForm extends \yii\base\Model
11
{
12
    /**
13
     * @var string
14
     */
15
    public $email;
16
    /**
17
     * @var \app\models\User
18
     */
19
    private $user = null;
20
21
    /**
22
     * @param User $user
23
     */
24 8
    public function __construct($user)
25
    {
26 8
        $this->user = $user;
27 8
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32 8
    public function rules()
33
    {
34
        return [
35 8
            ['email', 'filter', 'filter' => 'trim'],
36
            ['email', 'required'],
37
            ['email', 'string', 'max' => 255],
38
            ['email', 'email'],
39 8
            ['email', 'unique',
40 8
                'targetClass' => '\app\models\User',
41 8
                'message' => Yii::t('app.validators', 'This email address has already been taken')
42
            ],
43
        ];
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 7
    public function attributeLabels()
50
    {
51 7
        return (new User())->attributeLabels();
52
    }
53
54
    /**
55
     * Save photo
56
     *
57
     * @param \app\models\UserProfile $profile
58
     * @param string $photo
59
     * @return void
60
     */
61 6
    private function savePhoto($profile, $photo)
62
    {
63 6
        $file = Upload::makeUploadedFile($photo);
64 6
        $model = new DynamicModel(compact('file'));
65 6
        $model->addRule('file', 'image', $profile->fileRules('photo', true))->validate();
0 ignored issues
show
Documentation Bug introduced by
The method fileRules does not exist on object<app\models\UserProfile>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
66
        if (!$model->hasErrors()) {
67
            $profile->createFile('photo', $file->tempName, $model->file->name);
0 ignored issues
show
Documentation Bug introduced by
The method createFile does not exist on object<app\models\UserProfile>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
68
        }
69
    }
70
71
    /**
72
     * Signs user up
73
     *
74
     * @param bool $checkExistEmail
75
     * @return \app\models\User
76
     */
77 8
    public function signup($checkExistEmail = true)
78
    {
79 8
        if ($this->validate($checkExistEmail ? null : [])) {
80 6
            $this->user->email = $this->email;
81
82 6
            $profile = $this->user->profile;
0 ignored issues
show
Documentation introduced by
The property profile does not exist on object<app\models\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
83 6
            if ($profile->isNewRecord && !empty($profile->photo)) {
84 6
                $this->savePhoto($profile, $profile->photo);
85
            }
86
87
            if ($this->user->save()) {
88
                if ($this->user->authorize(true)) {
89
                    return $this->user;
90
                }
91
            } // @codeCoverageIgnore
92
        } // @codeCoverageIgnore
93
94 2
        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\SignupProviderForm::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...
95
    }
96
97
    /**
98
     * Sends an email with a link, for confirm the email
99
     *
100
     * @return boolean
101
     */
102
    public function sendEmail()
103
    {
104
        if (!User::isTokenValid($this->user->email_confirm_token)) {
105
            $this->user->generateEmailConfirmToken();
106
            $this->user->updateAttributes([
107
                'email_confirm_token' => $this->user->email_confirm_token,
108
                'date_confirm' => $this->user->date_confirm,
109
            ]);
110
        }
111
112
        return Yii::$app->notify->sendMessage(
113
            $this->email,
114
            Yii::t('app', 'Activate Your Account'),
115
            'emailConfirmToken',
116
            ['user' => $this->user]
117
        );
118
    }
119
}
120