Passed
Pull Request — master (#81)
by
unknown
14:34
created

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