Completed
Push — master ( 142578...5f4f14 )
by Igor
06:03
created

SignupProviderForm   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 108
ccs 39
cts 39
cp 1
rs 10
c 3
b 0
f 0

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 10 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 8
    public function __construct($user)
22
    {
23 8
        $this->user = $user;
24 8
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29 8
    public function rules()
30
    {
31
        return [
32 8
            ['email', 'filter', 'filter' => 'trim'],
33
            ['email', 'required'],
34
            ['email', 'string', 'max' => 255],
35
            ['email', 'email'],
36 8
            ['email', 'unique',
37 8
                'targetClass' => '\app\models\User',
38 8
                'message' => Yii::t('app.validators', 'This email address has already been taken')
39
            ],
40
        ];
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 7
    public function attributeLabels()
47
    {
48 7
        return (new User())->attributeLabels();
49
    }
50
51
    /**
52
     * Save photo
53
     *
54
     * @param \app\models\UserProfile $profile
55
     * @param string $photo
56
     * @return void
57
     */
58 6
    private function savePhoto($profile, $photo)
59
    {
60 6
        $file = Upload::makeUploadedFile($photo);
61 6
        $model = new DynamicModel(compact('file'));
62 6
        $model->addRule('file', 'image', $profile->getFileRules('photo', true))->validate();
0 ignored issues
show
Documentation Bug introduced by
The method getFileRules 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...
63 6
        if (!$model->hasErrors()) {
64 5
            $file = $profile->createFile('photo', $file->tempName, 'photo', true);
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...
65 5
            $profile->photo = $file->id;
66
        }
67 6
    }
68
69
    /**
70
     * Signs user up
71
     *
72
     * @param bool $checkExistEmail
73
     * @return \app\models\User
74
     */
75 8
    public function signup($checkExistEmail = true)
76
    {
77 8
        if ($this->validate($checkExistEmail ? null : [])) {
78 6
            $this->user->email = $this->email;
79
80 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...
81 6
            if ($profile->isNewRecord && !empty($profile->photo)) {
82 6
                $this->savePhoto($profile, $profile->photo);
83
            }
84
85 6
            if ($this->user->save()) {
86 6
                if ($this->user->authorize(true)) {
87 6
                    return $this->user;
88
                }
89
            } // @codeCoverageIgnore
90
        } // @codeCoverageIgnore
91
92 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...
93
    }
94
95
    /**
96
     * Sends an email with a link, for confirm the email
97
     *
98
     * @return boolean
99
     */
100 5
    public function sendEmail()
101
    {
102 5
        if (!User::isTokenValid($this->user->email_confirm_token)) {
103 5
            $this->user->generateEmailConfirmToken();
104 5
            $this->user->updateAttributes([
105 5
                'email_confirm_token' => $this->user->email_confirm_token,
106 5
                'date_confirm' => $this->user->date_confirm,
107
            ]);
108
        }
109
110 5
        return Yii::$app->notify->sendMessage(
111 5
            $this->email,
112 5
            Yii::t('app', 'Activate Your Account'),
113 5
            'emailConfirmToken',
114 5
            ['user' => $this->user]
115
        );
116
    }
117
}
118