Completed
Push — master ( a94186...6eb44b )
by Kirill
05:15
created

SimpleOffer::setParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace pastuhov\ymlcatalog\models;
3
4
class SimpleOffer extends BaseModel
5
{
6
    /**
7
     * @inheritdoc
8
     */
9
    public static $tag = 'offer';
10
    /**
11
     * @inheritdoc
12
     */
13
    public static $tagProperties = [
14
        'id',
15
        'bid',
16
        'cbid',
17
        'available'
18
    ];
19
20
    public $id;
21
    public $bid;
22
    public $cbid;
23
    public $available;
24
25
    public $url;
26
    public $price;
27
    public $oldprice;
28
    public $currencyId;
29
    public $categoryId;
30
    public $market_Category;
31
    public $store;
32
    public $pickup;
33
    public $delivery;
34
    public $local_Delivery_Cost;
35
    public $name;
36
    public $vendor;
37
    public $vendorCode;
38
    public $description;
39
    public $sales_Notes;
40
    public $manufacturer_Warranty;
41
    public $country_Of_Origin;
42
    public $adult;
43
    public $age;
44
    public $barcode;
45
    public $cpa;
46
    public $params = [];
47
    public $pictures = [];
48
49
    /**
50
     * @inheritdoc
51
     */
52 6
    public function getYmlAttributes()
53
    {
54
        return [
55 6
            'url',
56 6
            'price',
57 6
            'oldprice',
58 6
            'currencyId',
59 6
            'categoryId',
60 6
            'market_Category',
61 6
            'store',
62 6
            'pickup',
63 6
            'delivery',
64 6
            'local_Delivery_Cost',
65 6
            'name',
66 6
            'vendor',
67 6
            'vendorCode',
68 6
            'description',
69 6
            'sales_Notes',
70 6
            'manufacturer_Warranty',
71 6
            'country_Of_Origin',
72 6
            'adult',
73 6
            'age',
74 6
            'barcode',
75 6
            'cpa',
76 6
        ];
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 6
    public function rules()
83
    {
84
        return [
85
            [
86 6
                ['id', 'price', 'currencyId', 'categoryId', 'name', 'available'],
87 6
                'required',
88 6
            ],
89
            [
90 6
                ['sales_Notes'],
91 6
                'string',
92 6
                'max' => 50,
93 6
            ],
94
            [
95 6
                ['name', 'vendor'],
96 6
                'string',
97 6
                'max' => 120,
98 6
            ],
99
            [
100 6
                ['delivery'],
101 6
                'string',
102 6
                'max' => 4,
103 6
            ],
104
            [
105 6
                ['id', 'categoryId', 'bid', 'cbid'],
106 6
                'integer',
107 6
            ],
108
            [
109 6
                ['url'],
110 6
                'url',
111 6
            ],
112
            [
113 6
                ['price', 'oldprice'],
114 6
                'double',
115 6
            ],
116
            [
117
                [
118 6
                    'currencyId',
119 6
                ],
120 6
                'in',
121
                'range' => [
122 6
                    'RUR',
123 6
                    'UAH',
124 6
                    'BYR',
125 6
                    'KZT',
126 6
                    'USD',
127
                    'EUR'
128 6
                ],
129 6
            ],
130
            [
131 6
                ['description'],
132 6
                'string',
133 6
                'max' => 175,
134 6
            ],
135
            [
136 6
                ['pictures'],
137 6
                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...
138 6
                    if (count($this->pictures) > 10) {
139
                        $this->addError('pictures', 'maximum 10 pictures');
140
                    }
141 6
                }
142 6
            ],
143
            [
144 6
                ['params'],
145 6
                'each',
146 6
                'rule' => ['string']
147 6
            ],
148
            [
149 6
                ['pictures'],
150 6
                'each',
151 6
                'rule' => ['url']
152 6
            ]
153 6
        ];
154
    }
155
156
    /**
157
     * @param array $params
158
     */
159 6
    public function setParams(array $params)
160
    {
161 6
        $this->params = $params;
162 6
    }
163
164
    /**
165
     * @param array $pictures
166
     */
167 6
    public function setPictures(array $pictures)
168
    {
169 6
        $this->pictures = $pictures;
170 6
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175 6
    protected function getYmlBody()
176
    {
177 6
        $string = '';
178
179 6
        foreach ($this->getYmlAttributes() as $attribute) {
180 6
            $string .= $this->getYmlAttribute($attribute);
181 6
        }
182
183 6
        foreach ($this->params as $name => $value) {
184
            $string .= $this->getYmlParamTag($name, $value);
185 6
        }
186
187 6
        foreach ($this->pictures as $picture) {
188 6
            $string .= $this->getYmlPictureTag($picture);
189 6
        }
190
191 6
        return $string;
192
    }
193
194
195
    /**
196
     * @param string $attribute
197
     * @param string $value
198
     * @return string
199
     */
200 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...
201
    {
202
        if ($value === null) {
203
            return '';
204
        }
205
206
        $string = '<param name="' . $attribute . '">' . $value . '</param>' . PHP_EOL;
207
208
        return $string;
209
    }
210
211
    /**
212
     * @param string $value
213
     * @return string
214
     */
215 6 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...
216
    {
217 6
        if ($value === null) {
218
            return '';
219
        }
220
221 6
        $string = '<picture>' . $value . '</picture>' . PHP_EOL;
222
223 6
        return $string;
224
    }
225
}
226