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

DeliveryOption::rules()   C

Complexity

Conditions 9
Paths 1

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 59
rs 6.9133
cc 9
eloc 36
nc 1
nop 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
namespace pastuhov\ymlcatalog\models;
3
use pastuhov\ymlcatalog\DeliveryOptionInterface;
4
5
/**
6
 * Class DeliveryOption
7
 *
8
 * Модель опции условий доставки - тега option в секции delivery-option
9
 * @package pastuhov\ymlcatalog\models
10
 */
11
class DeliveryOption extends BaseModel implements DeliveryOptionInterface
12
{
13
    /**
14
     * @inheritdoc
15
     */
16
    public static $tag = 'option';
17
18
    /** @var int Стоимость доставки в рублях. */
19
    public $cost;
20
21
    /** @var string Срок доставки в рабочих днях. */
22
    public $days;
23
24
    /**
25
     * Укажите местное время (в часовом поясе магазина)
26
     *
27
     * @var int
28
     */
29
    public $orderBefore;
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public static function getTagProperties()
35
    {
36
        return [
37
            'cost',
38
            'days',
39
        ];
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function rules()
46
    {
47
        return [
48
            [
49
                ['cost', 'days'],
50
                'required',
51
            ],
52
            [
53
                ['cost'],
54
                'number',
55
                'integerOnly' => true,
56
                'min' => 0,
57
                'max' => SimpleOffer::NUMBER_LIMIT,
58
            ],
59
            [
60
                ['days'],
61
                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...
62
                    $value = $this->$attribute;
63
                    $isBigValue = false;
64
                    if (!preg_match('/^[\d]+(-[\d]+)?$/', $value)) {
65
                        $this->addError($attribute, 'Wrong ' . $attribute . ' value');
66
                        return;
67
                    }
68
69
                    if (strpos($value, '-') !== false) {
70
                        $parts = explode('-', $value);
71
                        if (count($parts) != 2) {
72
                            $this->addError($attribute, 'Wrong parts count in ' . $attribute);
73
                            return;
74
                        }
75
76
                        if ((int) $parts[0] >= $parts[1]) {
77
                            $this->addError($attribute, 'Wrong ' . $attribute . ' value');
78
                            return;
79
                        }
80
81
                        if ($parts[0] > 31 || $parts[1] > 31) {
82
                            $isBigValue = true;
83
                        }
84
                    } else {
85
                        if ($value > 31) {
86
                            $isBigValue = true;
87
                        }
88
                    }
89
90
                    if ($isBigValue) {
91
                        $this->addError($attribute, 'Value ' . $attribute . ' is greater than 31');
92
                    }
93
                },
94
            ],
95
            [
96
                ['orderBefore'],
97
                'number',
98
                'integerOnly' => true,
99
                'min' => 0,
100
                'max' => 24,
101
            ],
102
        ];
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    protected function getYmlBody()
109
    {
110
        return null;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    protected function getYmlStartTag()
117
    {
118
        return str_replace('>', ' />', parent::getYmlStartTag());
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    protected function getYmlEndTag()
125
    {
126
        return '';
127
    }
128
129
    public function getCost() {
130
        return $this->cost;
131
    }
132
133
    /**
134
     * Срок доставки в рабочих днях.
135
     *
136
     * Можно указать как конкретное количество дней, так и период «от — до». Например, срок доставки от 2 до 4 дней описывается следующим образом: days="2-4".
137
     * @return string
138
     */
139
    public function getDays() {
140
        return $this->days;
141
    }
142
143
    /**
144
     * Срок доставки в рабочих днях.
145
     *
146
     * Можно указать как конкретное количество дней, так и период «от — до». Например, срок доставки от 2 до 4 дней описывается следующим образом: days="2-4".
147
     * @return string
148
     */
149
    public function getOrderBefore() {
150
        return $this->days;
151
    }
152
}
153