Passed
Push — master ( fb2db6...4a8108 )
by payever
02:07
created

ProductRequestEntity::setSalePrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
17
/**
18
 * PHP version 5.4 and 7
19
 *
20
 * @package   Payever\Products
21
 * @author    Hennadii.Shymanskyi <[email protected]>
22
 * @copyright 2017-2019 payever GmbH
23
 * @license   MIT <https://opensource.org/licenses/MIT>
24
 *
25
 * @method string getExternalId()
26
 * @method string[] getImages()
27
 * @method string[] getImagesUrl()
28
 * @method bool getEnabled()
29
 * @method bool getInventoryTrackingEnabled()
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 string getSku()
39
 * @method int getInventory()
40
 * @method string getBarcode()
41
 * @method string getType()
42
 * @method self[]|null getVariants()
43
 * @method ProductShippingEntity|null getShipping()
44
 * @method \DateTime|false getCreatedAt()
45
 * @method \DateTime|false getUpdatedAt()
46
 * @method self setExternalId(string $externalId)
47
 * @method self setImages(array $images)
48
 * @method self setImagesUrl(array $imagesUrl)
49
 * @method self setEnabled(bool $enabled)
50
 * @method self setInventoryTrackingEnabled(bool $enabled)
51
 * @method self setUuid(string $uuid)
52
 * @method self setBusinessUuid(string $businessUuid)
53
 * @method self setCurrency(string $currency)
54
 * @method self setTitle(string $title)
55
 * @method self setDescription(string $description)
56
 * @method self setPrice(float $price)
57
 * @method self setSku(string $sku)
58
 * @method self setInventory(int $inventory)
59
 * @method self setBarcode(string $barcode)
60
 * @method self setType(string $type)
61
 */
62
class ProductRequestEntity extends RequestEntity
63
{
64
    const UNDERSCORE_ON_SERIALIZATION = false;
65
66
    /**
67
     * Subscription external id.
68
     * Required for all requests.
69
     *
70
     * @var string
71
     */
72
    protected $externalId;
73
74
    /** @var array */
75
    protected $images = array();
76
77
    /** @var array */
78
    protected $imagesUrl = array();
79
80
    /** @var bool */
81
    protected $enabled = true;
82
83
    /** @var bool */
84
    protected $inventoryTrackingEnabled = false;
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
    /**
111
     * Read as 'salePriceHidden'
112
     *
113
     * @var bool
114
     */
115
    protected $hidden = true;
116
117
    /** @var float */
118
    protected $sortPrice;
119
120
    /** @var string */
121
    protected $sku;
122
123
    /** @var int */
124
    protected $inventory;
125
126
    /** @var string */
127
    protected $barcode;
128
129
    /** @var string */
130
    protected $type;
131
132
    /** @var self[]|null */
133
    protected $variants;
134
135
    /** @var ProductShippingEntity|null */
136
    protected $shipping;
137
138
    /** @var \DateTime|bool */
139
    protected $createdAt;
140
141
    /** @var \DateTime|bool */
142
    protected $updatedAt;
143
144
    /**
145
     * @param string $updatedAt
146
     *
147
     * @return static
148
     */
149
    public function setUpdatedAt($updatedAt)
150
    {
151
        $this->updatedAt = date_create($updatedAt);
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param string $createdAt
158
     *
159
     * @return static
160
     */
161
    public function setCreatedAt($createdAt)
162
    {
163
        $this->createdAt = date_create($createdAt);
164
165
        return $this;
166
    }
167
168
    /**
169
     * @param array $data
170
     *
171
     * @return static
172
     */
173
    public function setShipping($data)
174
    {
175
        $this->shipping = new ProductShippingEntity($data);
176
177
        return $this;
178
    }
179
180
    /**
181
     * @param array $data
182
     *
183
     * @return static
184
     */
185
    public function setCategories($data)
186
    {
187
        foreach ($data as $plainCategory) {
188
            /** Both ProductCategoryEntity fields array and simple title are possible */
189
            $this->categories[] = is_array($plainCategory)
190
                ? new ProductCategoryEntity($plainCategory)
191
                : $plainCategory
192
            ;
193
        }
194
195
        return $this;
196
    }
197
198
    /**
199
     * @param array[array]|static[] $data
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[array]|static[] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
200
     *
201
     * @return static
202
     */
203
    public function setVariants($data)
204
    {
205
        foreach ($data as $variant) {
206
            $this->addVariant($variant);
207
        }
208
209
        return $this;
210
    }
211
212
    /**
213
     * @param array|static $variant
214
     */
215
    public function addVariant($variant)
216
    {
217
        if (is_array($variant)) {
218
            $variant = new static($variant);
219
            $variant->setCurrency($this->getCurrency());
220
        }
221
222
        $this->variants[] = $variant;
223
    }
224
225
    /**
226
     * @return array
227
     */
228
    public function getImagesUuid()
229
    {
230
        return array_map(
231
            function ($imageName) {
232
                return substr($imageName, 0, strpos($imageName, '.'));
233
            },
234
            $this->getImages()
235
        );
236
    }
237
238
    /**
239
     * @param float $price
240
     *
241
     * @return self
242
     */
243
    public function setSalePrice($price)
244
    {
245
        $this->salePrice = $price;
246
        $this->hidden = !$price;
247
248
        return $this;
249
    }
250
}
251