Completed
Push — master ( 0729b3...dbaae6 )
by Kamil
18:26
created

Product::removeAssociation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
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\Product\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Sylius\Component\Archetype\Model\ArchetypeInterface as BaseArchetypeInterface;
17
use Sylius\Component\Attribute\Model\AttributeValueInterface as BaseAttributeValueInterface;
18
use Sylius\Component\Resource\Model\SoftDeletableTrait;
19
use Sylius\Component\Resource\Model\TimestampableTrait;
20
use Sylius\Component\Resource\Model\ToggleableTrait;
21
use Sylius\Component\Translation\Model\AbstractTranslatable;
22
use Sylius\Component\Variation\Model\OptionInterface as BaseOptionInterface;
23
use Sylius\Component\Variation\Model\VariantInterface as BaseVariantInterface;
24
25
/**
26
 * @author Paweł Jędrzejewski <[email protected]>
27
 * @author Gonzalo Vilaseca <[email protected]>
28
 */
29
class Product extends AbstractTranslatable implements ProductInterface
30
{
31
    use SoftDeletableTrait, TimestampableTrait, ToggleableTrait;
32
33
    /**
34
     * @var mixed
35
     */
36
    protected $id;
37
38
    /**
39
     * @var null|BaseArchetypeInterface
40
     */
41
    protected $archetype;
42
43
    /**
44
     * @var \DateTime
45
     */
46
    protected $availableOn;
47
48
    /**
49
     * @var \DateTime
50
     */
51
    protected $availableUntil;
52
53
    /**
54
     * @var Collection|BaseAttributeValueInterface[]
55
     */
56
    protected $attributes;
57
58
    /**
59
     * @var Collection|BaseVariantInterface[]
60
     */
61
    protected $variants;
62
63
    /**
64
     * @var Collection|BaseOptionInterface[]
65
     */
66
    protected $options;
67
68
    /**
69
     * @var Collection|ProductAssociationInterface[]
70
     */
71
    protected $associations;
72
73
    public function __construct()
74
    {
75
        parent::__construct();
76
77
        $this->availableOn = new \DateTime();
78
        $this->attributes = new ArrayCollection();
79
        $this->associations = new ArrayCollection();
80
        $this->variants = new ArrayCollection();
81
        $this->options = new ArrayCollection();
82
        $this->createdAt = new \DateTime();
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function __toString()
89
    {
90
        return $this->getName();
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getId()
97
    {
98
        return $this->id;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getArchetype()
105
    {
106
        return $this->archetype;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function setArchetype(BaseArchetypeInterface $archetype = null)
113
    {
114
        $this->archetype = $archetype;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getName()
121
    {
122
        return $this->translate()->getName();
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function setName($name)
129
    {
130
        $this->translate()->setName($name);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function getSlug()
137
    {
138
        return $this->translate()->getSlug();
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function setSlug($slug = null)
145
    {
146
        $this->translate()->setSlug($slug);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getDescription()
153
    {
154
        return $this->translate()->getDescription();
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function setDescription($description)
161
    {
162
        $this->translate()->setDescription($description);
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function getMetaKeywords()
169
    {
170
        return $this->translate()->getMetaKeywords();
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function setMetaKeywords($metaKeywords)
177
    {
178
        $this->translate()->setMetaKeywords($metaKeywords);
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function getMetaDescription()
185
    {
186
        return $this->translate()->getMetaDescription();
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function setMetaDescription($metaDescription)
193
    {
194
        $this->translate()->setMetaDescription($metaDescription);
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function isAvailable()
201
    {
202
        return (new DateRange($this->availableOn, $this->availableUntil))->isInRange();
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function getAvailableOn()
209
    {
210
        return $this->availableOn;
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function setAvailableOn(\DateTime $availableOn = null)
217
    {
218
        $this->availableOn = $availableOn;
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public function getAvailableUntil()
225
    {
226
        return $this->availableUntil;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232
    public function setAvailableUntil(\DateTime $availableUntil = null)
233
    {
234
        $this->availableUntil = $availableUntil;
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function getAttributes()
241
    {
242
        return $this->attributes;
243
    }
244
245
    /**
246
     * {@inheritdoc}
247
     */
248
    public function setAttributes(Collection $attributes)
249
    {
250
        foreach ($attributes as $attribute) {
251
            $this->addAttribute($attribute);
252
        }
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258
    public function addAttribute(BaseAttributeValueInterface $attribute)
259
    {
260
        if (!$this->hasAttribute($attribute)) {
261
            $attribute->setProduct($this);
262
            $this->attributes->add($attribute);
263
        }
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function removeAttribute(BaseAttributeValueInterface $attribute)
270
    {
271
        if ($this->hasAttribute($attribute)) {
272
            $this->attributes->removeElement($attribute);
273
            $attribute->setProduct(null);
274
        }
275
    }
276
277
    /**
278
     * {@inheritdoc}
279
     */
280
    public function hasAttribute(BaseAttributeValueInterface $attribute)
281
    {
282
        return $this->attributes->contains($attribute);
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288
    public function hasAttributeByCode($attributeCode)
289
    {
290
        foreach ($this->attributes as $attribute) {
291
            if ($attribute->getAttribute()->getCode() === $attributeCode) {
292
                return true;
293
            }
294
        }
295
296
        return false;
297
    }
298
299
    /**
300
     * {@inheritdoc}
301
     */
302
    public function getAttributeByCode($attributeCode)
303
    {
304
        foreach ($this->attributes as $attribute) {
305
            if ($attributeCode === $attribute->getAttribute()->getCode()) {
306
                return $attribute;
307
            }
308
        }
309
310
        return null;
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     */
316
    public function getMasterVariant()
317
    {
318
        foreach ($this->variants as $variant) {
319
            if ($variant->isMaster()) {
320
                return $variant;
321
            }
322
        }
323
324
        return null;
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330
    public function setMasterVariant(BaseVariantInterface $masterVariant)
331
    {
332
        $masterVariant->setMaster(true);
333
334
        if (!$this->variants->contains($masterVariant)) {
335
            $masterVariant->setProduct($this);
336
            $this->variants->add($masterVariant);
337
        }
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    public function hasVariants()
344
    {
345
        return !$this->getVariants()->isEmpty();
346
    }
347
348
    /**
349
     * {@inheritdoc}
350
     */
351
    public function getVariants()
352
    {
353
        return $this->variants->filter(function (BaseVariantInterface $variant) {
354
            return !$variant->isDeleted() && !$variant->isMaster();
355
        });
356
    }
357
358
    /**
359
     * {@inheritdoc}
360
     */
361
    public function getAvailableVariants()
362
    {
363
        return $this->variants->filter(function (BaseVariantInterface $variant) {
364
            return !$variant->isDeleted() && !$variant->isMaster() && $variant->isAvailable();
365
        });
366
    }
367
368
    /**
369
     * {@inheritdoc}
370
     */
371
    public function setVariants(Collection $variants)
372
    {
373
        $this->variants->clear();
374
375
        foreach ($variants as $variant) {
376
            $this->addVariant($variant);
377
        }
378
    }
379
380
    /**
381
     * {@inheritdoc}
382
     */
383
    public function addVariant(BaseVariantInterface $variant)
384
    {
385
        if (!$this->hasVariant($variant)) {
386
            $variant->setProduct($this);
387
            $this->variants->add($variant);
388
        }
389
    }
390
391
    /**
392
     * {@inheritdoc}
393
     */
394
    public function removeVariant(BaseVariantInterface $variant)
395
    {
396
        if ($this->hasVariant($variant)) {
397
            $variant->setProduct(null);
398
            $this->variants->removeElement($variant);
399
        }
400
    }
401
402
    /**
403
     * {@inheritdoc}
404
     */
405
    public function hasVariant(BaseVariantInterface $variant)
406
    {
407
        return $this->variants->contains($variant);
408
    }
409
410
    /**
411
     * {@inheritdoc}
412
     */
413
    public function hasOptions()
414
    {
415
        return !$this->options->isEmpty();
416
    }
417
418
    /**
419
     * {@inheritdoc}
420
     */
421
    public function getOptions()
422
    {
423
        return $this->options;
424
    }
425
426
    /**
427
     * {@inheritdoc}
428
     */
429
    public function setOptions(Collection $options)
430
    {
431
        $this->options = $options;
432
    }
433
434
    /**
435
     * {@inheritdoc}
436
     */
437
    public function addOption(BaseOptionInterface $option)
438
    {
439
        if (!$this->hasOption($option)) {
440
            $this->options->add($option);
441
        }
442
    }
443
444
    /**
445
     * {@inheritdoc}
446
     */
447
    public function removeOption(BaseOptionInterface $option)
448
    {
449
        if ($this->hasOption($option)) {
450
            $this->options->removeElement($option);
451
        }
452
    }
453
454
    /**
455
     * {@inheritdoc}
456
     */
457
    public function hasOption(BaseOptionInterface $option)
458
    {
459
        return $this->options->contains($option);
460
    }
461
462
    /**
463
     * {@inheritdoc}
464
     */
465
    public function setDeletedAt(\DateTime $deletedAt = null)
466
    {
467
        if (null === $deletedAt) {
468
            foreach ($this->variants as $variant) {
469
                $variant->setDeletedAt(null);
470
            }
471
        }
472
473
        $this->deletedAt = $deletedAt;
474
    }
475
476
    /**
477
     * {@inheritdoc}
478
     */
479
    public function getAssociations()
480
    {
481
        return $this->associations;
482
    }
483
484
    /**
485
     * {@inheritdoc}
486
     */
487
    public function addAssociation(ProductAssociationInterface $association)
488
    {
489
        if (!$this->hasAssociation($association)) {
490
            $this->associations->add($association);
491
            $association->setOwner($this);
492
        }
493
    }
494
495
    /**
496
     * {@inheritdoc}
497
     */
498
    public function removeAssociation(ProductAssociationInterface $association)
499
    {
500
        if ($this->hasAssociation($association)) {
501
            $association->setOwner(null);
502
            $this->associations->removeElement($association);
503
        }
504
    }
505
506
    /**
507
     * {@inheritdoc}
508
     */
509
    public function hasAssociation(ProductAssociationInterface $association)
510
    {
511
        return $this->associations->contains($association);
512
    }
513
}
514