Passed
Push — master ( 289e82...63eff2 )
by payever
04:07
created

ProductRequestEntity   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 28
eloc 70
c 4
b 0
f 0
dl 0
loc 285
rs 10

14 Methods

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