Completed
Push — master ( 4c9fb6...4ad644 )
by
unknown
25s queued 11s
created

Wishlist::getProductVariants()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusWishlistPlugin\Entity;
14
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\Common\Collections\Collection;
17
use Sylius\Component\Core\Model\ProductInterface;
18
use Sylius\Component\Core\Model\ProductVariantInterface;
19
use Sylius\Component\Core\Model\ShopUserInterface;
20
21
class Wishlist implements WishlistInterface
22
{
23
    protected ?int $id = null;
24
25
    /** @var Collection|WishlistProductInterface[] */
26
    protected $wishlistProducts;
27
28
    protected ?ShopUserInterface $shopUser = null;
29
30
    /** @var WishlistTokenInterface|null */
31
    protected $token;
32
33
    public function __construct()
34
    {
35
        $this->wishlistProducts = new ArrayCollection();
36
        $this->token = new WishlistToken();
37
        $this->id = null;
38
    }
39
40
    public function getId(): ?int
41
    {
42
        return $this->id;
43
    }
44
45
    public function getProducts(): Collection
46
    {
47
        $products = [];
48
49
        foreach ($this->wishlistProducts as $wishlistProduct) {
50
            $products[] = $wishlistProduct->getProduct();
51
        }
52
53
        return new ArrayCollection($products);
54
    }
55
56
    /**
57
     * @return Collection<int,ProductVariantInterface|null>
58
     */
59
    public function getProductVariants(): Collection
60
    {
61
        $variants = [];
62
63
        foreach ($this->wishlistProducts as $wishlistProduct) {
64
            $variants[] = $wishlistProduct->getVariant();
65
        }
66
67
        return new ArrayCollection($variants);
68
    }
69
70
    public function hasProductVariant(ProductVariantInterface $productVariant): bool
71
    {
72
        foreach ($this->wishlistProducts as $wishlistProduct) {
73
            if ($productVariant === $wishlistProduct->getVariant()) {
74
                return true;
75
            }
76
        }
77
78
        return false;
79
    }
80
81
    public function getWishlistProducts(): Collection
82
    {
83
        return $this->wishlistProducts;
84
    }
85
86
    public function hasProduct(ProductInterface $product): bool
87
    {
88
        foreach ($this->wishlistProducts as $wishlistProduct) {
89
            if ($product === $wishlistProduct->getProduct()) {
90
                return true;
91
            }
92
        }
93
94
        return false;
95
    }
96
97
    public function setWishlistProducts(Collection $wishlistProducts): void
98
    {
99
        $this->wishlistProducts = $wishlistProducts;
100
    }
101
102
    public function hasWishlistProduct(WishlistProductInterface $wishlistProduct): bool
103
    {
104
        return $this->wishlistProducts->contains($wishlistProduct);
105
    }
106
107
    public function addWishlistProduct(WishlistProductInterface $wishlistProduct): void
108
    {
109
        if (!$this->hasProductVariant($wishlistProduct->getVariant())) {
0 ignored issues
show
Bug introduced by
It seems like $wishlistProduct->getVariant() can also be of type null; however, parameter $productVariant of BitBag\SyliusWishlistPlu...st::hasProductVariant() does only seem to accept Sylius\Component\Core\Mo...ProductVariantInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

109
        if (!$this->hasProductVariant(/** @scrutinizer ignore-type */ $wishlistProduct->getVariant())) {
Loading history...
110
            $wishlistProduct->setWishlist($this);
111
            $this->wishlistProducts->add($wishlistProduct);
112
        }
113
    }
114
115
    public function getShopUser(): ?ShopUserInterface
116
    {
117
        return $this->shopUser;
118
    }
119
120
    public function setShopUser(ShopUserInterface $shopUser): void
121
    {
122
        $this->shopUser = $shopUser;
123
    }
124
125
    public function getToken(): string
126
    {
127
        return (string) $this->token;
128
    }
129
130
    public function setToken(string $token): void
131
    {
132
        $this->token = new WishlistToken($token);
133
    }
134
135
    public function removeProduct(WishlistProductInterface $product): self
136
    {
137
        if ($this->hasWishlistProduct($product)) {
138
            $this->wishlistProducts->removeElement($product);
139
        }
140
141
        return $this;
142
    }
143
144
    public function removeProductVariant(ProductVariantInterface $variant): self
145
    {
146
        foreach ($this->wishlistProducts as $wishlistProduct) {
147
            if ($wishlistProduct->getVariant() === $variant) {
148
                $this->wishlistProducts->removeElement($wishlistProduct);
149
            }
150
        }
151
152
        return $this;
153
    }
154
}
155