Skip to content

Commit 247fc7e

Browse files
committed
refactor(wkhtmltopdf): remove Orientation extra option (already managed by Snappy options)
1 parent d4c41ec commit 247fc7e

File tree

5 files changed

+47
-93
lines changed

5 files changed

+47
-93
lines changed

src/Backend/WkHtmlToPdf/ExtraOption.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,5 @@ abstract class ExtraOption
1212
/**
1313
* @param non-empty-array<string> $command
1414
*/
15-
public function __construct(private readonly bool $repeatable, private readonly array $command) {}
16-
17-
final public function isRepeatable(): bool
18-
{
19-
return $this->repeatable;
20-
}
21-
22-
/**
23-
* @return non-empty-array<string>
24-
*/
25-
final public function getCommand(): array
26-
{
27-
return $this->command;
28-
}
15+
public function __construct(public readonly bool $repeatable, public readonly array $command) {}
2916
}

src/Backend/WkHtmlToPdf/ExtraOption/Orientation.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Backend/WkHtmlToPdf/Tests/ExtraOptionTest.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\Tests;
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
8-
use KNPLabs\Snappy\Core\Backend\Options\PageOrientation;
98
use PHPUnit\Framework\Attributes\CoversNothing;
109
use PHPUnit\Framework\Attributes\DataProvider;
1110
use PHPUnit\Framework\TestCase;
@@ -142,16 +141,6 @@ public static function nonRepeatableProvider(): iterable
142141
['--margin-top', '10mm'],
143142
];
144143

145-
yield [
146-
new ExtraOption\Orientation(PageOrientation::LANDSCAPE),
147-
['--orientation', 'Landscape'],
148-
];
149-
150-
yield [
151-
new ExtraOption\Orientation(PageOrientation::PORTRAIT),
152-
['--orientation', 'Portrait'],
153-
];
154-
155144
yield [
156145
new ExtraOption\PageHeight('297mm'),
157146
['--page-height', '297mm'],
@@ -494,8 +483,8 @@ public static function nonRepeatableProvider(): iterable
494483
#[DataProvider('repeatableProvider')]
495484
public function testRepeatableOption(ExtraOption $option, array $command): void
496485
{
497-
self::assertTrue($option->isRepeatable());
498-
self::assertSame($option->getCommand(), $command);
486+
self::assertTrue($option->repeatable);
487+
self::assertSame($option->command, $command);
499488
}
500489

501490
/**
@@ -504,7 +493,7 @@ public function testRepeatableOption(ExtraOption $option, array $command): void
504493
#[DataProvider('nonRepeatableProvider')]
505494
public function testNonRepeatableOption(ExtraOption $option, array $command): void
506495
{
507-
self::assertFalse($option->isRepeatable());
508-
self::assertSame($option->getCommand(), $command);
496+
self::assertFalse($option->repeatable);
497+
self::assertSame($option->command, $command);
509498
}
510499
}

src/Backend/WkHtmlToPdf/Tests/WkHtmlToPdfAdapterTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ final class WkHtmlToPdfAdapterTest extends TestCase
2626

2727
private WkHtmlToPdfAdapter $wkHtmlToPdfAdapter;
2828

29+
/**
30+
* @var MockObject&StreamFactoryInterface
31+
*/
2932
private MockObject $streamFactory;
3033

34+
/**
35+
* @var MockObject&UriFactoryInterface
36+
*/
3137
private MockObject $uriFactory;
3238

3339
private string $tempDir;
@@ -75,8 +81,6 @@ public function testGenerateFromHtmlFile(): void
7581
self::fail('Erreur lors de la génération du PDF : '.$exception->getMessage());
7682
}
7783

78-
self::assertNotNull($resultStream);
79-
self::assertInstanceOf(StreamInterface::class, $resultStream);
8084
self::assertNotEmpty($resultStream->getContents());
8185

8286
unlink($testFilePath);
@@ -112,10 +116,14 @@ public function testGenerateWithAdditionalOptions(): void
112116

113117
$this->wkHtmlToPdfAdapter = $this->factory->create($options);
114118

119+
$realpath = realpath($testFilePath);
120+
121+
self::assertIsString($realpath);
122+
115123
$this->uriFactory
116124
->method('createUri')
117125
->with($testFilePath)
118-
->willReturn(new Uri(realpath($testFilePath)))
126+
->willReturn(new Uri($realpath))
119127
;
120128

121129
$stream = $this->createMock(StreamInterface::class);
@@ -128,8 +136,6 @@ public function testGenerateWithAdditionalOptions(): void
128136

129137
$resultStream = $this->wkHtmlToPdfAdapter->generateFromHtmlFile(new \SplFileInfo($testFilePath));
130138

131-
self::assertNotNull($resultStream);
132-
self::assertInstanceOf(StreamInterface::class, $resultStream);
133139
self::assertNotEmpty($resultStream->getContents());
134140

135141
unlink($testFilePath);

src/Backend/WkHtmlToPdf/WkHtmlToPdfAdapter.php

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public function __construct(
3636
private readonly StreamFactoryInterface $streamFactory,
3737
private readonly UriFactoryInterface $uriFactory,
3838
) {
39-
$this->validateOptions($options);
40-
4139
$this->factory = $factory;
4240
$this->options = $options;
41+
42+
$this->compileOptions();
4343
}
4444

4545
public function generateFromHtmlFile(\SplFileInfo $file): StreamInterface
@@ -81,51 +81,54 @@ public function generateFromUri(UriInterface $uri): StreamInterface
8181
return $this->streamFactory->createStreamFromResource($outputFile->resource);
8282
}
8383

84-
private function validateOptions(Options $options): void
84+
/**
85+
* @return array<string>
86+
*/
87+
private function compileOptions(): array
8588
{
89+
$options = [];
90+
91+
if ($this->options->pageOrientation instanceof PageOrientation) {
92+
$options = [
93+
...$options,
94+
'--orientation',
95+
match ($this->options->pageOrientation) {
96+
PageOrientation::Portrait => 'Portrait',
97+
PageOrientation::Landscape => 'Landscape',
98+
},
99+
];
100+
}
101+
86102
$optionTypes = [];
87103

88-
foreach ($options->extraOptions as $option) {
89-
if (!$option instanceof ExtraOption) {
104+
foreach ($this->options->extraOptions as $extraOption) {
105+
if (!$extraOption instanceof ExtraOption) {
90106
throw new \InvalidArgumentException(
91107
\sprintf(
92108
'Invalid option type provided. Expected "%s", received "%s".',
93109
ExtraOption::class,
94-
get_debug_type($option),
110+
get_debug_type($extraOption),
95111
)
96112
);
97113
}
98114

99-
if (\in_array($option::class, $optionTypes, true) && !$option->isRepeatable()) {
115+
if ($extraOption->repeatable && \in_array($extraOption::class, $optionTypes, true)) {
100116
throw new \InvalidArgumentException(
101117
\sprintf(
102118
'Duplicate option type provided: "%s".',
103-
$option::class,
119+
$extraOption::class,
104120
)
105121
);
106122
}
107123

108-
$optionTypes[] = $option::class;
124+
$options = [
125+
...$options,
126+
...$extraOption->command,
127+
];
128+
129+
$optionTypes[] = $extraOption::class;
109130
}
110-
}
111131

112-
/**
113-
* @return array<float|int|string>
114-
*/
115-
private function compileOptions(): array
116-
{
117-
return array_reduce(
118-
$this->options->extraOptions,
119-
fn (array $carry, ExtraOption $extraOption): array => $extraOption instanceof ExtraOption\Orientation && $this->options->pageOrientation instanceof PageOrientation
120-
? [
121-
...$carry,
122-
...(new ExtraOption\Orientation($this->options->pageOrientation))->getCommand(),
123-
]
124-
: [
125-
...$carry,
126-
...$extraOption->getCommand(),
127-
],
128-
[],
129-
);
132+
return $options;
130133
}
131134
}

0 commit comments

Comments
 (0)