Completed
Pull Request — master (#23)
by
unknown
03:14
created

SimpleOffer::appendDeliveryOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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