UserProfileForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 73.91%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 92
ccs 17
cts 23
cp 0.7391
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A rules() 0 8 1
A attributeLabels() 0 8 1
A model() 0 4 1
A save() 0 14 2
1
<?php
2
3
namespace app\modules\admin\models\forms;
4
5
use Yii;
6
use yii\base\Exception;
7
use app\models\entity\UserProfile;
8
9
class UserProfileForm extends \yii\base\Model
10
{
11
    /**
12
     * @var int
13
     */
14
    public $user_id;
15
    /**
16
     * @var string
17
     */
18
    public $full_name;
19
    /**
20
     * @var string
21
     */
22
    public $photo;
23
    /**
24
     * @var \app\models\entity\UserProfile
25
     */
26
    private $model;
27
28
    /**
29
     * Creates a form model given a user profile
30
     *
31
     * @param UserProfile $model
32
     * @param array $config name-value pairs that will be used to initialize the object properties
33
     */
34
    public function __construct(UserProfile $model, $config = [])
35 1
    {
36
        $this->model = $model;
37
38 1
        $this->user_id = $model->user_id;
39
        $this->full_name = $model->full_name;
40
        $this->photo = $model->photo;
41
42
        parent::__construct($config);
43
    }
44
45
46
   /**
47
    * @return array The validation rules
48
    */
49 1
    public function rules()
50
    {
51
        return [
52 1
            [['photo'], 'safe'],
53 1
54 1
            ['full_name', 'string', 'max' => 40],
55 1
        ];
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function attributeLabels()
62
    {
63
        return [
64 1
            'user_id' => Yii::t('app', 'User'),
65
            'full_name' => Yii::t('app', 'Name'),
66 1
            'photo' => Yii::t('app', 'Photo'),
67
        ];
68 1
    }
69 1
70 1
    /**
71 1
     * Get model
72 1
     *
73
     * @return UserProfile
74
     */
75
    public function model(): UserProfile
76
    {
77
        return $this->model;
78
    }
79 1
80
    /**
81 1
     * Save profile
82
     *
83
     * @throws Exception
84
     * @return UserProfile
85 1
     */
86
    public function save(): UserProfile
87
    {
88
        $model = $this->model();
89
90
        $model->user_id = $this->user_id;
91
        $model->full_name = $this->full_name;
92
        $model->photo = $this->photo;
93
94
        if (!$model->save()) {
95
            throw new Exception(Yii::t('app', 'An error occurred while saving profile'));
96
        }
97
98
        return $model;
99
    }
100
}
101