Skip to content

Commit 12e96bc

Browse files
chore(poc): retakes
1 parent 53a4143 commit 12e96bc

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/Infrastructure/Adapter/DatexGenerator.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use App\Application\QueryBusInterface;
99
use App\Application\Regulation\DatexGeneratorInterface;
1010
use App\Application\Regulation\Query\GetRegulationOrdersToDatexFormatQuery;
11+
use Symfony\Component\Filesystem\Filesystem;
12+
use Symfony\Component\Filesystem\Path;
1113

1214
final class DatexGenerator implements DatexGeneratorInterface
1315
{
@@ -17,9 +19,10 @@ public function __construct(
1719
private \Twig\Environment $twig,
1820
private DateUtilsInterface $dateUtils,
1921
private QueryBusInterface $queryBus,
22+
private Filesystem $filesystem,
2023
string $projectDir,
2124
) {
22-
$this->datexFilePath = $projectDir . '/var/datex/regulations.xml';
25+
$this->datexFilePath = Path::join($projectDir, '/var/datex/regulations.xml');
2326
}
2427

2528
public function generate(): void
@@ -28,10 +31,10 @@ public function generate(): void
2831
new GetRegulationOrdersToDatexFormatQuery(),
2932
);
3033

31-
$dir = \dirname($this->datexFilePath);
34+
$dir = Path::getDirectory($this->datexFilePath);
3235

33-
if (!is_dir($dir)) {
34-
mkdir($dir, 0o755, true);
36+
if (!$this->filesystem->exists($dir)) {
37+
$this->filesystem->mkdir($dir, 0o755);
3538
}
3639

3740
$tmpFile = $this->datexFilePath . '.tmp';
@@ -50,15 +53,15 @@ public function generate(): void
5053

5154
ob_end_flush();
5255
fclose($handle);
53-
rename($tmpFile, $this->datexFilePath);
56+
$this->filesystem->rename($tmpFile, $this->datexFilePath, overwrite: true);
5457
}
5558

5659
public function getCachedDatex(): string
5760
{
58-
if (!file_exists($this->datexFilePath)) {
61+
if (!$this->filesystem->exists($this->datexFilePath)) {
5962
$this->generate();
6063
}
6164

62-
return file_get_contents($this->datexFilePath);
65+
return $this->filesystem->readFile($this->datexFilePath);
6366
}
6467
}

tests/Unit/Infrastructure/Adapter/DatexGeneratorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Infrastructure\Adapter\DatexGenerator;
1111
use PHPUnit\Framework\MockObject\MockObject;
1212
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Filesystem\Filesystem;
1314

1415
final class DatexGeneratorTest extends TestCase
1516
{
@@ -77,6 +78,7 @@ public function testGenerateCreatesDirectoryAndFile(): void
7778
$this->twig,
7879
$this->dateUtils,
7980
$this->queryBus,
81+
new Filesystem(),
8082
$this->tmpDir,
8183
);
8284

@@ -120,6 +122,7 @@ public function testGenerateOverwritesExistingFile(): void
120122
$this->twig,
121123
$this->dateUtils,
122124
$this->queryBus,
125+
new Filesystem(),
123126
$this->tmpDir,
124127
);
125128

0 commit comments

Comments
 (0)