WishlistToPdfExporter::createVariantModelToPdf()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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