Passed
Pull Request — master (#93)
by
unknown
04:06 queued 10s
created

WishlistToPdfExporter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
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\Services\Generator\ModelCreator;
17
use Doctrine\Common\Collections\Collection;
18
use Dompdf\Dompdf;
19
use Dompdf\Options;
20
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
use Twig\Environment;
23
24
final class WishlistToPdfExporter implements WishlistToPdfExporterInterface
25
{
26
    private ProductVariantRepositoryInterface $productVariantRepository;
27
28
    private Environment $twigEnvironment;
29
30
    private ModelCreator $modelCreator;
31
32
    public function __construct(
33
        ProductVariantRepositoryInterface $productVariantRepository,
34
        Environment $twigEnvironment,
35
        ModelCreator $modelCreator
36
    ) {
37
        $this->productVariantRepository = $productVariantRepository;
38
        $this->twigEnvironment = $twigEnvironment;
39
        $this->modelCreator = $modelCreator;
40
    }
41
42
    public function createModelToPdfAndExportToPdf(Collection $wishlistProducts, Request $request): void
43
    {
44
        $productsToExport = $this->createVariantModelToPdf($wishlistProducts, $request);
45
46
        if (empty($productsToExport)) {
47
            throw new NoProductSelectedException();
48
        }
49
        $this->exportToPdf($productsToExport);
50
    }
51
52
    private function createVariantModelToPdf(Collection $wishlistProducts, Request $request): array
53
    {
54
        $selectedProducts = [];
55
56
        /** @var WishlistItemInterface $wishlistProduct */
57
        foreach ($wishlistProducts as $wishlistProduct) {
58
            if ($wishlistProduct->isSelected()) {
59
                $variant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant());
60
61
                if (null === $variant || null === $wishlistProduct) {
62
                    throw new ProductVariantNotFoundException(
63
                        sprintf('The Product does not exist')
64
                    );
65
                }
66
67
                $itemWishlistModel = $this->modelCreator->createWishlistItemToPdf(@$wishlistProduct, $request, $variant);
68
                $selectedProducts[] = $itemWishlistModel;
69
            }
70
        }
71
72
        return $selectedProducts;
73
    }
74
75
    private function exportToPdf(array $selectedProducts): void
76
    {
77
        $pdfOptions = new Options();
78
        $pdfOptions->set('isRemoteEnabled', true);
79
        $pdfOptions->set('defaultFont', 'Arial');
80
        $dompdf = new Dompdf($pdfOptions);
81
        $html = $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/_wishlist_pdf.html.twig', [
82
            'title' => 'My wishlist products',
83
            'date' => date('d.m.Y'),
84
            'products' => $selectedProducts,
85
        ]);
86
        $dompdf->loadHtml($html);
87
        $dompdf->setPaper('A4', 'portrait');
88
        $dompdf->render();
89
        $dompdf->stream('wishlist.pdf', ['Attachment' => true]);
90
    }
91
}
92