removeSelectedProductsFromWishlist()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 6
rs 10
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\WishlistItem;
14
use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveSelectedProductsFromWishlist;
15
use Doctrine\Common\Collections\Collection;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
18
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
21
use Symfony\Contracts\Translation\TranslatorInterface;
22
23
final class RemoveSelectedProductsFromWishlistHandler implements MessageHandlerInterface
24
{
25
    private ProductVariantRepositoryInterface $productVariantRepository;
26
27
    private EntityManagerInterface $wishlistProductManager;
28
29
    private FlashBagInterface $flashBag;
30
31
    private TranslatorInterface $translator;
32
33
    private int $itemsProcessed = 0;
34
35
    public function __construct(
36
        ProductVariantRepositoryInterface $productVariantRepository,
37
        EntityManagerInterface $wishlistProductManager,
38
        FlashBagInterface $flashBag,
39
        TranslatorInterface $translator
40
    ) {
41
        $this->productVariantRepository = $productVariantRepository;
42
        $this->wishlistProductManager = $wishlistProductManager;
43
        $this->flashBag = $flashBag;
44
        $this->translator = $translator;
45
    }
46
47
    public function __invoke(RemoveSelectedProductsFromWishlist $removeSelectedProductsFromWishlistCommand): void
48
    {
49
        $this->removeSelectedProductsFromWishlist($removeSelectedProductsFromWishlistCommand->getWishlistProducts());
50
51
        $this->addFlashMessage();
52
    }
53
54
    private function removeSelectedProductsFromWishlist(Collection $wishlistProducts): void
55
    {
56
        /** @var WishlistItem $wishlistProduct */
57
        foreach ($wishlistProducts as $wishlistProduct) {
58
            if ($wishlistProduct->isSelected()) {
59
                $this->removeProductFromWishlist($wishlistProduct);
60
            }
61
        }
62
    }
63
64
    private function removeProductFromWishlist(WishlistItem $wishlistProduct): void
65
    {
66
        $productVariant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant());
67
68
        if (null === $productVariant) {
69
            throw new NotFoundHttpException();
70
        }
71
72
        $this->wishlistProductManager->remove($wishlistProduct->getWishlistProduct());
73
        ++$this->itemsProcessed;
74
    }
75
76
    private function addFlashMessage(): void
77
    {
78
        if (0 < $this->itemsProcessed) {
79
            $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.removed_selected_wishlist_items'));
80
81
            return;
82
        }
83
84
        $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
85
    }
86
}
87