Passed
Pull Request — master (#87)
by
unknown
03:40
created

ExportWishlistToPdfAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 73
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
B __invoke() 0 35 6
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\Command\Wishlist\AddWishlistProduct;
14
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
15
use BitBag\SyliusWishlistPlugin\Exporter\ExporterWishlistToPdfInterface;
16
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Sylius\Component\Order\Context\CartContextInterface;
19
use Symfony\Component\Form\FormFactoryInterface;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
24
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
25
use Symfony\Contracts\Translation\TranslatorInterface;
26
use Twig\Environment;
27
28
final class ExportWishlistToPdfAction
29
{
30
    private WishlistContextInterface $wishlistContext;
31
32
    private CartContextInterface $cartContext;
33
34
    private FormFactoryInterface $formFactory;
35
36
    private FlashBagInterface $flashBag;
37
38
    private TranslatorInterface $translator;
39
40
    private UrlGeneratorInterface $urlGenerator;
41
42
    private Environment $twigEnvironment;
43
44
    private ExporterWishlistToPdfInterface $exporterWishlistToPdf;
45
46
    public function __construct(
47
        WishlistContextInterface          $wishlistContext,
48
        CartContextInterface              $cartContext,
49
        FormFactoryInterface              $formFactory,
50
        FlashBagInterface                 $flashBag,
51
        TranslatorInterface               $translator,
52
        UrlGeneratorInterface             $urlGenerator,
53
        Environment                       $twigEnvironment,
54
        ExporterWishlistToPdfInterface    $exporterWishlistToPdf
55
    ) {
56
        $this->wishlistContext = $wishlistContext;
57
        $this->cartContext = $cartContext;
58
        $this->formFactory = $formFactory;
59
        $this->flashBag = $flashBag;
60
        $this->translator = $translator;
61
        $this->urlGenerator = $urlGenerator;
62
        $this->twigEnvironment = $twigEnvironment;
63
        $this->exporterWishlistToPdf = $exporterWishlistToPdf;
64
    }
65
66
    public function __invoke(Request $request): Response
67
    {
68
        $wishlist = $this->wishlistContext->getWishlist($request);
69
        $cart = $this->cartContext->getCart();
70
71
        $commandsArray = new ArrayCollection();
72
73
        foreach ($wishlist->getWishlistProducts() as $wishlistProductItem) {
74
            $wishlistProductCommand = new AddWishlistProduct();
75
            $wishlistProductCommand->setWishlistProduct($wishlistProductItem);
76
            $commandsArray->add($wishlistProductCommand);
77
        }
78
79
        $form = $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
80
            'cart' => $cart,
81
        ]);
82
83
        $form->handleRequest($request);
84
85
        if ($form->isSubmitted() && $form->isValid()) {
86
            $wishlistProducts = $form->get("items")->getData();
87
            if (!$this->exporterWishlistToPdf->handleCartItems($wishlistProducts, $request)) {
88
                $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
89
            }
90
            return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
91
        }
92
93
        foreach ($form->getErrors() as $error) {
94
            $this->flashBag->add('error', $error->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
            $this->flashBag->add('error', $error->/** @scrutinizer ignore-call */ getMessage());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
        }
96
97
        return new Response(
98
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
99
                'wishlist' => $wishlist,
100
                'form' => $form->createView(),
101
            ])
102
        );
103
    }
104
}
105