Completed
Push — master ( e41cc1...feed29 )
by Andrey
08:18
created

Page::rules()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 98

Duplication

Lines 5
Ratio 5.1 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 0
dl 5
loc 98
rs 7.4214
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use Itstructure\MultiLevelMenu\MenuWidget;
8
use Itstructure\MFUploader\behaviors\{BehaviorMediafile, BehaviorAlbum};
9
use Itstructure\MFUploader\models\OwnerAlbum;
10
use Itstructure\MFUploader\models\album\Album;
11
use Itstructure\MFUploader\interfaces\UploadModelInterface;
12
use app\traits\ThumbnailTrait;
13
14
/**
15
 * This is the model class for table "pages".
16
 *
17
 * @property int|string $thumbnail thumbnail(mediafile id or url).
18
 * @property array $image image(array of 'mediafile id' or 'mediafile url').
19
 * @property array $albums Existing album ids.
20
 * @property int $id
21
 * @property string $title
22
 * @property string $description
23
 * @property string $content
24
 * @property string $metaKeys
25
 * @property string $metaDescription
26
 * @property string $created_at
27
 * @property string $updated_at
28
 * @property int $parentId
29
 * @property int $newParentId
30
 * @property string $icon
31
 * @property string $alias
32
 * @property int $active
33
 *
34
 * @package app\models
35
 */
36
class Page extends ActiveRecord
37
{
38
    use ThumbnailTrait;
39
40
    /**
41
     * @var int|string thumbnail(mediafile id or url).
42
     */
43
    public $thumbnail;
44
45
    /**
46
     * @var array image(array of 'mediafile id' or 'mediafile url').
47
     */
48
    public $image;
49
50
    /**
51
     * @var array
52
     */
53
    public $albums = [];
54
55
    /**
56
     * @var int
57
     */
58
    public $newParentId;
59
60
    /**
61
     * Init albums.
62
     */
63
    public function afterFind()
64
    {
65
        $this->albums = $this->getAlbums();
66
67
        parent::afterFind();
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public static function tableName()
74
    {
75
        return 'pages';
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function rules()
82
    {
83
        return [
84
            [
85
                [
86
                    'title',
87
                    'content',
88
                    'active',
89
                    'metaKeys',
90
                    'metaDescription',
91
                ],
92
                'required',
93
            ],
94
            [
95
                [
96
                    'description',
97
                    'content',
98
                ],
99
                'string',
100
            ],
101
            [
102
                [
103
                    'icon',
104
                    'title',
105
                    'metaKeys',
106
                    'alias',
107
                ],
108
                'string',
109
                'max' => 128,
110
            ],
111
            [
112
                [
113
                    'metaDescription',
114
                ],
115
                'string',
116
                'max' => 255,
117
            ],
118
            [
119
                [
120
                    'parentId',
121
                    'newParentId',
122
                    'active'
123
                ],
124
                'integer',
125
            ],
126
            [
127
                'alias',
128
                'filter',
129
                'filter' => function ($value) {
130
                    return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value)));
131
                }
132
            ],
133
            [
134
                'alias',
135
                'unique',
136
                'skipOnError'     => true,
137
                'targetClass'     => static::class,
138
                'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : ''
139
            ],
140
            [
141
                UploadModelInterface::FILE_TYPE_THUMB,
142 View Code Duplication
                function($attribute){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
                    if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){
144
                        $this->addError($attribute, 'Tumbnail content must be a numeric or string.');
145
                    }
146
                },
147
                'skipOnError' => false,
148
            ],
149
            [
150
                UploadModelInterface::FILE_TYPE_IMAGE,
151
                function($attribute) {
152
                    if (!is_array($this->{$attribute})) {
153
                        $this->addError($attribute, 'Image field content must be an array.');
154
                    }
155
                },
156
                'skipOnError' => false,
157
            ],
158
            [
159
                'albums',
160
                'each',
161
                'rule' => ['integer'],
162
            ],
163
            [
164
                'title',
165
                'unique',
166
                'skipOnError'     => true,
167
                'targetClass'     => static::class,
168
                'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : ''
169
            ],
170
            [
171
                [
172
                    'created_at',
173
                    'updated_at',
174
                ],
175
                'safe',
176
            ],
177
        ];
178
    }
179
180
    /**
181
     * @inheritdoc
182
     */
183 View Code Duplication
    public function behaviors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
    {
185
        return ArrayHelper::merge(parent::behaviors(), [
186
            'mediafile' => [
187
                'class' => BehaviorMediafile::class,
188
                'name' => static::tableName(),
189
                'attributes' => [
190
                    UploadModelInterface::FILE_TYPE_THUMB,
191
                    UploadModelInterface::FILE_TYPE_IMAGE,
192
                ],
193
            ],
194
            'albums' => [
195
                'class' => BehaviorAlbum::class,
196
                'name' => static::tableName(),
197
                'attributes' => [
198
                    'albums',
199
                ],
200
            ],
201
        ]);
202
    }
203
204
    /**
205
     * @return array
206
     */
207 View Code Duplication
    public function attributes(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
    {
209
        return [
210
            UploadModelInterface::FILE_TYPE_THUMB,
211
            UploadModelInterface::FILE_TYPE_IMAGE,
212
            'albums',
213
            'id',
214
            'parentId',
215
            'icon',
216
            'alias',
217
            'active',
218
            'newParentId',
219
            'title',
220
            'description',
221
            'content',
222
            'metaKeys',
223
            'metaDescription',
224
            'created_at',
225
            'updated_at',
226
        ];
227
    }
228
229
    /**
230
     * @inheritdoc
231
     */
232 View Code Duplication
    public function attributeLabels()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
    {
234
        return [
235
            'id' => 'ID',
236
            'parentId' => Yii::t('app', 'Parent object'),
237
            'icon' => Yii::t('app', 'Icon'),
238
            'active' => Yii::t('app', 'Active status'),
239
            'alias' => Yii::t('app', 'URL Alias'),
240
            'title' => Yii::t('app', 'Title'),
241
            'description' => Yii::t('app', 'Description'),
242
            'content' => Yii::t('app', 'Content'),
243
            'metaKeys' => Yii::t('app', 'Meta keys'),
244
            'metaDescription' => Yii::t('app', 'Meta description'),
245
            'created_at' => Yii::t('app', 'Created date'),
246
            'updated_at' => Yii::t('app', 'Updated date'),
247
        ];
248
    }
249
250
    /**
251
     * @param bool $insert
252
     *
253
     * @return bool
254
     */
255 View Code Duplication
    public function beforeSave($insert)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
256
    {
257
        if (empty($this->newParentId)) {
258
            $this->parentId = null;
259
260
        } elseif (MenuWidget::checkNewParentId($this, $this->newParentId)) {
261
            $this->parentId = (int)$this->newParentId;
262
        }
263
264
        return parent::beforeSave($insert);
265
    }
266
267
    /**
268
     * Reassigning child objects to their new parent after delete the main model record.
269
     */
270
    public function afterDelete()
271
    {
272
        MenuWidget::afterDeleteMainModel($this);
273
274
        parent::afterDelete();
275
    }
276
277
    /**
278
     * @return array|\yii\db\ActiveRecord[]
279
     */
280
    public static function getMenu()
281
    {
282
        return static::find()->select([
283
            'id', 'parentId', 'title'
284
        ])->all();
285
    }
286
287
    /**
288
     * @return array|\yii\db\ActiveRecord[]
289
     */
290 View Code Duplication
    public static function getActiveMenu()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
291
    {
292
        return static::find()->select([
293
            'id', 'parentId', 'title', 'alias'
294
        ])->where([
295
            'active' => 1
296
        ])->all();
297
    }
298
299
    /**
300
     * Get albums, that catalog has.
301
     *
302
     * @return Album[]
303
     */
304 View Code Duplication
    public function getAlbums()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
305
    {
306
        return OwnerAlbum::getAlbumsQuery([
307
            'owner' => $this->tableName(),
308
            'ownerId' => $this->id,
309
            'ownerAttribute' => 'albums',
310
        ])->all();
311
    }
312
}
313