Passed
Pull Request — master (#58)
by
unknown
04:25
created

WishlistUpdater::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 2
dl 0
loc 6
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 Doctrine\Persistence\ObjectManager;
9
use Sylius\Component\Core\Model\ProductVariantInterface;
10
use Sylius\Component\Product\Model\ProductInterface;
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, ProductInterface $product): WishlistInterface
39
    {
40
        $wishlist->addWishlistProduct($product);
0 ignored issues
show
Bug introduced by
$product of type Sylius\Component\Product\Model\ProductInterface is incompatible with the type BitBag\SyliusWishlistPlu...ishlistProductInterface expected by parameter $wishlistProduct of BitBag\SyliusWishlistPlu...e::addWishlistProduct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $wishlist->addWishlistProduct(/** @scrutinizer ignore-type */ $product);
Loading history...
41
        $this->updateWishlist($wishlist);
42
43
        return $wishlist;
44
    }
45
46
    public function removeProductFromWishlist(WishlistInterface $wishlist, ProductInterface $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