Completed
Pull Request — master (#6)
by
unknown
04:50
created

SimpleOffer::setPictures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    public function getYmlAttributes()
53
    {
54
        return [
55
            'url',
56
            'price',
57
            'oldprice',
58
            'currencyId',
59
            'categoryId',
60
            'market_Category',
61
            'store',
62
            'pickup',
63
            'delivery',
64
            'local_Delivery_Cost',
65
            'name',
66
            'vendor',
67
            'vendorCode',
68
            'description',
69
            'sales_Notes',
70
            'manufacturer_Warranty',
71
            'country_Of_Origin',
72
            'adult',
73
            'age',
74
            'barcode',
75
            'cpa',
76
        ];
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function rules()
83
    {
84
        return [
85
            [
86
                ['id', 'price', 'currencyId', 'categoryId', 'name', 'available'],
87
                'required',
88
            ],
89
            [
90
                ['sales_Notes'],
91
                'string',
92
                'max' => 50,
93
            ],
94
            [
95
                ['name', 'vendor'],
96
                'string',
97
                'max' => 120,
98
            ],
99
            [
100
                ['delivery'],
101
                'string',
102
                'max' => 4,
103
            ],
104
            [
105
                ['id', 'categoryId', 'bid', 'cbid'],
106
                'integer',
107
            ],
108
            [
109
                ['url'],
110
                'url',
111
            ],
112
            [
113
                ['price', 'oldprice'],
114
                'double',
115
            ],
116
            [
117
                [
118
                    'currencyId',
119
                ],
120
                'in',
121
                'range' => [
122
                    'RUR',
123
                    'UAH',
124
                    'BYR',
125
                    'KZT',
126
                    'USD',
127
                    'EUR'
128
                ],
129
            ],
130
            [
131
                ['description'],
132
                'string',
133
                'max' => 175,
134
            ],
135
            [
136
                ['pictures'],
137
                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
                    if (count($this->pictures) > 10) {
139
                        $this->addError('pictures', 'maximum 10 pictures');
140
                    }
141
                }
142
            ],
143
            [
144
                ['params'],
145
                'each',
146
                'rule' => ['string']
147
            ],
148
            [
149
                ['pictures'],
150
                'each',
151
                'rule' => ['url']
152
            ]
153
        ];
154
    }
155
156
    /**
157
     * @param array $params
158
     */
159
    public function setParams(array $params)
160
    {
161
        $this->params = $params;
162
    }
163
164
    /**
165
     * @param array $pictures
166
     */
167
    public function setPictures(array $pictures)
168
    {
169
        $this->pictures = $pictures;
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175
    protected function getYmlBody()
176
    {
177
        $string = '';
178
179
        foreach ($this->getYmlAttributes() as $attribute) {
180
            $string .= $this->getYmlAttribute($attribute);
181
        }
182
183
        foreach ($this->params as $name => $value) {
184
            $string .= $this->getYmlParamTag($name, $value);
185
        }
186
187
        foreach ($this->pictures as $picture) {
188
            $string .= $this->getYmlPictureTag($picture);
189
        }
190
191
        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 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
        if ($value === null) {
218
            return '';
219
        }
220
221
        $string = '<picture>' . $value . '</picture>' . PHP_EOL;
222
223
        return $string;
224
    }
225
}
226