Passed
Push — master ( 8357f2...2b1b38 )
by payever
02:23
created

ProductRequestEntity::getSku()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * PHP version 5.4 and 7
4
 *
5
 * @package   Payever\Products
6
 * @author    Hennadii.Shymanskyi <[email protected]>
7
 * @copyright 2017-2019 payever GmbH
8
 * @license   MIT <https://opensource.org/licenses/MIT>
9
 */
10
11
namespace Payever\ExternalIntegration\Products\Http\RequestEntity;
12
13
use Payever\ExternalIntegration\Core\Http\RequestEntity;
14
use Payever\ExternalIntegration\Products\Http\MessageEntity\ProductCategoryEntity;
15
use Payever\ExternalIntegration\Products\Http\MessageEntity\ProductShippingEntity;
16
use Payever\ExternalIntegration\Products\Http\MessageEntity\ProductVariantOptionEntity;
17
18
/**
19
 * PHP version 5.4 and 7
20
 *
21
 * @package   Payever\Products
22
 * @author    Hennadii.Shymanskyi <[email protected]>
23
 * @copyright 2017-2019 payever GmbH
24
 * @license   MIT <https://opensource.org/licenses/MIT>
25
 *
26
 * @method string getExternalId()
27
 * @method string[] getImages()
28
 * @method string[] getImagesUrl()
29
 * @method bool getActive()
30
 * @method string getUuid()
31
 * @method string getBusinessUuid()
32
 * @method ProductCategoryEntity[]|string[] getCategories()
33
 * @method string getCurrency()
34
 * @method string getTitle()
35
 * @method string getDescription()
36
 * @method float getPrice()
37
 * @method float|null getSalePrice()
38
 * @method bool getOnSales()
39
 * @method string getBarcode()
40
 * @method float getVatRate()
41
 * @method ProductVariantOptionEntity[]|array getOptions()
42
 * @method string getType()
43
 * @method self[]|array getVariants()
44
 * @method ProductShippingEntity|null getShipping()
45
 * @method \DateTime|false getCreatedAt()
46
 * @method \DateTime|false getUpdatedAt()
47
 * @method string|null getParent()
48
 * @method string|null getProduct()
49
 * @method self setExternalId(string $externalId)
50
 * @method self setImages(array $images)
51
 * @method self setImagesUrl(array $imagesUrl)
52
 * @method self setActive(bool $active)
53
 * @method self setUuid(string $uuid)
54
 * @method self setBusinessUuid(string $businessUuid)
55
 * @method self setCurrency(string $currency)
56
 * @method self setTitle(string $title)
57
 * @method self setDescription(string $description)
58
 * @method self setPrice(float $price)
59
 * @method self setOnSales(bool $onSales)
60
 * @method self setSku(string $sku)
61
 * @method self setBarcode(string $barcode)
62
 * @method self setVatRate(float $vatRate)
63
 * @method self setType(string $type)
64
 */
65
class ProductRequestEntity extends RequestEntity
66
{
67
    const UNDERSCORE_ON_SERIALIZATION = false;
68
69
    /**
70
     * Subscription external id.
71
     * Required for all requests.
72
     *
73
     * @var string
74
     */
75
    protected $externalId;
76
77
    /** @var array */
78
    protected $images = array();
79
80
    /** @var array */
81
    protected $imagesUrl = array();
82
83
    /** @var bool */
84
    protected $active = true;
85
86
    /** @var string */
87
    protected $uuid;
88
89
    /** @var string */
90
    protected $businessUuid;
91
92
    /** @var ProductCategoryEntity[]|string[] */
93
    protected $categories = array();
94
95
    /** @var string */
96
    protected $currency;
97
98
    /** @var string */
99
    protected $title;
100
101
    /** @var string */
102
    protected $description;
103
104
    /** @var float */
105
    protected $price;
106
107
    /** @var float|null */
108
    protected $salePrice;
109
110
    /** @var bool */
111
    protected $onSales = false;
112
113
    /** @var string */
114
    protected $sku;
115
116
    /** @var string */
117
    protected $barcode;
118
119
    /** @var string */
120
    protected $type;
121
122
    /** @var float */
123
    protected $vatRate;
124
125
    /** @var self[]|array */
126
    protected $variants = array();
127
128
    /**
129
     * Parent product id for variants.
130
     * Present only in request with a single variant data inside.
131
     *
132
     * @var string|null
133
     */
134
    protected $parent;
135
136
    /**
137
     * Parent product id for variants.
138
     * Present only in request with parent product with variants.
139
     *
140
     * @var string|null
141
     */
142
    protected $product;
143
144
    /** @var ProductShippingEntity|null */
145
    protected $shipping;
146
147
    /** @var \DateTime|bool */
148
    protected $createdAt;
149
150
    /** @var \DateTime|bool */
151
    protected $updatedAt;
152
153
    /**
154
     * Available only for product variants
155
     *
156
     * @var ProductVariantOptionEntity[]|array
157
     */
158
    protected $options = array();
159
160
    /**
161
     * @param string $updatedAt
162
     *
163
     * @return static
164
     */
165
    public function setUpdatedAt($updatedAt)
166
    {
167
        $this->updatedAt = date_create($updatedAt);
168
169
        return $this;
170
    }
171
172
    /**
173
     * @param string $createdAt
174
     *
175
     * @return static
176
     */
177
    public function setCreatedAt($createdAt)
178
    {
179
        $this->createdAt = date_create($createdAt);
180
181
        return $this;
182
    }
183
184
    /**
185
     * @param array $data
186
     *
187
     * @return static
188
     */
189
    public function setShipping($data)
190
    {
191
        $this->shipping = new ProductShippingEntity($data);
192
193
        return $this;
194
    }
195
196
    /**
197
     * @param array $data
198
     *
199
     * @return static
200
     */
201
    public function setCategories($data)
202
    {
203
        foreach ($data as $plainCategory) {
204
            /** Both ProductCategoryEntity fields array and simple title are possible */
205
            $this->categories[] = is_array($plainCategory)
206
                ? new ProductCategoryEntity($plainCategory)
207
                : $plainCategory
208
            ;
209
        }
210
211
        return $this;
212
    }
213
214
    /**
215
     * @param array[]|static[] $data
216
     *
217
     * @return static
218
     */
219
    public function setVariants($data)
220
    {
221
        foreach ($data as $variant) {
222
            $this->addVariant($variant);
223
        }
224
225
        return $this;
226
    }
227
228
    /**
229
     * @param array|static $variant
230
     *
231
     * @return self
232
     */
233
    public function addVariant($variant)
234
    {
235
        if (is_array($variant)) {
236
            $variant = new static($variant);
237
            $variant->setCurrency($this->getCurrency());
238
        }
239
240
        $this->variants[] = $variant;
241
242
        return $this;
243
    }
244
245
    /**
246
     * Set product variant option
247
     *
248
     * @param array $options
249
     *
250
     * @return self
251
     */
252
    public function setOptions($options)
253
    {
254
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
255
            foreach ($options as $option) {
256
                $this->options[] = new ProductVariantOptionEntity($option);
257
            }
258
        }
259
260
        return $this;
261
    }
262
263
    /**
264
     * Add product variant option
265
     *
266
     * @param string $name
267
     * @param string $value
268
     *
269
     * @return self
270
     */
271
    public function addOption($name, $value)
272
    {
273
        $this->options[] = new ProductVariantOptionEntity(compact('name', 'value'));
274
275
        return $this;
276
    }
277
278
    /**
279
     * @return array
280
     */
281
    public function getImagesUuid()
282
    {
283
        return array_map(
284
            function ($imageName) {
285
                return substr($imageName, 0, strpos($imageName, '.'));
286
            },
287
            $this->getImages()
288
        );
289
    }
290
291
    /**
292
     * @param float $salePrice
293
     *
294
     * @return self
295
     */
296
    public function setSalePrice($salePrice)
297
    {
298
        $this->salePrice = $salePrice;
299
        $this->onSales = $salePrice > 0 && (!$this->getPrice() || $this->getPrice() > $salePrice);
300
301
        return $this;
302
    }
303
304
    /**
305
     * Whether this product entity represents product variant and should be linked to a product
306
     *
307
     * @return bool
308
     */
309
    public function isVariant()
310
    {
311
        return $this->getParent() || $this->getProduct() || count($this->getOptions());
312
    }
313
314
    /**
315
     * @return string
316
     */
317
    public function getSku()
318
    {
319
        return $this->sku ? $this->sku : $this->uuid;
320
    }
321
}
322