Completed
Push — master ( 871cd8...ab1e03 )
by Kirill
03:44
created

SimpleOffer::rules()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 83
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 60
CRAP Score 2.0851

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 83
ccs 60
cts 83
cp 0.7229
rs 8.7468
cc 2
eloc 47
nc 1
nop 0
crap 2.0851

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 pastuhov\ymlcatalog\models;
4
5
use yii\base\Exception;
6
use yii\base\InvalidParamException;
7
8
class SimpleOffer extends BaseModel
9
{
10
    /**
11
     * @inheritdoc
12
     */
13
    public static $tag = 'offer';
14
    /**
15
     * @inheritdoc
16
     */
17
    public static $tagProperties = [
18
        'id',
19
        'bid',
20
        'cbid',
21
        'available'
22
    ];
23
24
    public $id;
25
    public $bid;
26
    public $cbid;
27
    public $available;
28
29
    public $url;
30
    public $price;
31
    public $oldprice;
32
    public $currencyId;
33
    public $categoryId;
34
    public $market_Category;
35
    public $store;
36
    public $pickup;
37
    public $delivery;
38
    public $local_Delivery_Cost;
39
    public $name;
40
    public $vendor;
41
    public $vendorCode;
42
    public $description;
43
    public $sales_notes;
44
    public $manufacturer_Warranty;
45
    public $country_Of_Origin;
46
    public $adult;
47
    public $age;
48
    public $barcode;
49
    public $cpa;
50
    public $params = [];
51
    public $pictures = [];
52
    /**
53
     * Опции доставки
54
     *
55
     * @var array
56
     */
57
    public $deliveryOptions = [];
58
59
    /**
60
     * @inheritdoc
61
     */
62 4
    public function getYmlAttributes()
63
    {
64
        return [
65 4
            'url',
66 4
            'price',
67 4
            'oldprice',
68 4
            'currencyId',
69 4
            'categoryId',
70 4
            'market_Category',
71 4
            'store',
72 4
            'pickup',
73 4
            'delivery',
74 4
            'local_Delivery_Cost',
75 4
            'name',
76 4
            'vendor',
77 4
            'vendorCode',
78 4
            'description',
79 4
            'sales_notes',
80 4
            'manufacturer_Warranty',
81 4
            'country_Of_Origin',
82 4
            'adult',
83 4
            'age',
84 4
            'barcode',
85 4
            'cpa',
86 4
        ];
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 4
    public function rules()
93
    {
94
        return [
95
            [
96 4
                ['id', 'price', 'currencyId', 'categoryId', 'name', 'available'],
97 4
                'required',
98 4
            ],
99
            [
100 4
                ['sales_notes'],
101 4
                'string',
102 4
                'max' => 50,
103 4
            ],
104
            [
105 4
                ['name', 'vendor'],
106 4
                'string',
107 4
                'max' => 120,
108 4
            ],
109
            [
110 4
                ['delivery', 'pickup', 'store', 'manufacturer_Warranty', 'adult'],
111 4
                'boolean',
112 4
                'trueValue' => 'true',
113 4
                'falseValue' => 'false',
114 4
                'strict' => true,
115 4
            ],
116
            [
117 4
                ['cpa'],
118
                'boolean'
119 4
            ],
120
            [
121 4
                ['id', 'categoryId', 'bid', 'cbid', 'age'],
122 4
                'integer',
123 4
            ],
124
            [
125 4
                ['name', 'market_Category', 'vendorCode', 'country_Of_Origin', 'barcode'],
126 4
                'string',
127 4
            ],
128
            [
129 4
                ['url'],
130 4
                'url',
131 4
            ],
132
            [
133 4
                ['price', 'oldprice', 'local_Delivery_Cost'],
134 4
                'double',
135 4
            ],
136
            [
137
                [
138 4
                    'currencyId',
139 4
                ],
140 4
                'in',
141
                'range' => [
142 4
                    'RUR',
143 4
                    'RUB',
144 4
                    'UAH',
145 4
                    'BYR',
146 4
                    'KZT',
147 4
                    'USD',
148
                    'EUR'
149 4
                ],
150 4
            ],
151
            [
152 4
                ['description'],
153 4
                'string',
154 4
            ],
155
            [
156 4
                ['pictures'],
157 4
                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...
158 4
                    if (count($this->pictures) > 10) {
159
                        $this->addError('pictures', 'maximum 10 pictures');
160
                    }
161 4
                }
162 4
            ],
163
            [
164 4
                ['params'],
165 4
                'each',
166 4
                'rule' => ['string']
167 4
            ],
168
            [
169 4
                ['pictures'],
170 4
                'each',
171 4
                'rule' => ['url']
172 4
            ],
173 4
        ];
174
    }
175
176
    /**
177
     * @param array $params
178
     */
179 5
    public function setParams(array $params)
180
    {
181 5
        $this->params = $params;
182 5
    }
183
184
    /**
185
     * @param array $pictures
186
     */
187 5
    public function setPictures(array $pictures)
188
    {
189 5
        $this->pictures = $pictures;
190 5
    }
191
192
    /**
193
     * @param array $options
194
     *
195
     * @throws Exception
196
     */
197 5
    public function setDeliveryOptions(array $options)
198
    {
199 5
        if(count($options) > 5) {
200
            throw new InvalidParamException('Maximum count of delivery options array is 5');
201
        }
202 5
        $this->deliveryOptions = $options;
203 5
    }
204
205
    /**
206
     * @inheritdoc
207
     */
208 4
    protected function getYmlBody()
209
    {
210 4
        $string = '';
211
212 4
        foreach ($this->getYmlAttributes() as $attribute) {
213 4
            $string .= $this->getYmlAttribute($attribute);
214 4
        }
215
216 4
        foreach ($this->params as $name => $value) {
217
            $string .= $this->getYmlParamTag($name, $value);
218 4
        }
219
220 4
        foreach ($this->pictures as $picture) {
221 4
            $string .= $this->getYmlPictureTag($picture);
222 4
        }
223
224 4
        $this->appendDeliveryOptions($string);
225
226 4
        return $string;
227
    }
228
229
    /**
230
     * Добавляет теги ддля опций доставки
231
     *
232
     * @param $string
233
     *
234
     * @throws Exception
235
     */
236 4
    protected function appendDeliveryOptions(&$string) {
237 4
        if(count($this->deliveryOptions) < 1) {
238 4
            return;
239
        }
240 4
        $string .= '<delivery-options>' . PHP_EOL;
241 4
        $deliveryOptionBase = new DeliveryOption();
242
243 4
        foreach($this->deliveryOptions as $deliveryOption) {
244 4
            $deliveryOptionBase->loadModel($deliveryOption);
245 4
            $string .= $deliveryOptionBase->getYml();
246 4
        }
247 4
        $string .= '</delivery-options>' . PHP_EOL;
248 4
    }
249
250
251
    /**
252
     * @param string $attribute
253
     * @param string $value
254
     * @return string
255
     */
256 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...
257
    {
258
        if ($value === null) {
259
            return '';
260
        }
261
262
        $string = '<param name="' . $attribute . '">' . $value . '</param>' . PHP_EOL;
263
264
        return $string;
265
    }
266
267
    /**
268
     * @param string $value
269
     * @return string
270
     */
271 4 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...
272
    {
273 4
        if ($value === null) {
274
            return '';
275
        }
276
277 4
        $string = '<picture>' . $value . '</picture>' . PHP_EOL;
278
279 4
        return $string;
280
    }
281
}
282