Wishlist::removeProduct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusWishlistPlugin\Entity;
12
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\Common\Collections\Collection;
15
use Sylius\Component\Core\Model\ProductInterface;
16
use Sylius\Component\Core\Model\ProductVariantInterface;
17
use Sylius\Component\Core\Model\ShopUserInterface;
18
19
class Wishlist implements WishlistInterface
20
{
21
    protected ?int $id = null;
22
23
    /** @var Collection|WishlistProductInterface[] */
24
    protected $wishlistProducts;
25
26
    protected ?ShopUserInterface $shopUser = null;
27
28
    /** @var WishlistTokenInterface|null */
29
    protected $token;
30
31
    public function __construct()
32
    {
33
        $this->wishlistProducts = new ArrayCollection();
34
        $this->token = new WishlistToken();
35
        $this->id = null;
36
    }
37
38
    public function getId(): ?int
39
    {
40
        return $this->id;
41
    }
42
43
    public function getProducts(): Collection
44
    {
45
        $products = [];
46
47
        foreach ($this->wishlistProducts as $wishlistProduct) {
48
            $products[] = $wishlistProduct->getProduct();
49
        }
50
51
        return new ArrayCollection($products);
52
    }
53
54
    /**
55
     * @return Collection<int,ProductVariantInterface|null>
56
     */
57
    public function getProductVariants(): Collection
58
    {
59
        $variants = [];
60
61
        foreach ($this->wishlistProducts as $wishlistProduct) {
62
            $variants[] = $wishlistProduct->getVariant();
63
        }
64
65
        return new ArrayCollection($variants);
66
    }
67
68
    public function hasProductVariant(ProductVariantInterface $productVariant): bool
69
    {
70
        foreach ($this->wishlistProducts as $wishlistProduct) {
71
            if ($productVariant === $wishlistProduct->getVariant()) {
72
                return true;
73
            }
74
        }
75
76
        return false;
77
    }
78
79
    public function getWishlistProducts(): Collection
80
    {
81
        return $this->wishlistProducts;
82
    }
83
84
    public function hasProduct(ProductInterface $product): bool
85
    {
86
        foreach ($this->wishlistProducts as $wishlistProduct) {
87
            if ($product === $wishlistProduct->getProduct()) {
88
                return true;
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    public function setWishlistProducts(Collection $wishlistProducts): void
96
    {
97
        $this->wishlistProducts = $wishlistProducts;
98
    }
99
100
    public function hasWishlistProduct(WishlistProductInterface $wishlistProduct): bool
101
    {
102
        return $this->wishlistProducts->contains($wishlistProduct);
103
    }
104
105
    public function addWishlistProduct(WishlistProductInterface $wishlistProduct): void
106
    {
107
        /** @var ProductVariantInterface $variant */
108
        $variant = $wishlistProduct->getVariant();
109
        if (!$this->hasProductVariant($variant)) {
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