Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
48 / 48 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| PdfHelper | |
100.00% |
48 / 48 |
|
100.00% |
9 / 9 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRoot | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| renderTransactionHtml | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
2 | |||
| renderPayrollsHtml | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getOutputFromHtml | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| renderPayrollPdf | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| renderTransactionsPdf | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| getHeaderHtml | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getFooterHtml | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Service; |
| 6 | |
| 7 | use App\Entity\Store; |
| 8 | use App\Entity\Transaction; |
| 9 | use App\Repository\TransactionRepository; |
| 10 | use Knp\Snappy\Pdf; |
| 11 | use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 12 | use Twig\Environment; |
| 13 | |
| 14 | readonly class PdfHelper |
| 15 | { |
| 16 | public function __construct( |
| 17 | #[Autowire('%kernel.project_dir%')] |
| 18 | private string $rootDir, |
| 19 | private Environment $twig, |
| 20 | private Pdf $pdfEngine, |
| 21 | ) {} |
| 22 | |
| 23 | public function getRoot(): string |
| 24 | { |
| 25 | return $this->rootDir; |
| 26 | } |
| 27 | |
| 28 | public function renderTransactionHtml( |
| 29 | TransactionRepository $transactionRepository, |
| 30 | Store $store, |
| 31 | int $year, |
| 32 | int $transactionsPerPage = 42 |
| 33 | ): string |
| 34 | { |
| 35 | $transactions = $transactionRepository->findByStoreAndYear( |
| 36 | $store, |
| 37 | $year |
| 38 | ); |
| 39 | |
| 40 | $pages = (int)(count($transactions) / $transactionsPerPage) + 1; |
| 41 | $fillers = $transactionsPerPage - (count($transactions) - ($pages - 1) |
| 42 | * $transactionsPerPage); |
| 43 | |
| 44 | for ($i = 1; $i < $fillers; ++$i) { |
| 45 | $transaction = new Transaction(); |
| 46 | $transactions[] = $transaction; |
| 47 | } |
| 48 | |
| 49 | return $this->twig->render( |
| 50 | '_pdf/transactions-pdf.html.twig', |
| 51 | [ |
| 52 | 'saldoAnterior' => $transactionRepository->getSaldoAnterior( |
| 53 | $store, |
| 54 | $year |
| 55 | ), |
| 56 | 'transactions' => $transactions, |
| 57 | 'store' => $store, |
| 58 | 'year' => $year, |
| 59 | ] |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | public function renderPayrollsHtml( |
| 64 | int $year, |
| 65 | int $month, |
| 66 | PayrollHelper $payrollHelper, |
| 67 | int $storeId = 0 |
| 68 | ): string |
| 69 | { |
| 70 | return $this->twig->render( |
| 71 | '_pdf/payrolls-pdf.html.twig', |
| 72 | $payrollHelper->getData($year, $month, $storeId) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param array<string>|string $htmlPages |
| 78 | * @param array<string, string|bool> $options |
| 79 | */ |
| 80 | public function getOutputFromHtml( |
| 81 | array|string $htmlPages, |
| 82 | array $options = [] |
| 83 | ): string |
| 84 | { |
| 85 | return $this->pdfEngine->getOutputFromHtml($htmlPages, $options); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Render payroll HTML to PDF bytes with local-file-access enabled. |
| 90 | */ |
| 91 | public function renderPayrollPdf(int $year, int $month, PayrollHelper $payrollHelper, int $storeId = 0): string |
| 92 | { |
| 93 | return $this->getOutputFromHtml( |
| 94 | $this->renderPayrollsHtml($year, $month, $payrollHelper, $storeId), |
| 95 | ['enable-local-file-access' => true] |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Render transaction HTML pages to PDF bytes with headers/footers and local-file-access enabled. |
| 101 | * |
| 102 | * @param array<string>|string $htmlPages |
| 103 | */ |
| 104 | public function renderTransactionsPdf(array|string $htmlPages): string |
| 105 | { |
| 106 | return $this->getOutputFromHtml( |
| 107 | $htmlPages, |
| 108 | [ |
| 109 | 'header-html' => $this->getHeaderHtml(), |
| 110 | 'footer-html' => $this->getFooterHtml(), |
| 111 | 'enable-local-file-access' => true, |
| 112 | ] |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | public function getHeaderHtml(): string |
| 117 | { |
| 118 | return $this->twig->render( |
| 119 | '_header-pdf.html.twig', |
| 120 | [ |
| 121 | 'rootPath' => $this->rootDir.'/public', |
| 122 | ] |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | public function getFooterHtml(): string |
| 127 | { |
| 128 | return $this->twig->render('_footer-pdf.html.twig'); |
| 129 | } |
| 130 | } |