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

ExportWishlistToPdfAction   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 80
c 2
b 0
f 0
dl 0
loc 149
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A ExportToPdf() 0 17 1
A handleCartItems() 0 40 4
B __invoke() 0 40 6
1
<?php
2
declare(strict_types=1);
3
4
namespace BitBag\SyliusWishlistPlugin\Controller\Action;
5
6
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct;
7
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
8
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Dompdf\Dompdf;
11
use Dompdf\Options;
12
use Sylius\Component\Channel\Context\ChannelContextInterface;
13
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
14
use Sylius\Component\Order\Context\CartContextInterface;
15
use Symfony\Component\Form\FormFactoryInterface;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
use Symfony\Contracts\Translation\TranslatorInterface;
23
use Twig\Environment;
24
25
final class ExportWishlistToPdfAction
26
{
27
    private WishlistContextInterface $wishlistContext;
28
29
    private CartContextInterface $cartContext;
30
31
    private FormFactoryInterface $formFactory;
32
33
    private FlashBagInterface $flashBag;
34
35
    private TranslatorInterface $translator;
36
37
    private ProductVariantRepositoryInterface $productVariantRepository;
38
39
    private UrlGeneratorInterface $urlGenerator;
40
41
    private EntityManagerInterface $wishlistProductManager;
42
43
    private Environment $twigEnvironment;
44
45
    private ChannelContextInterface $channelContext;
46
47
    public function __construct(
48
        WishlistContextInterface          $wishlistContext,
49
        CartContextInterface              $cartContext,
50
        FormFactoryInterface              $formFactory,
51
        FlashBagInterface                 $flashBag,
52
        TranslatorInterface               $translator,
53
        ProductVariantRepositoryInterface $productVariantRepository,
54
        UrlGeneratorInterface             $urlGenerator,
55
        EntityManagerInterface            $wishlistProductManager,
56
        Environment                       $twigEnvironment,
57
        ChannelContextInterface           $channelContext
58
    ) {
59
        $this->wishlistContext = $wishlistContext;
60
        $this->cartContext = $cartContext;
61
        $this->formFactory = $formFactory;
62
        $this->flashBag = $flashBag;
63
        $this->translator = $translator;
64
        $this->productVariantRepository = $productVariantRepository;
65
        $this->urlGenerator = $urlGenerator;
66
        $this->wishlistProductManager = $wishlistProductManager;
67
        $this->twigEnvironment = $twigEnvironment;
68
        $this->channelContext = $channelContext;
69
    }
70
71
    public function __invoke(Request $request): Response
72
    {
73
        $wishlist = $this->wishlistContext->getWishlist($request);
74
        $cart = $this->cartContext->getCart();
75
76
        $commandsArray = [];
77
78
        foreach ($wishlist->getWishlistProducts() as $wishlistProductItem) {
79
            $wishlistProductCommand = new AddWishlistProduct();
80
            $wishlistProductCommand->setWishlistProduct($wishlistProductItem);
81
            $commandsArray[] = $wishlistProductCommand;
82
        }
83
84
        $form = $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
85
            'cart' => $cart,
86
87
        ]);
88
89
        $form->handleRequest($request);
90
91
        if ($form->isSubmitted() && $form->isValid()) {
92
            $wishlistProducts = $form->get("items")->getData();
93
94
            if ($this->handleCartItems($wishlistProducts, $request)) {
95
                $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.removed_selected_wishlist_items'));
96
            } else {
97
                $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
98
            }
99
100
            return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
101
        }
102
103
        foreach ($form->getErrors() as $error) {
104
            $this->flashBag->add('error', $error->getMessage());
105
        }
106
107
        return new Response(
108
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
109
                'wishlist' => $wishlist,
110
                'form' => $form->createView(),
111
            ])
112
        );
113
    }
114
115
    private function handleCartItems(array $wishlistProducts, Request $request): bool
116
    {
117
        $result = false;
118
        $selectedProducts = [];
119
        /** @var AddWishlistProduct $wishlistProduct */
120
        foreach ($wishlistProducts as $wishlistProduct) {
121
            if ($wishlistProduct->isSelected()) {
122
                $result = true;
123
                $variant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant());
124
125
                if (null === $variant) {
126
                    throw new NotFoundHttpException();
127
                }
128
129
                $cartItem = $wishlistProduct->getCartItem()->getCartItem();
130
                $actualVariant = $wishlistProduct->getWishlistProduct()->getVariant();
131
                $actualProduct = $actualVariant->getProduct();
132
133
                $productName = $actualProduct->getName();
134
                $productCode = $actualProduct->getCode();
135
                $productImage = $actualProduct->getImages();
136
                $productVariant = $actualVariant->getName();
137
                $productPrice = $actualVariant->getChannelPricingForChannel($this->channelContext->getChannel())->getPrice();
138
                $productQuantity = $cartItem->getQuantity();
139
140
                $selectedProducts[] =
141
                    [
142
                        'productName' => $productName,
143
                        'productCode' => $productCode,
144
//                        'productImage' => $productImage,
145
                        'productVariant' => $productVariant,
146
                        'productQuantity' => $productQuantity,
147
                        'productPrice' => $productPrice
148
                    ];
149
            }
150
        }
151
152
        $this->ExportToPdf($selectedProducts);
153
154
        return $result;
155
    }
156
157
    public function ExportToPdf(array $selectedProducts)
158
    {
159
        $pdfOptions = new Options();
160
        $pdfOptions->set('defaultFont', 'Arial');
161
162
        $dompdf = new Dompdf($pdfOptions);
163
164
        $html = $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/_wishlist_pdf.html.twig', [
165
            'title' => "Welcome to our PDF Test",
166
            'products' => $selectedProducts
167
        ]);
168
169
        $dompdf->loadHtml($html);
170
        $dompdf->setPaper('A4', 'portrait');
171
        $dompdf->render();
172
        $dompdf->stream("wishlist.pdf", [
173
            "Attachment" => true
174
        ]);
175
    }
176
}
177