Completed
Push — master ( d6d1fa...9af36b )
by Kirill
45s
created

SimpleOffer::appendDeliveryOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
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 10
ccs 0
cts 0
cp 0
rs 9.4285
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
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 6
    /**
53
     * Опции доставки
54
     *
55 6
     * @var array
56 4
     */
57 4
    public $deliveryOptions = [];
58 4
59 4
    /**
60 4
     * @inheritdoc
61 4
     */
62 4
    public function getYmlAttributes()
63 4
    {
64 4
        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
            'vendorCode',
78
            'description',
79
            'sales_notes',
80
            'manufacturer_Warranty',
81
            'country_Of_Origin',
82 6
            'adult',
83
            'age',
84
            'barcode',
85
            'cpa',
86 4
        ];
87 4
    }
88 6
89
    /**
90 4
     * @inheritdoc
91 4
     */
92 4
    public function rules()
93 4
    {
94
        return [
95 4
            [
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
            ],
109 4
            [
110 4
                ['delivery', 'pickup'],
111 4
                'in',
112
                'range' => [
113 4
                    'true',
114 4
                    'false'
115 4
                ]
116
            ],
117 4
            [
118 4
                ['id', 'categoryId', 'bid', 'cbid'],
119 4
                'integer',
120
            ],
121
            [
122 4
                ['name', 'market_Category'],
123 4
                'string',
124 4
            ],
125
            [
126 4
                ['url'],
127 4
                'url',
128 4
            ],
129 4
            [
130 4
                ['price', 'oldprice'],
131
                'double',
132 4
            ],
133 4
            [
134
                [
135 4
                    'currencyId',
136 4
                ],
137 4
                'in',
138
                'range' => [
139 4
                    'RUR',
140 6
                    'RUB',
141 6
                    'UAH',
142
                    'BYR',
143
                    'KZT',
144 6
                    'USD',
145 4
                    'EUR'
146
                ],
147 4
            ],
148 4
            [
149 4
                ['description'],
150 4
                'string',
151
            ],
152 4
            [
153 4
                ['pictures'],
154 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...
155 4
                    if (count($this->pictures) > 10) {
156 4
                        $this->addError('pictures', 'maximum 10 pictures');
157
                    }
158
                }
159
            ],
160
            [
161
                ['params'],
162 6
                'each',
163
                'rule' => ['string']
164 6
            ],
165 6
            [
166
                ['pictures'],
167
                'each',
168
                'rule' => ['url']
169
            ],
170 6
        ];
171
    }
172 6
173 6
    /**
174
     * @param array $params
175
     */
176
    public function setParams(array $params)
177
    {
178 6
        $this->params = $params;
179
    }
180 6
181
    /**
182 6
     * @param array $pictures
183 6
     */
184 4
    public function setPictures(array $pictures)
185
    {
186 6
        $this->pictures = $pictures;
187
    }
188 4
189
    /**
190 6
     * @param array $options
191 6
     *
192 4
     * @throws Exception
193
     */
194 6
    public function setDeliveryOptions(array $options)
195
    {
196
        if(count($options) > 5) {
197
            throw new InvalidParamException('Maximum count of delivery options array is 5');
198
        }
199
        $this->deliveryOptions = $options;
200
    }
201
202
    /**
203
     * @inheritdoc
204
     */
205
    protected function getYmlBody()
206
    {
207
        $string = '';
208
209
        foreach ($this->getYmlAttributes() as $attribute) {
210
            $string .= $this->getYmlAttribute($attribute);
211
        }
212
213
        foreach ($this->params as $name => $value) {
214
            $string .= $this->getYmlParamTag($name, $value);
215
        }
216
217
        foreach ($this->pictures as $picture) {
218 6
            $string .= $this->getYmlPictureTag($picture);
219
        }
220 6
221
        $this->appendDeliveryOptions($string);
222
223
        return $string;
224 6
    }
225
226 6
    /**
227
     * Добавляет теги ддля опций доставки
228
     *
229
     * @param $string
230
     *
231
     * @throws Exception
232
     */
233
    protected function appendDeliveryOptions(&$string) {
234
        $string .= '<delivery-options>' . PHP_EOL;
235
        $deliveryOptionBase = new DeliveryOption();
236
237
        foreach($this->deliveryOptions as $deliveryOption) {
238
            $deliveryOptionBase->loadModel($deliveryOption);
239
            $string .= $deliveryOptionBase->getYml();
240
        }
241
        $string .= '</delivery-options>' . PHP_EOL;
242
    }
243
244
245
    /**
246
     * @param string $attribute
247
     * @param string $value
248
     * @return string
249
     */
250 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...
251
    {
252
        if ($value === null) {
253
            return '';
254
        }
255
256
        $string = '<param name="' . $attribute . '">' . $value . '</param>' . PHP_EOL;
257
258
        return $string;
259
    }
260
261
    /**
262
     * @param string $value
263
     * @return string
264
     */
265 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...
266
    {
267
        if ($value === null) {
268
            return '';
269
        }
270
271
        $string = '<picture>' . $value . '</picture>' . PHP_EOL;
272
273
        return $string;
274
    }
275
}
276