Passed
Pull Request — master (#85)
by
unknown
03:29
created

ExportWishlistToPdfAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 2
b 0
f 0
nc 1
nop 8
dl 0
loc 18
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
76
            $wishlistProductCommand->setWishlistProduct($wishlistProductItem);
77
            $commandsArray->add($wishlistProductCommand);
78
        }
79
80
        $form = $this->formFactory->create(
81
            WishlistCollectionType::class,
82
            [
83
                'items' => $commandsArray,
84
            ],
85
            [
86
                'cart' => $cart,
87
            ]
88
        );
89
90
        $form->handleRequest($request);
91
92
        if ($form->isSubmitted() && $form->isValid()) {
93
            $wishlistProducts = $form->get('items')->getData();
94
95
            if (!$this->exporterWishlistToPdf->handleCartItems($wishlistProducts, $request)) {
96
                $this->flashBag->add(
97
                    'error',
98
                    $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products')
99
                );
100
            }
101
102
            return new RedirectResponse(
103
                $this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products')
104
            );
105
        }
106
107
        foreach ($form->getErrors() as $error) {
108
            $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

108
            $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...
109
        }
110
111
        return new Response(
112
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig',
113
                [
114
                    'wishlist' => $wishlist,
115
                    'form' => $form->createView(),
116
                ]
117
            )
118
        );
119
    }
120
}
121