Passed
Pull Request — master (#81)
by
unknown
03:50
created

CleanWishlistAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 48
rs 10
c 3
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A __invoke() 0 18 2
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\Controller\Action;
12
13
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
14
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
15
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
use Symfony\Contracts\Translation\TranslatorInterface;
23
24
final class CleanWishlistAction
25
{
26
    private WishlistContextInterface $wishlistContext;
27
28
    private WishlistRepositoryInterface $wishlistRepository;
29
30
    private EntityManagerInterface $wishlistManager;
31
32
    private FlashBagInterface $flashBag;
33
34
    private TranslatorInterface $translator;
35
36
    private UrlGeneratorInterface $urlGenerator;
37
38
    public function __construct(
39
        WishlistContextInterface $wishlistContext,
40
        WishlistRepositoryInterface $wishlistRepository,
41
        EntityManagerInterface $wishlistManager,
42
        FlashBagInterface $flashBag,
43
        TranslatorInterface $translator,
44
        UrlGeneratorInterface $urlGenerator
45
    ) {
46
        $this->wishlistContext = $wishlistContext;
47
        $this->wishlistRepository = $wishlistRepository;
48
        $this->wishlistManager = $wishlistManager;
49
        $this->urlGenerator = $urlGenerator;
50
        $this->flashBag = $flashBag;
51
        $this->translator = $translator;
52
    }
53
54
    public function __invoke(int $wishlistId, Request $request): Response
55
    {
56
        /** @var WishlistInterface $wishlist */
57
        $wishlist = $this->wishlistRepository->find($wishlistId);
58
59
        $wishlistProducts = $wishlist->getWishlistProducts();
60
61
        foreach ($wishlistProducts as $products) {
62
            $wishlist->removeProduct($products);
63
64
        }
65
66
        $this->wishlistManager->flush();
67
68
        $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.clear_wishlist'));
69
70
        return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_show_chosen_wishlist', [
71
            'wishlistId' => $wishlistId,
72
        ])
73
        );
74
    }
75
76
}
77