Passed
Pull Request — master (#81)
by
unknown
04:01
created

Wishlist::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
    protected string $name;
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 getName(): ?string
46
    {
47
        return $this->name;
48
    }
49
50
    public function setName(?string $name): void
51
    {
52
        $this->name = $name;
53
    }
54
55
    public function getProducts(): Collection
56
    {
57
        $products = [];
58
59
        foreach ($this->wishlistProducts as $wishlistProduct) {
60
            $products[] = $wishlistProduct->getProduct();
61
        }
62
63
        return new ArrayCollection($products);
64
    }
65
66
    /**
67
     * @return Collection<int,ProductVariantInterface|null>
68
     */
69
    public function getProductVariants(): Collection
70
    {
71
        $variants = [];
72
73
        foreach ($this->wishlistProducts as $wishlistProduct) {
74
            $variants[] = $wishlistProduct->getVariant();
75
        }
76
77
        return new ArrayCollection($variants);
78
    }
79
80
    public function hasProductVariant(ProductVariantInterface $productVariant): bool
81
    {
82
        foreach ($this->wishlistProducts as $wishlistProduct) {
83
            if ($productVariant === $wishlistProduct->getVariant()) {
84
                return true;
85
            }
86
        }
87
88
        return false;
89
    }
90
91
    public function getWishlistProducts(): Collection
92
    {
93
        return $this->wishlistProducts;
94
    }
95
96
    public function hasProduct(ProductInterface $product): bool
97
    {
98
        foreach ($this->wishlistProducts as $wishlistProduct) {
99
            if ($product === $wishlistProduct->getProduct()) {
100
                return true;
101
            }
102
        }
103
104
        return false;
105
    }
106
107
    public function setWishlistProducts(Collection $wishlistProducts): void
108
    {
109
        $this->wishlistProducts = $wishlistProducts;
110
    }
111
112
    public function hasWishlistProduct(WishlistProductInterface $wishlistProduct): bool
113
    {
114
        return $this->wishlistProducts->contains($wishlistProduct);
115
    }
116
117
    public function addWishlistProduct(WishlistProductInterface $wishlistProduct): void
118
    {
119
        /** @var ProductVariantInterface $variant */
120
        $variant = $wishlistProduct->getVariant();
121
        if (!$this->hasProductVariant($variant)) {
122
            $wishlistProduct->setWishlist($this);
123
            $this->wishlistProducts->add($wishlistProduct);
124
        }
125
    }
126
127
    public function getShopUser(): ?ShopUserInterface
128
    {
129
        return $this->shopUser;
130
    }
131
132
    public function setShopUser(ShopUserInterface $shopUser): void
133
    {
134
        $this->shopUser = $shopUser;
135
    }
136
137
    public function getToken(): string
138
    {
139
        return (string) $this->token;
140
    }
141
142
    public function setToken(string $token): void
143
    {
144
        $this->token = new WishlistToken($token);
145
    }
146
147
    public function removeProduct(WishlistProductInterface $product): self
148
    {
149
        if ($this->hasWishlistProduct($product)) {
150
            $this->wishlistProducts->removeElement($product);
151
        }
152
153
        return $this;
154
    }
155
156
    public function removeProductVariant(ProductVariantInterface $variant): self
157
    {
158
        foreach ($this->wishlistProducts as $wishlistProduct) {
159
            if ($wishlistProduct->getVariant() === $variant) {
160
                $this->wishlistProducts->removeElement($wishlistProduct);
161
            }
162
        }
163
164
        return $this;
165
    }
166
167
    public function clear(): void
168
    {
169
        $this->wishlistProducts = new ArrayCollection();
170
    }
171
}
172