Passed
Pull Request — master (#87)
by
unknown
03:55
created

ExporterWishlistToPdf::handleCartItems()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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