Passed
Pull Request — master (#81)
by
unknown
13:37
created

RemoveSelectedProductsFromWishlistHandler::addFlashMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
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\RemoveSelectedProductsFromWishlist;
14
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItem;
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
    public function __construct(
34
        ProductVariantRepositoryInterface $productVariantRepository,
35
        EntityManagerInterface $wishlistProductManager,
36
        FlashBagInterface $flashBag,
37
        TranslatorInterface $translator
38
    ) {
39
        $this->productVariantRepository = $productVariantRepository;
40
        $this->wishlistProductManager = $wishlistProductManager;
41
        $this->flashBag = $flashBag;
42
        $this->translator = $translator;
43
    }
44
45
    public function __invoke(RemoveSelectedProductsFromWishlist $removeSelectedProductsFromWishlistCommand): void
46
    {
47
        $this->removeSelectedProductsFromWishlist($removeSelectedProductsFromWishlistCommand->getWishlistProducts());
48
49
        $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.removed_selected_wishlist_items'));
50
    }
51
52
    private function removeSelectedProductsFromWishlist(Collection $wishlistProducts): void
53
    {
54
        /** @var WishlistItem $wishlistProduct */
55
        foreach ($wishlistProducts as $wishlistProduct) {
56
            $this->removeProductFromWishlist($wishlistProduct);
57
        }
58
    }
59
60
    private function removeProductFromWishlist(WishlistItem $wishlistProduct): void
61
    {
62
        $productVariant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant());
63
64
        if (null === $productVariant) {
65
            throw new NotFoundHttpException();
66
        }
67
68
        $this->wishlistProductManager->remove($wishlistProduct->getWishlistProduct());
69
    }
70
}
71