Passed
Pull Request — master (#58)
by
unknown
03:36
created

WishlistUpdater::removeWishlist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusWishlistPlugin\Updater;
6
7
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
8
use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface;
9
use Doctrine\Persistence\ObjectManager;
10
use Sylius\Component\Core\Model\ProductInterface;
11
use Sylius\Component\Core\Model\ProductVariantInterface;
12
13
final class WishlistUpdater implements WishlistUpdaterInterface
14
{
15
    private ObjectManager $wishlistManager;
16
17
    private ObjectManager $wishlistProductManager;
18
19
    public function __construct(
20
        ObjectManager $wishlistManager,
21
        ObjectManager $wishlistProductManager
22
    ) {
23
        $this->wishlistManager = $wishlistManager;
24
        $this->wishlistProductManager = $wishlistProductManager;
25
    }
26
27
    public function updateWishlist(WishlistInterface $wishlist): void
28
    {
29
        $this->wishlistManager->persist($wishlist);
30
        $this->wishlistManager->flush();
31
    }
32
33
    public function removeWishlist(WishlistInterface $wishlist): void
34
    {
35
        $this->wishlistManager->remove($wishlist);
36
        $this->wishlistManager->flush();
37
    }
38
39
    public function addProductToWishlist(WishlistInterface $wishlist, WishlistProductInterface $product): WishlistInterface
40
    {
41
        $wishlist->addWishlistProduct($product);
42
        $this->updateWishlist($wishlist);
43
44
        return $wishlist;
45
    }
46
47
    public function removeProductFromWishlist(WishlistInterface $wishlist, ProductInterface $product): WishlistInterface
48
    {
49
        foreach ($wishlist->getWishlistProducts() as $wishlistProduct) {
50
            if ($product === $wishlistProduct->getProduct()) {
51
                $this->wishlistProductManager->remove($wishlistProduct);
52
            }
53
        }
54
55
        $this->wishlistProductManager->flush();
56
57
        return $wishlist;
58
    }
59
60
    public function removeProductVariantFromWishlist(WishlistInterface $wishlist, ProductVariantInterface $variant): WishlistInterface
61
    {
62
        foreach ($wishlist->getWishlistProducts() as $wishlistProduct) {
63
            if ($variant === $wishlistProduct->getVariant()) {
64
                $this->wishlistProductManager->remove($wishlistProduct);
65
            }
66
        }
67
68
        $this->wishlistProductManager->flush();
69
70
        return $wishlist;
71
    }
72
}
73