Passed
Pull Request — master (#84)
by
unknown
04:09
created

RemoveSelectedProductsFromWishlistHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 26 7
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\RemoveSelectedProductsFromWishlist;
14
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
17
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
20
use Symfony\Contracts\Translation\TranslatorInterface;
21
22
final class RemoveSelectedProductsFromWishlistHandler implements MessageHandlerInterface
23
{
24
    private ProductVariantRepositoryInterface $productVariantRepository;
25
26
    private WishlistContextInterface $wishlistContext;
27
28
    private EntityManagerInterface $wishlistProductManager;
29
30
    private FlashBagInterface $flashBag;
31
32
    private TranslatorInterface $translator;
33
34
    public function __construct(
35
        ProductVariantRepositoryInterface $productVariantRepository,
36
        WishlistContextInterface $wishlistContext,
37
        EntityManagerInterface $wishlistProductManager,
38
        FlashBagInterface $flashBag,
39
        TranslatorInterface $translator
40
    ) {
41
        $this->productVariantRepository = $productVariantRepository;
42
        $this->wishlistContext = $wishlistContext;
43
        $this->wishlistProductManager = $wishlistProductManager;
44
        $this->flashBag = $flashBag;
45
        $this->translator = $translator;
46
    }
47
48
    public function __invoke(RemoveSelectedProductsFromWishlist $removeSelectedProductsFromWishlist): void
49
    {
50
        $itemsAdded = 0;
51
52
        foreach ($removeSelectedProductsFromWishlist->getWishlistProducts() as $wishlistProduct) {
53
            if ($wishlistProduct->isSelected()) {
54
                $variant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant());
55
56
                if (null === $variant) {
57
                    throw new NotFoundHttpException();
58
                }
59
60
                $wishlist = $removeSelectedProductsFromWishlist->getWishlist();
61
62
                foreach ($wishlist->getWishlistProducts() as $wishlistProductEntity) {
63
                    if ($variant === $wishlistProductEntity->getVariant()) {
64
                        $this->wishlistProductManager->remove($wishlistProductEntity);
65
                        ++$itemsAdded;
66
                    }
67
                }
68
            }
69
        }
70
        if (0 < $itemsAdded) {
71
            $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.removed_selected_wishlist_items'));
72
        } else {
73
            $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
74
        }
75
    }
76
}
77