1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusInvoicingPlugin\FileGenerator; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusInvoicingPlugin\Entity\InvoiceInterface; |
16
|
|
|
use BitBag\SyliusInvoicingPlugin\Resolver\CompanyDataResolverInterface; |
17
|
|
|
use Knp\Snappy\GeneratorInterface; |
18
|
|
|
use Symfony\Component\Templating\EngineInterface; |
19
|
|
|
|
20
|
|
|
final class InvoicePdfFileGenerator implements FileGeneratorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var GeneratorInterface |
24
|
|
|
*/ |
25
|
|
|
private $pdfFileGenerator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EngineInterface |
29
|
|
|
*/ |
30
|
|
|
private $templatingEngine; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var CompanyDataResolverInterface |
34
|
|
|
*/ |
35
|
|
|
private $companyDataResolver; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $filesPath; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param GeneratorInterface $pdfFileGenerator |
44
|
|
|
* @param EngineInterface $templatingEngine |
45
|
|
|
* @param CompanyDataResolverInterface $companyDataResolver |
46
|
|
|
* @param string $filesPath |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
GeneratorInterface $pdfFileGenerator, |
50
|
|
|
EngineInterface $templatingEngine, |
51
|
|
|
CompanyDataResolverInterface $companyDataResolver, |
52
|
|
|
string $filesPath |
53
|
|
|
) { |
54
|
|
|
$this->pdfFileGenerator = $pdfFileGenerator; |
55
|
|
|
$this->templatingEngine = $templatingEngine; |
56
|
|
|
$this->companyDataResolver = $companyDataResolver; |
57
|
|
|
$this->filesPath = $filesPath; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function generateFile(InvoiceInterface $invoice): string |
64
|
|
|
{ |
65
|
|
|
$html = $this->templatingEngine->render( |
66
|
|
|
'BitBagSyliusInvoicingPlugin::invoice.html.twig', [ |
67
|
|
|
'invoice' => $invoice, |
68
|
|
|
'companyData' => $this->companyDataResolver->resolveCompanyData(), |
69
|
|
|
] |
70
|
|
|
); |
71
|
|
|
$filename = $this->getInvoiceFilename($invoice); |
72
|
|
|
$path = $this->filesPath . DIRECTORY_SEPARATOR . $filename; |
73
|
|
|
|
74
|
|
|
$this->pdfFileGenerator->generateFromHtml($html, $path); |
75
|
|
|
|
76
|
|
|
return $filename; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param InvoiceInterface $invoice |
81
|
|
|
* @return string Returns an explicit invoice file name |
82
|
|
|
*/ |
83
|
|
|
protected function getInvoiceFilename(InvoiceInterface $invoice): string |
84
|
|
|
{ |
85
|
|
|
$tokens = [ |
86
|
|
|
$invoice->getOrder()->getNumber(), |
87
|
|
|
$invoice->getOrder()->getCreatedAt()->format('Ymd'), |
88
|
|
|
bin2hex(random_bytes(6)), |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
return (string) implode('_', $tokens) . '.pdf'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function getFilesPath(): string |
98
|
|
|
{ |
99
|
|
|
return $this->filesPath; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|