Passed
Push — master ( c21c1f...04db2a )
by Wilmer
01:51
created

SignupForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace app\basic\forms;
4
5
use app\basic\models\UserModels;
6
use yii\base\Application;
7
use yii\base\Model;
8
9
/**
10
 * SignupForm is the model behind the signup form Web Application Basic.
11
 **/
12
class SignupForm extends Model
13
{
14
	public $username;
15
	public $email;
16
	public $password;
17
    public $verifyCode;
18
    
19
    protected $app;
20
21
    /**
22
     * __construct
23
     *
24
     * @param Application $app
25
     **/
26
    public function __construct(Application $app)
27
    {
28
        $this->app = $app;
29
    }
30
31
    /**
32
     * rules
33
     *
34
	 * @return array the validation rules.
35
	 **/
36
	public function rules()
37
	{
38
		return [
39
			['username', 'trim'],
40
			['username', 'required'],
41
			['username', 'unique', 'targetClass' => 'app\basic\models\UserModels', 'message' => 'This username has already been taken.'],
42
			['username', 'string', 'min' => 2, 'max' => 255],
43
			['email', 'trim'],
44
			['email', 'required'],
45
			['email', 'email'],
46
			['email', 'string', 'max' => 255],
47
			['email', 'unique', 'targetClass' => 'app\basic\models\UserModels', 'message' => 'This email address has already been taken.'],
48
			['password', 'required'],
49
			['password', 'string', 'min' => 6],
50
			// verifyCode needs to be entered correctly
51
			//['verifyCode', \yii\captcha\CaptchaValidator::class],
52
		];
53
	}
54
55
	/**
56
	 * atributeLabels
57
	 * Translate Atribute Labels.
58
     *
59
	 * @return array customized attribute labels.
60
	 **/
61
	public function attributeLabels()
62
	{
63
		return [
64
			'email' => $this->app->t('basic', 'Email'),
65
			'username' => $this->app->t('basic', 'Username'),
66
			'password' => $this->app->t('basic', 'Password'),
67
			'verifyCode' => $this->app->t('basic', 'VerifyCode'),
68
		];
69
	}
70
71
	/**
72
     * signup
73
	 * Signs user up.
74
	 *
75
	 * @return User|null the saved model or null if saving fails.
0 ignored issues
show
Bug introduced by
The type app\basic\forms\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
	 **/
77
	public function signup()
78
	{
79
		if (!$this->validate()) {
80
			return null;
81
		}
82
83
		$user = new UserModels();
84
85
		$user->username = $this->username;
86
		$user->email = $this->email;
87
		$user->setPassword($this->password);
88
		$user->generateAuthKey();
89
90
		return $user->save() ? $user : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user->save() ? $user : null also could return the type app\basic\models\UserModels which is incompatible with the documented return type app\basic\forms\User|null.
Loading history...
91
	}
92
}
93