Completed
Branch master (734dbe)
by G
12:13
created

ProductVariantDepositTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 111
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDepositTaxCategory() 0 3 1
A hasChannelDepositForChannel() 0 3 1
A removeChannelDeposit() 0 5 2
A setDepositTaxCategory() 0 3 1
A getChannelDeposits() 0 3 1
A hasChannelDeposit() 0 3 1
A addChannelDeposit() 0 5 2
A initProductVariantDepositTrait() 0 3 1
A getChannelDepositForChannel() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gweb\SyliusProductDepositPlugin\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Sylius\Component\Core\Model\ChannelInterface;
10
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
11
12
/**
13
 * Trait that implements the deposit functionality
14
 * Used in:
15
 * <li>@see ProductVariant</li>
16
 *
17
 * @author Gerd Weitenberg <[email protected]>
18
 */
19
trait ProductVariantDepositTrait
20
{
21
    public function initProductVariantDepositTrait()
22
    {
23
        $this->channelDeposits = new ArrayCollection();
24
    }
25
26
    /**
27
     * @var Collection
28
     *
29
     * @ORM\OneToMany(
30
     *     targetEntity="\Gweb\SyliusProductDepositPlugin\Entity\ChannelDepositInterface",
31
     *     mappedBy="productVariant",
32
     *     indexBy="channelCode",
33
     *     cascade={"all"},
34
     *     orphanRemoval=true
35
     * )
36
     */
37
    protected $channelDeposits;
38
39
    /**
40
     * @var TaxCategoryInterface
41
     *
42
     * @ORM\ManyToOne(targetEntity="\Sylius\Component\Taxation\Model\TaxCategoryInterface", cascade={"all"}, fetch="EAGER")
43
     * @ORM\JoinColumn(name="deposit_tax_category_id", referencedColumnName="id", onDelete="SET NULL")
44
     */
45
    protected $depositTaxCategory;
46
47
    /**
48
     * Returns all deposit elements for all channels
49
     * @return Collection
50
     */
51
    public function getChannelDeposits(): Collection
52
    {
53
        return $this->channelDeposits;
54
    }
55
56
    /**
57
     * Returns the deposit element for one channel
58
     * @param ChannelInterface $channel
59
     * @return ChannelDepositInterface|null
60
     */
61
    public function getChannelDepositForChannel(ChannelInterface $channel): ?ChannelDepositInterface
62
    {
63
        if ($this->channelDeposits->containsKey($channel->getCode())) {
64
            return $this->channelDeposits->get($channel->getCode());
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * Check if channel has deposit element
72
     * @param ChannelInterface $channel
73
     * @return bool
74
     */
75
    public function hasChannelDepositForChannel(ChannelInterface $channel): bool
76
    {
77
        return null !== $this->getChannelDepositForChannel($channel);
78
    }
79
80
    /**
81
     * Check if has deposit element
82
     * @param ChannelDepositInterface $channelDeposit
83
     * @return bool
84
     */
85
    public function hasChannelDeposit(ChannelDepositInterface $channelDeposit): bool
86
    {
87
        return $this->channelDeposits->contains($channelDeposit);
88
    }
89
90
    /**
91
     * Adds an deposit element to the list
92
     * @param ChannelDepositInterface $channelDeposit
93
     * @return void
94
     */
95
    public function addChannelDeposit(ChannelDepositInterface $channelDeposit): void
96
    {
97
        if (!$this->hasChannelDeposit($channelDeposit)) {
98
            $channelDeposit->setProductVariant($this);
0 ignored issues
show
Bug introduced by
$this of type Gweb\SyliusProductDeposi...ductVariantDepositTrait is incompatible with the type Gweb\SyliusProductDeposi...ctVariantInterface|null expected by parameter $productVariants of Gweb\SyliusProductDeposi...ce::setProductVariant(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
            $channelDeposit->setProductVariant(/** @scrutinizer ignore-type */ $this);
Loading history...
99
            $this->channelDeposits->set($channelDeposit->getChannelCode(), $channelDeposit);
100
        }
101
    }
102
103
    /**
104
     * Removes a deposit element from the list
105
     * @param ChannelDepositInterface $channelDeposit
106
     * @return void
107
     */
108
    public function removeChannelDeposit(ChannelDepositInterface $channelDeposit): void
109
    {
110
        if ($this->hasChannelDeposit($channelDeposit)) {
111
            $channelDeposit->setProductVariant(null);
112
            $this->channelDeposits->remove($channelDeposit->getChannelCode());
113
        }
114
    }
115
116
    /**
117
     * @return TaxCategoryInterface
118
     */
119
    public function getDepositTaxCategory(): ?TaxCategoryInterface
120
    {
121
        return $this->depositTaxCategory;
122
    }
123
124
    /**
125
     * @param TaxCategoryInterface $depositTaxCategory
126
     */
127
    public function setDepositTaxCategory(?TaxCategoryInterface $depositTaxCategory): void
128
    {
129
        $this->depositTaxCategory = $depositTaxCategory;
130
    }
131
}
132