Completed
Push — master ( 094334...0b6e60 )
by Kamil
17:47
created

ProductVariant::hasImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Core\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Sylius\Component\Core\Pricing\Calculators;
17
use Sylius\Component\Product\Model\Variant as BaseVariant;
18
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
19
use Sylius\Component\Variation\Model\VariantInterface as BaseVariantInterface;
20
21
/**
22
 * @author Paweł Jędrzejewski <[email protected]>
23
 */
24
class ProductVariant extends BaseVariant implements ProductVariantInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $sku;
30
31
    /**
32
     * @var int
33
     */
34
    protected $price;
35
36
    /**
37
     * @var int
38
     */
39
    protected $originalPrice;
40
41
    /**
42
     * @var string
43
     */
44
    protected $pricingCalculator = Calculators::STANDARD;
45
46
    /**
47
     * @var array
48
     */
49
    protected $pricingConfiguration = [];
50
51
    /**
52
     * @var int
53
     */
54
    protected $onHold = 0;
55
56
    /**
57
     * @var int
58
     */
59
    protected $onHand = 0;
60
61
    /**
62
     * @var int
63
     */
64
    protected $sold = 0;
65
66
    /**
67
     * @var bool
68
     */
69
    protected $availableOnDemand = true;
70
71
    /**
72
     * @var Collection|ProductVariantImageInterface[]
73
     */
74
    protected $images;
75
76
    /**
77
     * @var float
78
     */
79
    protected $weight;
80
81
    /**
82
     * @var float
83
     */
84
    protected $width;
85
86
    /**
87
     * @var float
88
     */
89
    protected $height;
90
91
    /**
92
     * @var float
93
     */
94
    protected $depth;
95
96
    /**
97
     * @var TaxCategoryInterface
98
     */
99
    protected $taxCategory;
100
101
    /**
102
     * Override constructor to set on hand stock.
103
     */
104
    public function __construct()
105
    {
106
        parent::__construct();
107
108
        $this->images = new ArrayCollection();
109
    }
110
111
    public function __toString()
112
    {
113
        $string = $this->getProduct()->getName();
114
115
        if (!$this->getOptions()->isEmpty()) {
116
            $string .= '(';
117
118
            foreach ($this->getOptions() as $option) {
119
                $string .= $option->getOption()->getName().': '.$option->getValue().', ';
120
            }
121
122
            $string = substr($string, 0, -2).')';
123
        }
124
125
        return $string;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getMetadataClassIdentifier()
132
    {
133
        return self::METADATA_CLASS_IDENTIFIER;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function getMetadataIdentifier()
140
    {
141
        return $this->getMetadataClassIdentifier().'-'.$this->getId();
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function getSku()
148
    {
149
        return $this->sku;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function setSku($sku)
156
    {
157
        $this->sku = $sku;
158
159
        return $this;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getPrice()
166
    {
167
        return $this->price;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function setPrice($price)
174
    {
175
        if (!is_int($price)) {
176
            throw new \InvalidArgumentException('Price must be an integer.');
177
        }
178
        $this->price = $price;
179
180
        return $this;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function setOriginalPrice($originalPrice)
187
    {
188
        if (!is_int($originalPrice)) {
189
            throw new \InvalidArgumentException('Original price must be an integer.');
190
        }
191
        $this->originalPrice = $originalPrice;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function getOriginalPrice()
198
    {
199
        return $this->originalPrice;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function getPricingCalculator()
206
    {
207
        return $this->pricingCalculator;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function setPricingCalculator($calculator)
214
    {
215
        $this->pricingCalculator = $calculator;
216
217
        return $this;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function getPricingConfiguration()
224
    {
225
        return $this->pricingConfiguration;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function setPricingConfiguration(array $configuration)
232
    {
233
        $this->pricingConfiguration = $configuration;
234
235
        return $this;
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241
    public function isInStock()
242
    {
243
        return 0 < $this->onHand;
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249
    public function getOnHold()
250
    {
251
        return $this->onHold;
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function setOnHold($onHold)
258
    {
259
        $this->onHold = $onHold;
260
261
        return $this;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function getOnHand()
268
    {
269
        return $this->onHand;
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function setOnHand($onHand)
276
    {
277
        $this->onHand = $onHand;
278
279
        if (0 > $this->onHand) {
280
            $this->onHand = 0;
281
        }
282
283
        return $this;
284
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289
    public function getSold()
290
    {
291
        return $this->sold;
292
    }
293
294
    /**
295
     * {@inheritdoc}
296
     */
297
    public function setSold($sold)
298
    {
299
        $this->sold = (int) $sold;
300
    }
301
302
    /**
303
     * {@inheritdoc}
304
     */
305
    public function getInventoryName()
306
    {
307
        return $this->getProduct()->getName();
308
    }
309
310
    /**
311
     * {@inheritdoc}
312
     */
313
    public function isAvailableOnDemand()
314
    {
315
        return $this->availableOnDemand;
316
    }
317
318
    /**
319
     * {@inheritdoc}
320
     */
321
    public function setAvailableOnDemand($availableOnDemand)
322
    {
323
        $this->availableOnDemand = (bool) $availableOnDemand;
324
325
        return $this;
326
    }
327
328
    /**
329
     * {@inheritdoc}
330
     */
331
    public function setDefaults(BaseVariantInterface $masterVariant)
332
    {
333
        parent::setDefaults($masterVariant);
334
335
        $this->setPrice($masterVariant->getPrice());
336
337
        return $this;
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    public function getShippingCategory()
344
    {
345
        return $this->getProduct()->getShippingCategory();
346
    }
347
348
    /**
349
     * {@inheritdoc}
350
     */
351
    public function hasImage(ProductVariantImageInterface $image)
352
    {
353
        return $this->images->contains($image);
354
    }
355
356
    /**
357
     * {@inheritdoc}
358
     */
359
    public function getImages()
360
    {
361
        return $this->images;
362
    }
363
364
    /**
365
     * {@inheritdoc}
366
     */
367
    public function getImage()
368
    {
369
        if ($this->images->isEmpty()) {
370
            return $this->getProduct()->getImage();
371
        }
372
373
        return $this->images->first();
374
    }
375
376
    /**
377
     * {@inheritdoc}
378
     */
379
    public function addImage(ProductVariantImageInterface $image)
380
    {
381
        if (!$this->hasImage($image)) {
382
            $image->setVariant($this);
383
            $this->images->add($image);
384
        }
385
386
        return $this;
387
    }
388
389
    /**
390
     * {@inheritdoc}
391
     */
392
    public function removeImage(ProductVariantImageInterface $image)
393
    {
394
        $image->setVariant(null);
395
        $this->images->removeElement($image);
396
397
        return $this;
398
    }
399
400
    /**
401
     * {@inheritdoc}
402
     */
403
    public function getWeight()
404
    {
405
        return $this->weight;
406
    }
407
408
    /**
409
     * {@inheritdoc}
410
     */
411
    public function setWeight($weight)
412
    {
413
        $this->weight = $weight;
414
415
        return $this;
416
    }
417
418
    /**
419
     * {@inheritdoc}
420
     */
421
    public function getWidth()
422
    {
423
        return $this->width;
424
    }
425
426
    /**
427
     * {@inheritdoc}
428
     */
429
    public function setWidth($width)
430
    {
431
        $this->width = $width;
432
433
        return $this;
434
    }
435
436
    /**
437
     * {@inheritdoc}
438
     */
439
    public function getHeight()
440
    {
441
        return $this->height;
442
    }
443
444
    /**
445
     * {@inheritdoc}
446
     */
447
    public function setHeight($height)
448
    {
449
        $this->height = $height;
450
451
        return $this;
452
    }
453
454
    /**
455
     * {@inheritdoc}
456
     */
457
    public function getDepth()
458
    {
459
        return $this->depth;
460
    }
461
462
    /**
463
     * {@inheritdoc}
464
     */
465
    public function setDepth($depth)
466
    {
467
        $this->depth = $depth;
468
469
        return $this;
470
    }
471
472
    /**
473
     * {@inheritdoc}
474
     */
475
    public function getShippingWeight()
476
    {
477
        return $this->getWeight();
478
    }
479
480
    /**
481
     * {@inheritdoc}
482
     */
483
    public function getShippingWidth()
484
    {
485
        return $this->getWidth();
486
    }
487
488
    /**
489
     * {@inheritdoc}
490
     */
491
    public function getShippingHeight()
492
    {
493
        return $this->getHeight();
494
    }
495
496
    /**
497
     * {@inheritdoc}
498
     */
499
    public function getShippingDepth()
500
    {
501
        return $this->getDepth();
502
    }
503
504
    /**
505
     * {@inheritdoc}
506
     */
507
    public function getShippingVolume()
508
    {
509
        return $this->depth * $this->height * $this->width;
510
    }
511
512
    /**
513
     * {@inheritdoc}
514
     */
515
    public function isPriceReduced()
516
    {
517
        return $this->originalPrice > $this->price;
518
    }
519
520
    /**
521
     * {@inheritdoc}
522
     */
523
    public function getTaxCategory()
524
    {
525
        return $this->taxCategory;
526
    }
527
528
    /**
529
     * {@inheritdoc}
530
     */
531
    public function setTaxCategory(TaxCategoryInterface $category = null)
532
    {
533
        $this->taxCategory = $category;
534
    }
535
}
536