Passed
Pull Request — master (#81)
by
unknown
03:42
created

Wishlist::setWishlistProducts()   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 1
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
    /** @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
        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

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