Passed
Pull Request — master (#93)
by
unknown
03:26
created

ExporterWishlistToPdf::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 6
dl 0
loc 14
rs 10
c 1
b 1
f 0
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\Services\Exporter;
12
13
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProductInterface;
14
use BitBag\SyliusWishlistPlugin\Model\Factory\VariantPdfModelFactoryInterface;
15
use BitBag\SyliusWishlistPlugin\Resolver\VariantImagePathResolverInterface;
16
use Doctrine\Common\Collections\Collection;
17
use Dompdf\Dompdf;
18
use Dompdf\Options;
19
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
22
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23
use Symfony\Contracts\Translation\TranslatorInterface;
24
use Twig\Environment;
25
26
final class ExporterWishlistToPdf implements ExporterWishlistToPdfInterface
27
{
28
    private bool $isSelected = false;
29
30
    private ProductVariantRepositoryInterface $productVariantRepository;
31
32
    private VariantImagePathResolverInterface $variantImagePathResolver;
33
34
    private VariantPdfModelFactoryInterface $variantPdfModelFactory;
35
36
    private Environment $twigEnvironment;
37
38
    private FlashBagInterface $flashBag;
39
40
    private TranslatorInterface $translator;
41
42
43
    public function __construct(
44
        ProductVariantRepositoryInterface $productVariantRepository,
45
        VariantImagePathResolverInterface $variantImagePathResolver,
46
        VariantPdfModelFactoryInterface $variantPdfModelFactory,
47
        Environment $twigEnvironment,
48
        FlashBagInterface $flashBag,
49
        TranslatorInterface $translator
50
    ) {
51
        $this->productVariantRepository = $productVariantRepository;
52
        $this->variantImagePathResolver = $variantImagePathResolver;
53
        $this->variantPdfModelFactory = $variantPdfModelFactory;
54
        $this->twigEnvironment = $twigEnvironment;
55
        $this->flashBag = $flashBag;
56
        $this->translator = $translator;
57
    }
58
59
    public function createModelToPdfAndExportToPdf(Collection $wishlistProducts, Request $request): void
60
    {
61
        $selectedProducts = [];
62
63
        /** @var AddWishlistProductInterface $wishlistProduct */
64
        foreach ($wishlistProducts as $wishlistProduct) {
65
            if ($wishlistProduct->isSelected()) {
66
                $this->isSelected = true;
67
68
                $variant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant());
69
70
                if (null === $variant) {
71
                    throw new NotFoundHttpException();
72
                }
73
74
                $cartItem = $wishlistProduct->getCartItem()->getCartItem();
75
                $quantity = $cartItem->getQuantity();
76
                $baseUrl = $request->getSchemeAndHttpHost();
77
                $urlToImage = $this->variantImagePathResolver->resolve($variant, $baseUrl);
78
                $actualVariant = $cartItem->getVariant()->getCode();
79
                $selectedProducts[] = $this->variantPdfModelFactory->createWithVariantAndImagePath(
80
                    $variant,
81
                    $urlToImage,
82
                    $quantity,
83
                    $actualVariant
84
                );
85
            }
86
        }
87
        $this->exportToPdf($selectedProducts);
88
    }
89
90
    private function exportToPdf(array $selectedProducts): void
91
    {
92
        if ( $this->isSelected === false) {
93
            $this->flashBag->add(
94
                'error',
95
                $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products')
96
            );
97
            return ;
98
        }
99
100
        $pdfOptions = new Options();
101
        $pdfOptions->set('isRemoteEnabled', true);
102
        $pdfOptions->set('defaultFont', 'Arial');
103
        $dompdf = new Dompdf($pdfOptions);
104
        $html = $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/_wishlist_pdf.html.twig', [
105
            'title' => 'My wishlist products',
106
            'date' => date('d.m.Y'),
107
            'products' => $selectedProducts,
108
        ]);
109
        $dompdf->loadHtml($html);
110
        $dompdf->setPaper('A4', 'portrait');
111
        $dompdf->render();
112
        $dompdf->stream('wishlist.pdf', ['Attachment' => true]);
113
    }
114
115
}