Passed
Pull Request — master (#87)
by
unknown
05:18
created

ExportWishlistToPdfAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

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

2 Methods

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

96
            $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...
97
        }
98
99
        return new Response(
100
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
101
                'wishlist' => $wishlist,
102
                'form' => $form->createView(),
103
            ])
104
        );
105
    }
106
}
107