Completed
Push — master ( c064eb...85769a )
by Igor
03:38
created

UserProfileForm::model()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace app\modules\admin\models\forms;
4
5
use Yii;
6
use app\models\UserProfile;
7
8
class UserProfileForm extends \yii\base\Model
9
{
10
    /**
11
     * @var int
12
     */
13
    public $user_id;
14
    /**
15
     * @var string
16
     */
17
    public $full_name;
18
    /**
19
     * @var string
20
     */
21
    public $photo;
22
    /**
23
     * @var string
24
     */
25
    public $birth_day;
26
    /**
27
     * @var \app\models\UserProfile
28
     */
29
    private $model;
30
31
   /**
32
    * @return array the validation rules.
33
    */
34 1
    public function rules()
35
    {
36
        return [
37
            [
38 1
                ['birth_day', 'photo'], 'safe',
39
            ],
40
41
            ['birth_day', 'date', 'format' => 'php:Y-m-d'],
42
43
            ['full_name', 'string', 'max' => 40],
44
        ];
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 1
    public function attributeLabels()
51
    {
52
        return [
53 1
            'user_id' => Yii::t('app', 'User'),
54 1
            'full_name' => Yii::t('app', 'Full Name'),
55 1
            'photo' => Yii::t('app', 'Photo'),
56 1
            'birth_day' => Yii::t('app', 'Birth Day'),
57
        ];
58
    }
59
60
    /**
61
     * Set model
62
     *
63
     * @param UserProfile $model
64
     */
65 1
    public function setModel(UserProfile $model): void
66
    {
67 1
        $this->model = $model;
68
69 1
        $this->user_id = $model->user_id;
70 1
        $this->full_name = $model->full_name;
71 1
        $this->photo = $model->photo;
72 1
        $this->birth_day = $model->birth_day;
73 1
    }
74
75
    /**
76
     * Get model
77
     *
78
     * @return UserProfile
79
     */
80 1
    public function model(): UserProfile
81
    {
82 1
        if ($this->model === null) {
83
            $this->model = new UserProfile();
84
        }
85
86 1
        return $this->model;
87
    }
88
89
    public function save()
90
    {
91
        $model = $this->model();
92
93
        $model->user_id = $this->user_id;
94
        $model->full_name = $this->full_name;
95
        $model->photo = $this->photo;
96
        $model->birth_day = $this->birth_day;
97
98
        if (!$model->save()) {
99
            throw new \yii\base\Exception(Yii::t('app.msg', 'An error occurred while saving'));
100
        }
101
102
        return $model;
103
    }
104
}
105