ProductVariantDepositTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setDepositTaxCategory() 0 3 1
A hasChannelDeposit() 0 3 1
A getDepositTaxCategory() 0 3 1
A removeChannelDeposit() 0 7 3
A addChannelDeposit() 0 7 3
A getChannelDeposits() 0 3 1
A initProductVariantDepositTrait() 0 3 1
A getDepositPriceByChannel() 0 13 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 Doctrine\ORM\Mapping as ORM;
10
use Sylius\Component\Core\Model\ChannelInterface;
11
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
12
13
/**
14
 * Trait that implements the deposit functionality
15
 * Used in:
16
 * <li>@see ProductVariant</li>
17
 */
18
trait ProductVariantDepositTrait
19
{
20
    public function initProductVariantDepositTrait(): void
21
    {
22
        $this->channelDeposits = new ArrayCollection();
23
    }
24
25
    /**
26
     * @var Collection
27
     *
28
     * @psalm-var Collection<array-key, ChannelDepositInterface>
29
     *
30
     * @ORM\OneToMany(
31
     *     targetEntity="\Gewebe\SyliusProductDepositPlugin\Entity\ChannelDepositInterface",
32
     *     mappedBy="productVariant",
33
     *     indexBy="channelCode",
34
     *     cascade={"all"},
35
     *     orphanRemoval=true
36
     * )
37
     */
38
    protected $channelDeposits;
39
40
    /**
41
     * @var TaxCategoryInterface|null
42
     *
43
     * @ORM\ManyToOne(targetEntity="\Sylius\Component\Taxation\Model\TaxCategoryInterface", cascade={"all"}, fetch="EAGER")
44
     * @ORM\JoinColumn(name="deposit_tax_category_id", referencedColumnName="id", onDelete="SET NULL")
45
     */
46
    protected $depositTaxCategory;
47
48
    /**
49
     * Returns all deposit elements for all channels
50
     * @psalm-return Collection<array-key, ChannelDepositInterface>
51
     */
52
    public function getChannelDeposits(): Collection
53
    {
54
        return $this->channelDeposits;
55
    }
56
57
    public function getDepositPriceByChannel(ChannelInterface $channel): ?int
58
    {
59
        $channelCode = $channel->getCode();
60
        if ($channelCode === null) {
61
            return null;
62
        }
63
64
        $channelDeposit = $this->channelDeposits->get($channelCode);
65
        if (!$channelDeposit instanceof ChannelDepositInterface) {
66
            return null;
67
        }
68
69
        return $channelDeposit->getPrice() ?? null;
70
    }
71
72
    public function hasChannelDeposit(ChannelDepositInterface $channelDeposit): bool
73
    {
74
        return $this->channelDeposits->contains($channelDeposit);
75
    }
76
77
    public function addChannelDeposit(ChannelDepositInterface $channelDeposit): void
78
    {
79
        $channelCode = $channelDeposit->getChannelCode();
80
81
        if ($channelCode !== null && !$this->hasChannelDeposit($channelDeposit)) {
82
            $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

82
            $channelDeposit->setProductVariant(/** @scrutinizer ignore-type */ $this);
Loading history...
83
            $this->channelDeposits->set($channelCode, $channelDeposit);
84
        }
85
    }
86
87
    public function removeChannelDeposit(ChannelDepositInterface $channelDeposit): void
88
    {
89
        $channelCode = $channelDeposit->getChannelCode();
90
91
        if ($channelCode !== null && $this->hasChannelDeposit($channelDeposit)) {
92
            $channelDeposit->setProductVariant(null);
93
            $this->channelDeposits->remove($channelCode);
94
        }
95
    }
96
97
    public function getDepositTaxCategory(): ?TaxCategoryInterface
98
    {
99
        return $this->depositTaxCategory;
100
    }
101
102
    public function setDepositTaxCategory(TaxCategoryInterface $depositTaxCategory): void
103
    {
104
        $this->depositTaxCategory = $depositTaxCategory;
105
    }
106
}
107