Passed
Pull Request — master (#87)
by
unknown
05:18
created

ExporterWishlistToPdf::exportToPdf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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