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

NewsForm   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 68.28%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 151
ccs 28
cts 41
cp 0.6828
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B rules() 0 29 1
A attributeLabels() 0 12 1
A setModel() 0 15 1
A model() 0 8 2
A save() 0 19 2
1
<?php
2
3
namespace app\modules\admin\models\forms;
4
5
use Yii;
6
use app\models\News;
7
8
class NewsForm extends \yii\base\Model
9
{
10
    /**
11
     * @var int
12
     */
13
    public $id;
14
    /**
15
     * @var string
16
     */
17
    public $title;
18
    /**
19
     * @var string
20
     */
21
    public $text;
22
    /**
23
     * @var string
24
     */
25
    public $preview ;
26
    /**
27
     * @var string
28
     */
29
    public $date_create;
30
    /**
31
     * @var string
32
     */
33
    public $date_update;
34
    /**
35
     * @var string
36
     */
37
    public $date_pub;
38
    /**
39
     * @var int
40
     */
41
    public $status;
42
    /**
43
     * @var array
44
     */
45
    public $gallery;
46
    /**
47
     * @var array
48
     */
49
    public $galleryTitles;
50
    /**
51
     * @var \app\models\News
52
     */
53
    private $model;
54
55
   /**
56
    * @return array the validation rules.
57
    */
58 2
    public function rules()
59
    {
60
        return [
61
            [
62 2
                ['title', 'text', 'date_pub'],
63
                'required'
64
            ],
65
            [
66
                [
67
                    'title', 'text', 'date_pub', 'preview',
68
                    'gallery', 'galleryTitles', 'status'
69
                ], 'safe'
70
            ],
71
72
            ['title', 'string', 'max' => 255],
73
            ['text', 'string'],
74
75
            [
76
                'date_pub',
77
                'date',
78
                'format' => 'php:Y-m-d H:i:s',
79
                'timestampAttribute' => 'date_pub',
80
                'timestampAttributeFormat' => 'php:Y-m-d H:i:s'
81
            ],
82
83
            ['status', 'integer'],
84 2
            ['status', 'in', 'range' => array_keys(News::getStatuses())],
85
        ];
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 2
    public function attributeLabels()
92
    {
93
        return [
94 2
            'id' => Yii::t('app', 'ID'),
95 2
            'title' => Yii::t('app', 'Title'),
96 2
            'text' => Yii::t('app', 'Text'),
97 2
            'preview' => Yii::t('app', 'Preview'),
98 2
            'gallery' => Yii::t('app', 'Gallery'),
99 2
            'date_pub' => Yii::t('app', 'Date publication'),
100 2
            'status' => Yii::t('app', 'Status'),
101
        ];
102
    }
103
104
    /**
105
     * Set model
106
     *
107
     * @param News $model
108
     */
109 1
    public function setModel(News $model): void
110
    {
111 1
        $this->model = $model;
112
113 1
        $this->id = $model->id;
114 1
        $this->title = $model->title;
115 1
        $this->text = $model->text;
116 1
        $this->preview = $model->preview;
117 1
        $this->gallery = $model->gallery;
118 1
        $this->galleryTitles = $model->galleryTitles;
119 1
        $this->date_create = $model->date_create;
120 1
        $this->date_update = $model->date_update;
121 1
        $this->date_pub = $model->date_pub;
122 1
        $this->status = $model->status;
123 1
    }
124
125
    /**
126
     * Get model
127
     *
128
     * @return News
129
     */
130 2
    public function model(): News
131
    {
132 2
        if ($this->model === null) {
133 1
            $this->model = new News();
134
        }
135
136 2
        return $this->model;
137
    }
138
139
    public function save()
140
    {
141
        $model = $this->model();
142
143
        $model->title = $this->title;
144
        $model->text = $this->text;
145
        $model->preview = $this->preview;
146
        $model->gallery = $this->gallery;
147
        $model->galleryTitles = $this->galleryTitles;
148
        $model->date_pub = $this->date_pub;
149
        $model->status = $this->status;
150
151
        if (!$model->save()) {
152
            throw new \yii\base\Exception(Yii::t('app.msg', 'An error occurred while saving'));
153
        }
154
155
        $this->id = $model->id;
156
        return $model;
157
    }
158
}
159