Completed
Push — master ( b39695...9f262c )
by Andrey
07:41
created

Category::beforeSave()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 11
loc 11
rs 9.9
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Category::afterDelete() 0 6 1
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use Itstructure\MultiLevelMenu\MenuWidget;
7
8
/**
9
 * This is the model class for table "categories".
10
 *
11
 * @property int $id
12
 * @property string $title
13
 * @property string $description
14
 * @property string $content
15
 * @property string $metaKeys
16
 * @property string $metaDescription
17
 * @property string $created_at
18
 * @property string $updated_at
19
 * @property int $parentId
20
 * @property string $alias
21
 * @property int $active
22
 *
23
 * @package app\models
24
 */
25
class Category extends ActiveRecord
26
{
27
    /**
28
     * @inheritdoc
29
     */
30
    public static function tableName()
31
    {
32
        return 'categories';
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function rules()
39
    {
40
        return [
41
            [
42
                [
43
                    'title',
44
                    'active',
45
                    'metaKeys',
46
                    'metaDescription',
47
                ],
48
                'required',
49
            ],
50
            [
51
                [
52
                    'description',
53
                    'content',
54
                ],
55
                'string',
56
            ],
57
            [
58
                [
59
                    'icon',
60
                    'title',
61
                    'metaKeys',
62
                    'alias',
63
                ],
64
                'string',
65
                'max' => 128,
66
            ],
67
            [
68
                [
69
                    'metaDescription',
70
                ],
71
                'string',
72
                'max' => 255,
73
            ],
74
            [
75
                [
76
                    'parentId',
77
                    'active'
78
                ],
79
                'integer',
80
            ],
81
            [
82
                'parentId',
83
                'filter',
84 View Code Duplication
                'filter' => function ($value) {
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...
85
                    if (empty($value)) {
86
                        return null;
87
                    } else {
88
                        return MenuWidget::checkNewParentId($this, $value) ? $value : $this->getOldAttribute('parentId');
89
                    }
90
                }
91
            ],
92
            [
93
                'alias',
94
                'filter',
95
                'filter' => function ($value) {
96
                    return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value)));
97
                }
98
            ],
99
            [
100
                'alias',
101
                'unique',
102
                'skipOnError'     => true,
103
                'targetClass'     => static::class,
104
                'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : ''
105
            ],
106
            [
107
                'title',
108
                'unique',
109
                'skipOnError'     => true,
110
                'targetClass'     => static::class,
111
                'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : ''
112
            ],
113
            [
114
                [
115
                    'created_at',
116
                    'updated_at',
117
                ],
118
                'safe',
119
            ],
120
        ];
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function attributes(): array
127
    {
128
        return [
129
            'id',
130
            'parentId',
131
            'alias',
132
            'active',
133
            'title',
134
            'description',
135
            'content',
136
            'metaKeys',
137
            'metaDescription',
138
            'created_at',
139
            'updated_at',
140
        ];
141
    }
142
143
    /**
144
     * @inheritdoc
145
     */
146
    public function attributeLabels()
147
    {
148
        return [
149
            'id' => 'ID',
150
            'parentId' => Yii::t('app', 'Parent object'),
151
            'active' => Yii::t('app', 'Active status'),
152
            'alias' => Yii::t('app', 'URL Alias'),
153
            'title' => Yii::t('app', 'Title'),
154
            'description' => Yii::t('app', 'Description'),
155
            'content' => Yii::t('app', 'Content'),
156
            'metaKeys' => Yii::t('app', 'Meta keys'),
157
            'metaDescription' => Yii::t('app', 'Meta description'),
158
            'created_at' => Yii::t('app', 'Created date'),
159
            'updated_at' => Yii::t('app', 'Updated date'),
160
        ];
161
    }
162
163
    /**
164
     * Reassigning child objects to their new parent after delete the main model record.
165
     */
166
    public function afterDelete()
167
    {
168
        MenuWidget::afterDeleteMainModel($this);
169
170
        parent::afterDelete();
171
    }
172
173
    /**
174
     * @return array|\yii\db\ActiveRecord[]
175
     */
176
    public static function getMenu()
177
    {
178
        return static::find()->select([
179
            'id', 'parentId', 'title'
180
        ])->all();
181
    }
182
183
    /**
184
     * @return array|\yii\db\ActiveRecord[]
185
     */
186 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...
187
    {
188
        return static::find()->select([
189
            'id', 'parentId', 'title', 'alias'
190
        ])->where([
191
            'active' => 1
192
        ])->all();
193
    }
194
}
195