Passed
Pull Request — master (#93)
by
unknown
04:02
created

ExportWishlistToPdfAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
c 2
b 0
f 0
dl 0
loc 84
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A __invoke() 0 38 5
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\Command\Wishlist\ExportSelectedProductsFromWishlistToPdf;
15
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
16
use BitBag\SyliusWishlistPlugin\Exporter\ExporterWishlistToPdfInterface;
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusWishlistPlu...rWishlistToPdfInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use BitBag\SyliusWishlistPlugin\Factory\Command\CommandFactory;
18
use BitBag\SyliusWishlistPlugin\Factory\Command\CommandFactoryInterface;
19
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
20
use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface;
21
use Doctrine\Common\Collections\ArrayCollection;
22
use Sylius\Component\Order\Context\CartContextInterface;
23
use Symfony\Component\Form\FormFactoryInterface;
24
use Symfony\Component\HttpFoundation\RedirectResponse;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpFoundation\Response;
27
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
28
use Symfony\Component\Messenger\HandleTrait;
29
use Symfony\Component\Messenger\MessageBusInterface;
30
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
31
use Symfony\Contracts\Translation\TranslatorInterface;
32
use Twig\Environment;
33
34
final class ExportWishlistToPdfAction
35
{
36
    use HandleTrait;
37
38
    private WishlistContextInterface $wishlistContext;
39
40
    private CartContextInterface $cartContext;
41
42
    private FormFactoryInterface $formFactory;
43
44
    private FlashBagInterface $flashBag;
45
46
    private TranslatorInterface $translator;
47
48
    private UrlGeneratorInterface $urlGenerator;
49
50
    private Environment $twigEnvironment;
51
52
    private WishlistCommandProcessorInterface $wishlistCommandProcessor;
53
54
    private CommandFactoryInterface $commandFactory;
55
56
    public function __construct(
57
        WishlistContextInterface $wishlistContext,
58
        CartContextInterface $cartContext,
59
        FormFactoryInterface $formFactory,
60
        FlashBagInterface $flashBag,
61
        TranslatorInterface $translator,
62
        UrlGeneratorInterface $urlGenerator,
63
        Environment $twigEnvironment,
64
        WishlistCommandProcessorInterface $wishlistCommandProcessor,
65
        MessageBusInterface $messageBus,
66
        CommandFactoryInterface $commandFactory
67
    ) {
68
        $this->wishlistContext = $wishlistContext;
69
        $this->cartContext = $cartContext;
70
        $this->formFactory = $formFactory;
71
        $this->flashBag = $flashBag;
72
        $this->translator = $translator;
73
        $this->urlGenerator = $urlGenerator;
74
        $this->twigEnvironment = $twigEnvironment;
75
        $this->wishlistCommandProcessor = $wishlistCommandProcessor;
76
        $this->messageBus = $messageBus;
77
        $this->commandFactory = $commandFactory;
78
    }
79
80
    public function __invoke(Request $request): Response
81
    {
82
        $wishlist = $this->wishlistContext->getWishlist($request);
83
        $cart = $this->cartContext->getCart();
84
85
        $commandsArray = $this->wishlistCommandProcessor->createAddCommandCollectionFromWishlistProducts($wishlist->getWishlistProducts());
86
87
        $form = $this->formFactory->create(
88
            WishlistCollectionType::class,
89
            [
90
                'items' => $commandsArray,
91
            ],
92
            [
93
                'cart' => $cart,
94
            ]
95
        );
96
97
        $form->handleRequest($request);
98
        if ($form->isSubmitted() && $form->isValid()) {
99
            $wishlistProducts = $form->get('items')->getData();
100
            $command = $this->commandFactory->createFrom($wishlistProducts, $request);
101
102
            if (!$this->handle($command)) {
103
                $this->flashBag->add(
104
                    'error',
105
                    $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products')
106
                );
107
            }
108
        }
109
110
        foreach ($form->getErrors() as $error) {
111
            $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

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