Passed
Push — master ( d48f81...6d6ad0 )
by Andrey
04:09
created

Product::rules()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 80
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 44
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 80
rs 8.9048

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\helpers\ArrayHelper;
6
use Itstructure\AdminModule\models\{MultilanguageTrait, Language, ActiveRecord};
7
use Itstructure\AdminModule\interfaces\ModelInterface;
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 "products".
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 $created_at
22
 * @property string $updated_at
23
 * @property int $pageId
24
 * @property string $icon
25
 * @property string $alias
26
 * @property int $active
27
 *
28
 * @property Page $page
29
 * @property ProductLanguage[] $productsLanguages
30
 * @property Language[] $languages
31
 *
32
 * @package app\models
33
 */
34
class Product extends ActiveRecord
35
{
36
    use MultilanguageTrait, ThumbnailTrait;
0 ignored issues
show
introduced by
The trait Itstructure\AdminModule\models\MultilanguageTrait requires some properties which are not provided by app\models\Product: $language, $shortName
Loading history...
Bug introduced by
The trait app\traits\ThumbnailTrait requires the property $alt which is not provided by app\models\Product.
Loading history...
37
38
    /**
39
     * @var int|string thumbnail(mediafile id or url).
40
     */
41
    public $thumbnail;
42
43
    /**
44
     * @var array image(array of 'mediafile id' or 'mediafile url').
45
     */
46
    public $image;
47
48
    /**
49
     * @var array
50
     */
51
    public $albums = [];
52
53
    /**
54
     * Initialize.
55
     * Set albums, that product has.
56
     */
57
    public function init()
58
    {
59
        $this->albums = $this->getAlbums();
60
61
        parent::init();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public static function tableName()
68
    {
69
        return 'products';
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function rules()
76
    {
77
        return [
78
            [
79
                [
80
                    'pageId',
81
                    'active',
82
                    'alias',
83
                ],
84
                'required'
85
            ],
86
            [
87
                [
88
                    'pageId',
89
                    'active',
90
                ],
91
                'integer'
92
            ],
93
            [
94
                [
95
                    'created_at',
96
                    'updated_at'
97
                ],
98
                'safe'
99
            ],
100
            [
101
                'icon',
102
                'string',
103
                'max' => 64
104
            ],
105
            [
106
                'alias',
107
                'string',
108
                'max' => 255,
109
            ],
110
            [
111
                'alias',
112
                'filter',
113
                'filter' => function ($value) {
114
                    return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value)));
115
                }
116
            ],
117
            [
118
                'alias',
119
                'unique',
120
                'skipOnError'     => true,
121
                'targetClass'     => static::class,
122
                'filter' => $this->getScenario() == ModelInterface::SCENARIO_UPDATE ? 'id != '.$this->id : ''
123
            ],
124
            [
125
                [
126
                    'pageId'
127
                ],
128
                'exist',
129
                'skipOnError' => true,
130
                'targetClass' => Page::class,
131
                'targetAttribute' => ['pageId' => 'id']
132
            ],
133
            [
134
                UploadModelInterface::FILE_TYPE_THUMB,
135
                function($attribute){
136
                    if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){
137
                        $this->addError($attribute, 'Tumbnail content must be a numeric or string.');
138
                    }
139
                },
140
                'skipOnError' => false,
141
            ],
142
            [
143
                UploadModelInterface::FILE_TYPE_IMAGE,
144
                function($attribute) {
145
                    if (!is_array($this->{$attribute})) {
146
                        $this->addError($attribute, 'Image field content must be an array.');
147
                    }
148
                },
149
                'skipOnError' => false,
150
            ],
151
            [
152
                'albums',
153
                'each',
154
                'rule' => ['integer'],
155
            ],
156
        ];
157
    }
158
159
    /**
160
     * @inheritdoc
161
     */
162
    public function behaviors()
163
    {
164
        return ArrayHelper::merge(parent::behaviors(), [
165
            'mediafile' => [
166
                'class' => BehaviorMediafile::class,
167
                'name' => static::tableName(),
168
                'attributes' => [
169
                    UploadModelInterface::FILE_TYPE_THUMB,
170
                    UploadModelInterface::FILE_TYPE_IMAGE,
171
                ],
172
            ],
173
            'albums' => [
174
                'class' => BehaviorAlbum::class,
175
                'name' => static::tableName(),
176
                'attributes' => [
177
                    'albums',
178
                ],
179
            ],
180
        ]);
181
    }
182
183
    /**
184
     * @return array
185
     */
186
    public function attributes(): array
187
    {
188
        return [
189
            UploadModelInterface::FILE_TYPE_THUMB,
190
            UploadModelInterface::FILE_TYPE_IMAGE,
191
            'albums',
192
            'id',
193
            'pageId',
194
            'icon',
195
            'alias',
196
            'active',
197
            'created_at',
198
            'updated_at'
199
        ];
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function attributeLabels()
206
    {
207
        return [
208
            'id' => 'ID',
209
            'pageId' => 'Page ID',
210
            'icon' => 'Icon',
211
            'alias' => 'URL Alias',
212
            'active' => 'Active',
213
            'created_at' => 'Created At',
214
            'updated_at' => 'Updated At',
215
        ];
216
    }
217
218
    /**
219
     * @return \yii\db\ActiveQuery
220
     */
221
    public function getPage()
222
    {
223
        return $this->hasOne(Page::class, [
224
            'id' => 'pageId'
225
        ]);
226
    }
227
228
    /**
229
     * @return \yii\db\ActiveQuery
230
     */
231
    public function getProductsLanguages()
232
    {
233
        return $this->hasMany(ProductLanguage::class, [
234
            'products_id' => 'id'
235
        ]);
236
    }
237
238
    /**
239
     * @return \yii\db\ActiveQuery
240
     */
241
    public function getLanguages()
242
    {
243
        return $this->hasMany(Language::class, [
244
            'id' => 'language_id'
245
        ])->viaTable('products_language', [
246
            'products_id' => 'id'
247
        ]);
248
    }
249
250
    /**
251
     * Get albums, that product has.
252
     *
253
     * @return Album[]
254
     */
255
    public function getAlbums()
256
    {
257
        return OwnerAlbum::getAlbumsQuery([
258
            'owner' => $this->tableName(),
259
            'ownerId' => $this->id,
260
            'ownerAttribute' => 'albums',
261
        ])->all();
262
    }
263
}
264