Passed
Pull Request — master (#84)
by
unknown
03:57
created

RemoveSelectedProductsFromWishlistHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handleItem() 0 17 4
A __invoke() 0 19 5
A __construct() 0 12 1
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\CommandHandler\Wishlist;
12
13
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct;
14
use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveSelectedProductsFromWishlist;
15
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
16
use Doctrine\Common\Collections\Collection;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
19
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
22
use Symfony\Contracts\Translation\TranslatorInterface;
23
24
final class RemoveSelectedProductsFromWishlistHandler implements MessageHandlerInterface
25
{
26
    private ProductVariantRepositoryInterface $productVariantRepository;
27
28
    private WishlistContextInterface $wishlistContext;
29
30
    private EntityManagerInterface $wishlistProductManager;
31
32
    private FlashBagInterface $flashBag;
33
34
    private TranslatorInterface $translator;
35
36
    public function __construct(
37
        ProductVariantRepositoryInterface $productVariantRepository,
38
        WishlistContextInterface $wishlistContext,
39
        EntityManagerInterface $wishlistProductManager,
40
        FlashBagInterface $flashBag,
41
        TranslatorInterface $translator
42
    ) {
43
        $this->productVariantRepository = $productVariantRepository;
44
        $this->wishlistContext = $wishlistContext;
45
        $this->wishlistProductManager = $wishlistProductManager;
46
        $this->flashBag = $flashBag;
47
        $this->translator = $translator;
48
    }
49
50
    public function __invoke(RemoveSelectedProductsFromWishlist $removeSelectedProductsFromWishlist): void
51
    {
52
        $itemsAdded = 0;
53
        $wishlistItems = $removeSelectedProductsFromWishlist->getWishlist()->getWishlistProducts();
54
55
        /** @var AddWishlistProduct $wishlistProduct */
56
        foreach ($removeSelectedProductsFromWishlist->getWishlistProducts() as $wishlistProduct) {
57
            if (!$wishlistProduct->isSelected()) {
58
                continue;
59
            }
60
            if ($this->handleItem($wishlistProduct, $wishlistItems)) {
61
                ++$itemsAdded;
62
            }
63
        }
64
65
        if (0 < $itemsAdded) {
66
            $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.removed_selected_wishlist_items'));
67
        } else {
68
            $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
69
        }
70
    }
71
72
    private function handleItem(AddWishlistProduct $wishlistProduct, Collection $wishlistItems): bool
73
    {
74
        $productVariant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant());
75
76
        if (null === $productVariant) {
77
            throw new NotFoundHttpException();
78
        }
79
80
        foreach ($wishlistItems as $wishlistProductEntity) {
81
            if ($productVariant === $wishlistProductEntity->getVariant()) {
82
                $this->wishlistProductManager->remove($wishlistProductEntity);
83
84
                return true;
85
            }
86
        }
87
88
        return false;
89
    }
90
}
91