Passed
Pull Request — master (#58)
by
unknown
13:18 queued 09:31
created

WishlistUpdater::updateWishlist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
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\ProductVariantInterface;
11
12
final class WishlistUpdater implements WishlistUpdaterInterface
13
{
14
    private ObjectManager $wishlistManager;
15
16
    private ObjectManager $wishlistProductManager;
17
18
    public function __construct(
19
        ObjectManager $wishlistManager,
20
        ObjectManager $wishlistProductManager
21
    ) {
22
        $this->wishlistManager = $wishlistManager;
23
        $this->wishlistProductManager = $wishlistProductManager;
24
    }
25
26
    public function updateWishlist(WishlistInterface $wishlist): void
27
    {
28
        $this->wishlistManager->persist($wishlist);
29
        $this->wishlistManager->flush();
30
    }
31
32
    public function removeWishlist(WishlistInterface $wishlist): void
33
    {
34
        $this->wishlistManager->remove($wishlist);
35
        $this->wishlistManager->flush();
36
    }
37
38
    public function addProductToWishlist(WishlistInterface $wishlist, WishlistProductInterface $product): WishlistInterface
39
    {
40
        $wishlist->addWishlistProduct($product);
41
        $this->updateWishlist($wishlist);
42
43
        return $wishlist;
44
    }
45
46
    public function removeProductFromWishlist(WishlistInterface $wishlist, WishlistProductInterface $product): WishlistInterface
47
    {
48
        foreach ($wishlist->getWishlistProducts() as $wishlistProduct) {
49
            if ($product === $wishlistProduct->getProduct()) {
50
                $this->wishlistProductManager->remove($wishlistProduct);
51
            }
52
        }
53
54
        $this->wishlistProductManager->flush();
55
56
        return $wishlist;
57
    }
58
59
    public function removeProductVariantFromWishlist(WishlistInterface $wishlist, ProductVariantInterface $variant): WishlistInterface
60
    {
61
        foreach ($wishlist->getWishlistProducts() as $wishlistProduct) {
62
            if ($variant === $wishlistProduct->getVariant()) {
63
                $this->wishlistProductManager->remove($wishlistProduct);
64
            }
65
        }
66
67
        $this->wishlistProductManager->flush();
68
69
        return $wishlist;
70
    }
71
}
72