Passed
Push — master ( 9d9530...9d60e1 )
by G
07:54
created

ProductVariantDepositTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 121
rs 10
c 1
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getChannelDeposits() 0 3 1
A setDepositTaxCategory() 0 3 1
A hasChannelDepositForChannel() 0 3 1
A hasChannelDeposit() 0 3 1
A getDepositTaxCategory() 0 3 1
A getChannelDepositForChannel() 0 9 3
A removeChannelDeposit() 0 7 3
A addChannelDeposit() 0 7 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewebe\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
trait ProductVariantDepositTrait
18
{
19
    public function initProductVariantDepositTrait(): void
20
    {
21
        $this->channelDeposits = new ArrayCollection();
22
    }
23
24
    /**
25
     * @var Collection
26
     *
27
     * @psalm-var Collection<array-key, ChannelDepositInterface>
28
     *
29
     * @ORM\OneToMany(
30
     *     targetEntity="\Gewebe\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|ChannelDepositInterface[]
50
     *
51
     * @psalm-return Collection<array-key, ChannelDepositInterface>
52
     */
53
    public function getChannelDeposits(): Collection
54
    {
55
        return $this->channelDeposits;
56
    }
57
58
    /**
59
     * Returns the deposit element for one channel
60
     * @param ChannelInterface $channel
61
     * @return ChannelDepositInterface|null
62
     */
63
    public function getChannelDepositForChannel(ChannelInterface $channel): ?ChannelDepositInterface
64
    {
65
        $channelCode = $channel->getCode();
66
67
        if ($channelCode !== null && $this->channelDeposits->containsKey($channelCode)) {
68
            return $this->channelDeposits->get($channelCode);
69
        }
70
71
        return null;
72
    }
73
74
    /**
75
     * Check if channel has deposit element
76
     * @param ChannelInterface $channel
77
     * @return bool
78
     */
79
    public function hasChannelDepositForChannel(ChannelInterface $channel): bool
80
    {
81
        return null !== $this->getChannelDepositForChannel($channel);
82
    }
83
84
    /**
85
     * Check if has deposit element
86
     * @param ChannelDepositInterface $channelDeposit
87
     * @return bool
88
     */
89
    public function hasChannelDeposit(ChannelDepositInterface $channelDeposit): bool
90
    {
91
        return $this->channelDeposits->contains($channelDeposit);
92
    }
93
94
    /**
95
     * Adds an deposit element to the list
96
     * @param ChannelDepositInterface $channelDeposit
97
     * @return void
98
     */
99
    public function addChannelDeposit(ChannelDepositInterface $channelDeposit): void
100
    {
101
        $channelCode = $channelDeposit->getChannelCode();
102
103
        if ($channelCode !== null && !$this->hasChannelDeposit($channelDeposit)) {
104
            $channelDeposit->setProductVariant($this);
0 ignored issues
show
Bug introduced by
$this of type Gewebe\SyliusProductDepo...ductVariantDepositTrait is incompatible with the type Gewebe\SyliusProductDepo...ctVariantInterface|null expected by parameter $productVariants of Gewebe\SyliusProductDepo...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

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