Completed
Push — master ( 7e20fc...28c56b )
by Igor
03:33
created

SignupProviderForm   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 15
c 6
b 0
f 0
lcom 1
cbo 6
dl 0
loc 152
ccs 0
cts 82
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A rules() 0 13 1
A attributeLabels() 0 4 1
A makeUploadedFile() 0 14 1
A savePhoto() 0 9 2
A saveUser() 0 16 4
A login() 0 4 1
A signup() 0 8 2
A sendEmail() 0 17 2
1
<?php
2
3
namespace app\models\forms;
4
5
use Yii;
6
use yii\base\DynamicModel;
7
use yii\helpers\FileHelper;
8
use yii\web\UploadedFile;
9
use app\models\User;
10
11
class SignupProviderForm extends \yii\base\Model
12
{
13
    /**
14
     * @var string
15
     */
16
    public $email;
17
    /**
18
     * @var \app\models\User
19
     */
20
    private $user = null;
21
22
    /**
23
     * @param User $user
24
     */
25
    public function __construct($user, $email)
26
    {
27
        $this->user = $user;
28
        $this->email = $email;
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function rules()
35
    {
36
        return [
37
            ['email', 'filter', 'filter' => 'trim'],
38
            ['email', 'required'],
39
            ['email', 'string', 'max' => 255],
40
            ['email', 'email'],
41
            ['email', 'unique',
42
                'targetClass' => '\app\models\User',
43
                'message' => Yii::t('app.validators', 'This email address has already been taken')
44
            ],
45
        ];
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function attributeLabels()
52
    {
53
        return (new User())->attributeLabels();
54
    }
55
56
    /**
57
     * Create manually UploadedFile instance by file path
58
     *
59
     * @param string $path file path
60
     * @return UploadedFile
61
     */
62
    private function makeUploadedFile($path)
63
    {
64
        $tmpFile = tempnam(sys_get_temp_dir(), 'app');
65
        file_put_contents($tmpFile, file_get_contents($path));
66
67
        $uploadedFile = new UploadedFile();
68
        $uploadedFile->name = pathinfo($path, PATHINFO_BASENAME);
69
        $uploadedFile->tempName = $tmpFile;
70
        $uploadedFile->type = FileHelper::getMimeType($tmpFile);
71
        $uploadedFile->size = filesize($tmpFile);
72
        $uploadedFile->error = 0;
73
74
        return $uploadedFile;
75
    }
76
77
    /**
78
     * Save photo
79
     *
80
     * @param \app\models\UserProfile $profile
81
     * @param string $photo
82
     * @return void
83
     */
84
    private function savePhoto($profile, $photo)
85
    {
86
        $file = $this->makeUploadedFile($photo);
87
        $model = new DynamicModel(compact('file'));
88
        $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...
89
        if (!$model->hasErrors()) {
90
            $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...
91
        }
92
    }
93
94
    /**
95
     * Save user
96
     *
97
     * @return bool
98
     */
99
    public function saveUser()
100
    {
101
        $this->user->email = $this->email;
102
103
        $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...
104
        if ($profile->isNewRecord && !empty($profile->photo)) {
105
            $this->savePhoto($profile, $profile->photo);
106
        }
107
108
        if ($this->user->save()) {
109
            return true;
110
        }
111
112
        $this->addErrors($this->user->getErrors());
113
        return false;
114
    }
115
116
    /**
117
     * Login
118
     *
119
     * @return bool
120
     */
121
    public function login()
122
    {
123
        return $this->user->authorize(true);
124
    }
125
126
    /**
127
     * Signs user up
128
     *
129
     * @return bool
130
     */
131
    public function signup()
132
    {
133
        if ($this->validate()) {
134
            return $this->saveUser();
135
        } // @codeCoverageIgnore
136
137
        return false;
138
    }
139
140
    /**
141
     * Sends an email with a link, for confirm the email
142
     *
143
     * @return boolean
144
     */
145
    public function sendEmail()
146
    {
147
        if (!User::isTokenValid($this->user->email_confirm_token)) {
148
            $this->user->generateEmailConfirmToken();
149
            $this->user->updateAttributes([
150
                'email_confirm_token' => $this->user->email_confirm_token,
151
                'date_confirm' => $this->user->date_confirm,
152
            ]);
153
        }
154
155
        return Yii::$app->notify->sendMessage(
156
            $this->email,
157
            Yii::t('app', 'Activate Your Account'),
158
            'emailConfirmToken',
159
            ['user' => $this->user]
160
        );
161
    }
162
}
163