Passed
Pull Request — master (#93)
by
unknown
05:21
created

WishlistToPdfExporter::exportToPdf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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