Skip to content

Commit 19051ee

Browse files
committed
[Maintenance] Add TwigToPdfGenerator
1 parent 25f07eb commit 19051ee

6 files changed

+226
-64
lines changed

spec/Generator/CreditMemoPdfFileGeneratorSpec.php

+51-23
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Sylius\RefundPlugin\Exception\CreditMemoNotFound;
2121
use Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface;
2222
use Sylius\RefundPlugin\Generator\PdfOptionsGeneratorInterface;
23+
use Sylius\RefundPlugin\Generator\TwigToPdfGeneratorInterface;
2324
use Sylius\RefundPlugin\Model\CreditMemoPdf;
2425
use Symfony\Component\Config\FileLocatorInterface;
2526
use Twig\Environment;
@@ -28,19 +29,18 @@ final class CreditMemoPdfFileGeneratorSpec extends ObjectBehavior
2829
{
2930
function let(
3031
RepositoryInterface $creditMemoRepository,
31-
Environment $twig,
32-
GeneratorInterface $pdfGenerator,
3332
FileLocatorInterface $fileLocator,
34-
PdfOptionsGeneratorInterface $pdfOptionsGenerator
33+
TwigToPdfGeneratorInterface $twigToPdfGenerator
3534
): void {
3635
$this->beConstructedWith(
3736
$creditMemoRepository,
38-
$twig,
39-
$pdfGenerator,
37+
null,
38+
null,
4039
$fileLocator,
4140
'creditMemoTemplate.html.twig',
4241
'@SyliusRefundPlugin/Resources/assets/sylius-logo.png',
43-
$pdfOptionsGenerator
42+
null,
43+
$twigToPdfGenerator
4444
);
4545
}
4646

@@ -51,11 +51,9 @@ function it_implements_credit_memo_pdf_file_generator_interface(): void
5151

5252
function it_creates_credit_memo_pdf_with_generated_content_and_filename_basing_on_credit_memo_number(
5353
RepositoryInterface $creditMemoRepository,
54-
Environment $twig,
55-
GeneratorInterface $pdfGenerator,
5654
FileLocatorInterface $fileLocator,
5755
CreditMemoInterface $creditMemo,
58-
PdfOptionsGeneratorInterface $pdfOptionsGenerator
56+
TwigToPdfGeneratorInterface $twigToPdfGenerator
5957
): void {
6058
$creditMemoRepository->find('7903c83a-4c5e-4bcf-81d8-9dc304c6a353')->willReturn($creditMemo);
6159
$creditMemo->getNumber()->willReturn('2015/05/00004444');
@@ -65,18 +63,8 @@ function it_creates_credit_memo_pdf_with_generated_content_and_filename_basing_o
6563
->willReturn('located-path/sylius-logo.png')
6664
;
6765

68-
$twig
69-
->render('creditMemoTemplate.html.twig', ['creditMemo' => $creditMemo, 'creditMemoLogoPath' => 'located-path/sylius-logo.png'])
70-
->willReturn('<html>I am a credit memo pdf file content</html>')
71-
;
72-
73-
$pdfOptionsGenerator
74-
->generate()
75-
->willReturn(['allow' => ['located-path/sylius-logo.png']])
76-
;
77-
78-
$pdfGenerator
79-
->getOutputFromHtml('<html>I am a credit memo pdf file content</html>', ['allow' => ['located-path/sylius-logo.png']])
66+
$twigToPdfGenerator
67+
->generate('creditMemoTemplate.html.twig', ['creditMemo' => $creditMemo, 'creditMemoLogoPath' => 'located-path/sylius-logo.png'])
8068
->willReturn('PDF FILE')
8169
;
8270

@@ -97,10 +85,11 @@ function it_throws_exception_if_credit_memo_with_given_id_has_not_been_found(
9785
;
9886
}
9987

100-
function it_deprecates_not_passing_pdf_options_generator(
88+
function it_deprecates_not_passing_twig_to_pdf_generator(
10189
RepositoryInterface $creditMemoRepository,
10290
Environment $twig,
10391
GeneratorInterface $pdfGenerator,
92+
CreditMemoInterface $creditMemo,
10493
FileLocatorInterface $fileLocator
10594
): void {
10695
$this->beConstructedWith(
@@ -112,6 +101,45 @@ function it_deprecates_not_passing_pdf_options_generator(
112101
'@SyliusRefundPlugin/Resources/assets/sylius-logo.png'
113102
);
114103

115-
$this->shouldTrigger(\E_USER_DEPRECATED, 'Not passing a $pdfOptionsGenerator to Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator constructor is deprecated since sylius/refund-plugin 1.1 and will be prohibited in 2.0.')->duringInstantiation();
104+
$this->shouldTrigger(\E_USER_DEPRECATED, 'Not passing a $twigToPdfGenerator to Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator constructor is deprecated since sylius/refund-plugin 1.2 and will be prohibited in 2.0.')->duringInstantiation();
105+
}
106+
107+
function it_deprecates_passing_pdf_options_generator(
108+
RepositoryInterface $creditMemoRepository,
109+
Environment $twig,
110+
GeneratorInterface $pdfGenerator,
111+
FileLocatorInterface $fileLocator,
112+
PdfOptionsGeneratorInterface $pdfOptionsGenerator
113+
): void {
114+
$this->beConstructedWith(
115+
$creditMemoRepository,
116+
$twig,
117+
$pdfGenerator,
118+
$fileLocator,
119+
'creditMemoTemplate.html.twig',
120+
'@SyliusRefundPlugin/Resources/assets/sylius-logo.png',
121+
$pdfOptionsGenerator
122+
);
123+
124+
$this->shouldTrigger(\E_USER_DEPRECATED, 'Passing Twig\Environment as the second argument to Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator constructor is deprecated since sylius/refund-plugin 1.2 and will be prohibited in 2.0.')->duringInstantiation();
125+
$this->shouldTrigger(\E_USER_DEPRECATED, 'Passing Knp\Snappy\GeneratorInterface as the third argument to Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator constructor is deprecated since sylius/refund-plugin 1.2 and will be prohibited in 2.0.')->duringInstantiation();
126+
$this->shouldTrigger(\E_USER_DEPRECATED, 'Passing Sylius\RefundPlugin\Generator\PdfOptionsGeneratorInterface as the seventh argument to Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator constructor is deprecated since sylius/refund-plugin 1.2 and will be prohibited in 2.0.')->duringInstantiation();
127+
}
128+
129+
function it_prohibits_not_passing_any_generator(
130+
RepositoryInterface $creditMemoRepository,
131+
Environment $twig,
132+
FileLocatorInterface $fileLocator
133+
): void {
134+
$this->beConstructedWith(
135+
$creditMemoRepository,
136+
$twig,
137+
null,
138+
$fileLocator,
139+
'creditMemoTemplate.html.twig',
140+
'@SyliusRefundPlugin/Resources/assets/sylius-logo.png'
141+
);
142+
143+
$this->shouldThrow(\LogicException::class);
116144
}
117145
}
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace spec\Sylius\RefundPlugin\Generator;
15+
16+
use Knp\Snappy\GeneratorInterface;
17+
use PhpSpec\ObjectBehavior;
18+
use Sylius\RefundPlugin\Generator\PdfOptionsGeneratorInterface;
19+
use Sylius\RefundPlugin\Generator\TwigToPdfGeneratorInterface;
20+
use Twig\Environment;
21+
22+
final class TwigToPdfGeneratorSpec extends ObjectBehavior
23+
{
24+
function let(
25+
Environment $twig,
26+
GeneratorInterface $pdfGenerator,
27+
PdfOptionsGeneratorInterface $pdfOptionsGenerator
28+
): void {
29+
$this->beConstructedWith(
30+
$twig,
31+
$pdfGenerator,
32+
$pdfOptionsGenerator
33+
);
34+
}
35+
36+
function it_is_twig_to_pdf_generator_interface(): void
37+
{
38+
$this->shouldImplement(TwigToPdfGeneratorInterface::class);
39+
}
40+
41+
function it_generates_pdf_from_twig_template(
42+
Environment $twig,
43+
GeneratorInterface $pdfGenerator,
44+
PdfOptionsGeneratorInterface $pdfOptionsGenerator
45+
): void {
46+
$twig
47+
->render('template.html.twig', ['figcaption' => 'Swans', 'imgPath' => 'located-path/swans.png'])
48+
->willReturn('<html>I am a pdf file generated from twig template</html>')
49+
;
50+
51+
$pdfOptionsGenerator
52+
->generate()
53+
->willReturn(['allow' => ['allowed_file_in_knp_snappy_config.png', 'located-path/swans.png']])
54+
;
55+
56+
$pdfGenerator
57+
->getOutputFromHtml(
58+
'<html>I am a pdf file generated from twig template</html>',
59+
['allow' => ['allowed_file_in_knp_snappy_config.png', 'located-path/swans.png']]
60+
)
61+
->willReturn('PDF FILE')
62+
;
63+
64+
$this
65+
->generate('template.html.twig', ['figcaption' => 'Swans', 'imgPath' => 'located-path/swans.png'])
66+
->shouldBeLike('PDF FILE')
67+
;
68+
}
69+
}

src/Generator/CreditMemoPdfFileGenerator.php

+42-38
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,16 @@ final class CreditMemoPdfFileGenerator implements CreditMemoPdfFileGeneratorInte
2626
{
2727
private const FILE_EXTENSION = '.pdf';
2828

29-
private RepositoryInterface $creditMemoRepository;
30-
31-
private Environment $twig;
32-
33-
private GeneratorInterface $pdfGenerator;
34-
35-
private FileLocatorInterface $fileLocator;
36-
37-
private string $template;
38-
39-
private string $creditMemoLogoPath;
40-
41-
private ?PdfOptionsGeneratorInterface $pdfOptionsGenerator;
42-
4329
public function __construct(
44-
RepositoryInterface $creditMemoRepository,
45-
Environment $twig,
46-
GeneratorInterface $pdfGenerator,
47-
FileLocatorInterface $fileLocator,
48-
string $template,
49-
string $creditMemoLogoPath,
50-
?PdfOptionsGeneratorInterface $pdfOptionsGenerator = null
30+
private RepositoryInterface $creditMemoRepository,
31+
private ?Environment $twig,
32+
private ?GeneratorInterface $pdfGenerator,
33+
private FileLocatorInterface $fileLocator,
34+
private string $template,
35+
private string $creditMemoLogoPath,
36+
private ?PdfOptionsGeneratorInterface $pdfOptionsGenerator = null,
37+
private ?TwigToPdfGeneratorInterface $twigToPdfGenerator = null
5138
) {
52-
$this->creditMemoRepository = $creditMemoRepository;
53-
$this->twig = $twig;
54-
$this->pdfGenerator = $pdfGenerator;
55-
$this->fileLocator = $fileLocator;
56-
$this->template = $template;
57-
$this->creditMemoLogoPath = $creditMemoLogoPath;
58-
$this->pdfOptionsGenerator = $pdfOptionsGenerator;
59-
6039
$this->checkDeprecations();
6140
}
6241

@@ -74,21 +53,46 @@ public function generate(string $creditMemoId): CreditMemoPdf
7453

7554
$filename = str_replace('/', '_', $number) . self::FILE_EXTENSION;
7655

77-
$pdf = $this->pdfGenerator->getOutputFromHtml(
78-
$this->twig->render($this->template, [
79-
'creditMemo' => $creditMemo,
80-
'creditMemoLogoPath' => $this->fileLocator->locate($this->creditMemoLogoPath),
81-
]),
82-
$this->pdfOptionsGenerator ? $this->pdfOptionsGenerator->generate() : []
83-
);
56+
$pdf = $this->generateFromTemplate([
57+
'creditMemo' => $creditMemo,
58+
'creditMemoLogoPath' => $this->fileLocator->locate($this->creditMemoLogoPath),
59+
]);
8460

8561
return new CreditMemoPdf($filename, $pdf);
8662
}
8763

64+
private function generateFromTemplate(array $templateParams): string
65+
{
66+
if (null !== $this->twigToPdfGenerator) {
67+
return $this->twigToPdfGenerator->generate($this->template, $templateParams);
68+
}
69+
70+
if (null !== $this->pdfGenerator && null !== $this->twig) {
71+
return $this->pdfGenerator->getOutputFromHtml(
72+
$this->twig->render($this->template, $templateParams),
73+
$this->pdfOptionsGenerator ? $this->pdfOptionsGenerator->generate() : []
74+
);
75+
}
76+
77+
throw new \LogicException(sprintf('You must pass at least $twigToPdfGenerator to %s constructor.', self::class));
78+
}
79+
8880
private function checkDeprecations(): void
8981
{
90-
if (null === $this->pdfOptionsGenerator) {
91-
@trigger_error(sprintf('Not passing a $pdfOptionsGenerator to %s constructor is deprecated since sylius/refund-plugin 1.1 and will be prohibited in 2.0.', self::class), \E_USER_DEPRECATED);
82+
if (null !== $this->twig) {
83+
@trigger_error(sprintf('Passing %s as the second argument to %s constructor is deprecated since sylius/refund-plugin 1.2 and will be prohibited in 2.0.', Environment::class, self::class), \E_USER_DEPRECATED);
84+
}
85+
86+
if (null !== $this->pdfGenerator) {
87+
@trigger_error(sprintf('Passing %s as the third argument to %s constructor is deprecated since sylius/refund-plugin 1.2 and will be prohibited in 2.0.', GeneratorInterface::class, self::class), \E_USER_DEPRECATED);
88+
}
89+
90+
if (null !== $this->pdfOptionsGenerator) {
91+
@trigger_error(sprintf('Passing %s as the seventh argument to %s constructor is deprecated since sylius/refund-plugin 1.2 and will be prohibited in 2.0. You should pass $twigToPdfGenerator instead.', PdfOptionsGeneratorInterface::class, self::class), \E_USER_DEPRECATED);
92+
}
93+
94+
if (null === $this->twigToPdfGenerator) {
95+
@trigger_error(sprintf('Not passing a $twigToPdfGenerator to %s constructor is deprecated since sylius/refund-plugin 1.2 and will be prohibited in 2.0.', self::class), \E_USER_DEPRECATED);
9296
}
9397
}
9498
}

src/Generator/TwigToPdfGenerator.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\RefundPlugin\Generator;
15+
16+
use Knp\Snappy\GeneratorInterface;
17+
use Twig\Environment;
18+
19+
final class TwigToPdfGenerator implements TwigToPdfGeneratorInterface
20+
{
21+
public function __construct(
22+
private Environment $twig,
23+
private GeneratorInterface $pdfGenerator,
24+
private PdfOptionsGeneratorInterface $pdfOptionsGenerator
25+
) {
26+
}
27+
28+
public function generate(string $templateName, array $templateParams): string
29+
{
30+
return $this->pdfGenerator->getOutputFromHtml(
31+
$this->twig->render($templateName, $templateParams),
32+
$this->pdfOptionsGenerator->generate()
33+
);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\RefundPlugin\Generator;
15+
16+
interface TwigToPdfGeneratorInterface
17+
{
18+
public function generate(string $templateName, array $templateParams): string;
19+
}

src/Resources/config/services/generators.xml

+10-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131

3232
<service id="Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface" class="Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator">
3333
<argument type="service" id="sylius_refund.repository.credit_memo" />
34-
<argument type="service" id="twig" />
35-
<argument type="service" id="knp_snappy.pdf" />
34+
<argument>null</argument>
35+
<argument>null</argument>
3636
<argument type="service" id="file_locator" />
3737
<argument>@SyliusRefundPlugin/Download/creditMemo.html.twig</argument>
3838
<argument>%sylius.refund.template.logo_file%</argument>
39-
<argument type="service" id="Sylius\RefundPlugin\Generator\PdfOptionsGeneratorInterface" />
39+
<argument>null</argument>
40+
<argument type="service" id="Sylius\RefundPlugin\Generator\TwigToPdfGeneratorInterface" />
4041
</service>
4142

4243
<service id="Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator" alias="Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface">
@@ -55,5 +56,11 @@
5556
<argument>%knp_snappy.pdf.options%</argument>
5657
<argument>%sylius_refund.pdf_generator.allowed_files%</argument>
5758
</service>
59+
60+
<service id="Sylius\RefundPlugin\Generator\TwigToPdfGeneratorInterface" class="Sylius\RefundPlugin\Generator\TwigToPdfGenerator">
61+
<argument type="service" id="twig" />
62+
<argument type="service" id="knp_snappy.pdf" />
63+
<argument type="service" id="Sylius\RefundPlugin\Generator\PdfOptionsGeneratorInterface" />
64+
</service>
5865
</services>
5966
</container>

0 commit comments

Comments
 (0)