Completed
Pull Request — master (#39)
by
unknown
05:19 queued 02:35
created

Offer::getYmlAttributes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: execut
5
 * Date: 4/28/17
6
 * Time: 5:02 PM
7
 */
8
9
namespace pastuhov\ymlcatalog\models;
10
11
12
abstract class Offer extends BaseModel
13
{
14
    const NUMBER_LIMIT = 99999999999999999999;
15
    /**
16
     * @inheritdoc
17
     */
18
    public static $tag = 'offer';
19
20
    public $id;
21
    public $cbid;
22
    public $bid;
23
    public $fee;
24
25
    public $url;
26
    public $price;
27
    public $oldprice;
28
    public $currencyId;
29
    public $categoryId;
30
    public $pictures = [];
31
    public $delivery;
32
33
    /**
34
     * Опции доставки
35
     *
36
     * @var array
37
     */
38
    public $deliveryOptions = [];
39
40
    public $pickup;
41
    public $available;
42
    public $store;
43
44
    public $outlets = [];
45
46
    public $description;
47
    public $sales_notes;
48
49
    public $minQuantity;
50
    public $stepQuantity;
51
52
    public $manufacturer_warranty;
53
    public $country_of_origin;
54
    public $adult;
55
    public $age;
56
    public $barcode;
57
    public $cpa;
58
    public $params = [];
59
60
    public $expiry;
61
    public $weight;
62
63
64
    /**
65
     * Элемент предназначен для указания габаритов товара (длина, ширина, высота) в упаковке. Размеры указываются в сантиметрах.
66
     *
67
     * @var array
68
     */
69
    public $dimensionsValues = [];
70
71
    public $downloadable;
72
    public $group_id;
73
74
    /**
75
     * Элемент предназначен для передачи рекомендованных товаров.
76
     *
77
     * @var array
78
     */
79
    public $recValues = [];
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public static function getTagProperties() {
85
        return [
86
            'id',
87
            'cbid',
88
            'bid',
89
            'available',
90
            'fee',
91
            'group_id'
92
        ];
93
    }
94
95
    public function getRec() {
96
        if (!$this->recValues) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->recValues of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97
            return null;
98
        }
99
100
        return implode(',', $this->recValues);
101
    }
102
103
    public function getDimensions() {
104
        if (!$this->dimensionsValues) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->dimensionsValues of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
            return null;
106
        }
107
108
        return implode('/', $this->dimensionsValues);
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function getYmlAttributes()
115
    {
116
        return [
117
            'fee',
118
            'url',
119
            'price',
120
            'oldprice',
121
            'currencyId',
122
            'categoryId',
123
            'store',
124
            'pickup',
125
            'delivery',
126
            'description',
127
            'sales_notes',
128
            'min-quantity' => 'minQuantity',
129
            'step-quantity' => 'stepQuantity',
130
            'expiry',
131
            'manufacturer_warranty',
132
            'country_of_origin',
133
            'adult',
134
            'age',
135
            'barcode',
136
            'cpa',
137
            'weight',
138
            'dimensions',
139
            'downloadable',
140
            'group_id',
141
            'rec',
142
        ];
143
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148
    public function rules()
149
    {
150
        return [
151
            [
152
                ['id', 'url', 'price', 'currencyId', 'categoryId'],
153
                'required',
154
            ],
155
            [
156
                ['id', 'cbid', 'bid', 'fee', 'minQuantity'],
157
                'integer',
158
                'min' => 0,
159
                'max' => self::NUMBER_LIMIT,
160
            ],
161
            [
162
                ['url'],
163
                'url',
164
            ],
165
            [
166
                ['url'],
167
                'string',
168
                'max' => 512,
169
            ],
170
            [
171
                ['price', 'oldprice'],
172
                'double',
173
            ],
174
            [
175
                'oldprice',
176
                function ($attribute, $value) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
177
                    if ($this->$attribute <= $this->price) {
178
                        $this->addError($attribute, 'Old price must be greater than price');
179
                    }
180
                }
181
            ],
182
            [
183
                [
184
                    'currencyId',
185
                ],
186
                'in',
187
                'range' => [
188
                    'RUR',
189
                    'RUB',
190
                    'UAH',
191
                    'BYR',
192
                    'KZT',
193
                    'USD',
194
                    'EUR'
195
                ],
196
            ],
197
            [
198
                ['categoryId'],
199
                'integer',
200
                'min' => 0,
201
                'max' => 999999999999999999,
202
            ],
203
            [
204
                ['pictures'],
205
                function ($attribute, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
206
                    if (count($this->pictures) > 10) {
207
                        $this->addError('pictures', 'maximum 10 pictures');
208
                    }
209
                }
210
            ],
211
            [
212
                ['pictures'],
213
                'each',
214
                'rule' => ['string', 'max' => 512],
215
            ],
216
            [
217
                ['pictures'],
218
                'each',
219
                'rule' => ['url']
220
            ],
221
            [
222
                ['delivery', 'pickup', 'available', 'store', 'manufacturer_warranty', 'downloadable', 'adult',],
223
                'in',
224
                'range' => [
225
                    'true',
226
                    'false'
227
                ]
228
            ],
229
            [
230
                ['deliveryOptions'],
231
                function ($attribute, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
232
                    $value = $this->$attribute;
233
                    if (empty($value)) {
234
                        if ($this->delivery === 'true') {
235
                            $this->addError($attribute, 'Delivery options is required while delivery equal \'true\'');
236
                        }
237
                    } else {
238
                        if (count($value) > 5) {
239
                            $this->addError($attribute, 'Delivery options count greater whan 5');
240
                        }
241
                    }
242
                },
243
                'skipOnEmpty' => false,
244
            ],
245
            [
246
                ['outlets'],
247
                'safe',
248
            ],
249
            [
250
                ['description'],
251
                'string',
252
                'max' => 3000,
253
            ],
254
            [
255
                ['sales_notes'],
256
                'string',
257
                'max' => 50,
258
            ],
259
            [
260
                ['stepQuantity'],
261
                'integer',
262
                'min' => 1,
263
                'max' => self::NUMBER_LIMIT,
264
            ],
265
            [
266
                ['barcode'],
267
                'string',
268
                'max' => 250,
269
            ],
270
//            [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
271
//                ['age'],
272
//                'in',
273
//                'range' => [
274
//                    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
275
//                ],
276
//            ],
277
            [
278
                ['cpa'],
279
                'integer',
280
                'min' => 0,
281
                'max' => 1,
282
            ],
283
            [
284
                ['params'],
285
                'each',
286
                'rule' => ['string']
287
            ],
288
            [
289
                ['expiry'],
290
                'date',
291
                'format' => 'yyyy-MM-ddTHH:mm',
292
            ],
293
            [
294
                ['weight'],
295
                'number',
296
                'numberPattern' => '/^\[0-9]*\.?[0-9]+\s*$/'
297
            ],
298
            [
299
                'dimensionsValues',
300
                'each',
301
                'rule' => ['number', 'numberPattern' => '/^[0-9]*\.?[0-9]+\s*$/'],
302
            ],
303
            [
304
                'dimensionsValues',
305 View Code Duplication
                function ($attribute, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
306
                    $count = count($this->$attribute);
307
                    if ($count && $count != 3) {
308
                        $this->addError($attribute, 'dimensions must have 3 values');
309
                    }
310
                }
311
            ],
312
            [
313
                'recValues',
314
                'each',
315
                'rule' => ['number', 'min' => 0, 'max' => self::NUMBER_LIMIT],
316
            ],
317
            [
318
                'recValues',
319 View Code Duplication
                function ($attribute, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
320
                    $count = count($this->$attribute);
321
                    if ($count > 30) {
322
                        $this->addError($attribute, 'maximum 30 recommended offers');
323
                    }
324
                }
325
            ],
326
            [
327
                ['group_id'],
328
                'integer',
329
                'min' => 0,
330
                'max' => 999999999,
331
            ],
332
        ];
333
    }
334
335
    /**
336
     * @param array $params
337
     */
338
    public function setParams(array $params)
339
    {
340
        $this->params = $params;
341
    }
342
343
    /**
344
     * @param array $pictures
345
     */
346
    public function setPictures(array $pictures)
347
    {
348
        $this->pictures = $pictures;
349
    }
350
351
    /**
352
     * @inheritdoc
353
     */
354
    protected function getYmlBody()
355
    {
356
        $string = '';
357
        foreach ($this->getYmlAttributes() as $attribute) {
358
            $string .= $this->getYmlAttribute($attribute);
359
        }
360
361
        foreach ($this->params as $name => $value) {
362
            $string .= $this->getYmlParamTag($name, $value);
363
        }
364
365
        foreach ($this->pictures as $picture) {
366
            $string .= $this->getYmlPictureTag($picture);
367
        }
368
369
        $this->appendOutletOptions($string);
370
371
        $this->appendDeliveryOptions($string);
372
373
        return $string;
374
    }
375
376
    /**
377
     * Добавляет теги ддля опций доставки
378
     *
379
     * @param $string
380
     *
381
     * @throws Exception
382
     */
383 View Code Duplication
    protected function appendDeliveryOptions(&$string) {
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...
384
        if(count($this->deliveryOptions) < 1) {
385
            return;
386
        }
387
        $string .= '<delivery-options>' . PHP_EOL;
388
        $deliveryOptionBase = new DeliveryOption();
389
390
        foreach($this->deliveryOptions as $deliveryOption) {
391
            $deliveryOptionBase->loadAndValidate($deliveryOption);
392
            $string .= $deliveryOptionBase->getYml();
393
        }
394
        $string .= '</delivery-options>' . PHP_EOL;
395
    }
396
397
    /**
398
     * Добавляет теги ддля опций доставки
399
     *
400
     * @param $string
401
     *
402
     * @throws Exception
403
     */
404 View Code Duplication
    protected function appendOutletOptions(&$string) {
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...
405
        if(count($this->outlets) < 1) {
406
            return;
407
        }
408
409
        $string .= '<outlet>' . PHP_EOL;
410
        $outletOptionBase = new OutletOption();
411
412
        foreach($this->outlets as $outletOption) {
413
            $outletOptionBase->loadAndValidate($outletOption);
414
            $string .= $outletOptionBase->getYml();
415
        }
416
        $string .= '</outlet>' . PHP_EOL;
417
    }
418
419
    /**
420
     * @param string $attribute
421
     * @param string $value
422
     * @return string
423
     */
424 View Code Duplication
    protected function getYmlParamTag($attribute, $value)
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...
425
    {
426
        if ($value === null) {
427
            return '';
428
        }
429
430
        $string = '<param name="' . $attribute . '">' . $value . '</param>' . PHP_EOL;
431
432
        return $string;
433
    }
434
435
    /**
436
     * @param string $value
437
     * @return string
438
     */
439 View Code Duplication
    protected function getYmlPictureTag($value)
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...
440
    {
441
        if ($value === null) {
442
            return '';
443
        }
444
445
        $string = '<picture>' . $value . '</picture>' . PHP_EOL;
446
447
        return $string;
448
    }
449
}