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
|
|
|
$selectedProducts[] = $this->createCollectionOfWishlistItems($wishlistProduct, $request); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $selectedProducts; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function createCollectionOfWishlistItems( |
65
|
|
|
WishlistItemInterface $wishlistProduct, |
66
|
|
|
Request $request |
67
|
|
|
): VariantPdfModelInterface { |
68
|
|
|
$itemWishlistModel = null; |
69
|
|
|
|
70
|
|
|
if ($wishlistProduct->isSelected()) { |
71
|
|
|
$variant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant()); |
72
|
|
|
$this->wishlistThrowException($wishlistProduct, $variant); |
73
|
|
|
$itemWishlistModel = $this->modelCreator->createWishlistItemToPdf($wishlistProduct, $request, $variant); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (null === $itemWishlistModel) { |
77
|
|
|
throw new ProductVariantNotFoundException( |
78
|
|
|
sprintf('The Product does not selected') |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $itemWishlistModel; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function wishlistThrowException(WishlistItemInterface $wishlistProduct, ProductVariant $variant) |
86
|
|
|
{ |
87
|
|
|
if (null === $variant || null === $wishlistProduct) { |
88
|
|
|
throw new ProductVariantNotFoundException( |
89
|
|
|
sprintf('The Product does not exist') |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function exportToPdf(array $selectedProducts): void |
95
|
|
|
{ |
96
|
|
|
$pdfOptions = new Options(); |
97
|
|
|
$pdfOptions->set('isRemoteEnabled', true); |
98
|
|
|
$pdfOptions->set('defaultFont', 'Arial'); |
99
|
|
|
$dompdf = new Dompdf($pdfOptions); |
100
|
|
|
$html = $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/_wishlist_pdf.html.twig', [ |
101
|
|
|
'title' => 'My wishlist products', |
102
|
|
|
'date' => date('d.m.Y'), |
103
|
|
|
'products' => $selectedProducts, |
104
|
|
|
]); |
105
|
|
|
$dompdf->loadHtml($html); |
106
|
|
|
$dompdf->setPaper('A4', 'portrait'); |
107
|
|
|
$dompdf->render(); |
108
|
|
|
$dompdf->stream('wishlist.pdf', ['Attachment' => true]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|