Completed
Push — master ( 09ebf3...a2507a )
by Joachim
10s
created

ProductsAwareTrait::initializeProductsCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\SyliusBrandPlugin\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Sylius\Component\Core\Model\ProductInterface;
10
11
trait ProductsAwareTrait
12
{
13
    /** @var Collection|BrandAwareInterface[]|ProductInterface[] */
14
    protected $products;
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function initializeProductsCollection(): void
20
    {
21
        $this->products = new ArrayCollection();
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getProducts(): Collection
28
    {
29
        return $this->products;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->products could return the type Loevgaard\SyliusBrandPlu...odel\ProductInterface[] which is incompatible with the type-hinted return Doctrine\Common\Collections\Collection. Consider adding an additional type-check to rule them out.
Loading history...
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function hasProduct(BrandAwareInterface $product): bool
36
    {
37
        return $this->products->contains($product);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function addProduct(BrandAwareInterface $product): void
44
    {
45
        if (false === $this->hasProduct($product)) {
46
            /** @var ProductsAwareInterface $this */
47
            $product->setBrand($this);
48
            $this->products->add($product);
0 ignored issues
show
Bug introduced by
Accessing products on the interface Loevgaard\SyliusBrandPlu...\ProductsAwareInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
49
        }
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function removeProduct(BrandAwareInterface $product): void
56
    {
57
        if (true === $this->hasProduct($product)) {
58
            $product->setBrand(null);
59
            $this->products->removeElement($product);
60
        }
61
    }
62
}
63