Skip to content

Commit 06cb828

Browse files
authored
Merge branch 'main' into chore/fix-deprecated
2 parents cc187b7 + f1be85c commit 06cb828

File tree

25 files changed

+495
-6
lines changed

25 files changed

+495
-6
lines changed

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Application/Integration/EudonetParis/Command/ImportEudonetParisRegulationCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
final class ImportEudonetParisRegulationCommand implements CommandInterface
1313
{
14+
/**
15+
* @param SaveMeasureCommand[] $measureCommands
16+
*/
1417
public function __construct(
1518
public readonly SaveRegulationGeneralInfoCommand $generalInfoCommand,
16-
/** @var SaveMeasureCommand[] */
1719
public readonly array $measureCommands,
1820
) {
1921
$generalInfoCommand->source = RegulationOrderRecordSourceEnum::EUDONET_PARIS->value;

src/Application/Integration/Litteralis/Command/ImportLitteralisRegulationCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
final class ImportLitteralisRegulationCommand implements CommandInterface
1313
{
14+
/** @param SaveMeasureCommand[] $measureCommands */
1415
public function __construct(
1516
public SaveRegulationGeneralInfoCommand $generalInfoCommand,
16-
/** @var SaveMeasureCommand[] */
1717
public readonly array $measureCommands,
1818
public readonly ?string $downloadUrl = null,
1919
) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Application\Regulation\Command;
6+
7+
use App\Application\AsyncCommandInterface;
8+
9+
final class GenerateDatexCommand implements AsyncCommandInterface
10+
{
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Application\Regulation\Command;
6+
7+
use App\Application\Regulation\DatexGeneratorInterface;
8+
9+
final class GenerateDatexCommandHandler
10+
{
11+
public function __construct(
12+
private readonly DatexGeneratorInterface $datexGenerator,
13+
) {
14+
}
15+
16+
public function __invoke(GenerateDatexCommand $command): void
17+
{
18+
$this->datexGenerator->generate();
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Application\Regulation;
6+
7+
interface DatexGeneratorInterface
8+
{
9+
public function generate(): void;
10+
11+
public function getCachedDatex(): string;
12+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Infrastructure\Adapter;
6+
7+
use App\Application\DateUtilsInterface;
8+
use App\Application\QueryBusInterface;
9+
use App\Application\Regulation\DatexGeneratorInterface;
10+
use App\Application\Regulation\Query\GetRegulationOrdersToDatexFormatQuery;
11+
use Symfony\Component\Filesystem\Filesystem;
12+
use Symfony\Component\Filesystem\Path;
13+
14+
final class DatexGenerator implements DatexGeneratorInterface
15+
{
16+
private string $datexFilePath;
17+
18+
public function __construct(
19+
private \Twig\Environment $twig,
20+
private DateUtilsInterface $dateUtils,
21+
private QueryBusInterface $queryBus,
22+
private Filesystem $filesystem,
23+
string $projectDir,
24+
) {
25+
$this->datexFilePath = Path::join($projectDir, '/var/datex/regulations.xml');
26+
}
27+
28+
public function generate(): void
29+
{
30+
$regulationOrders = $this->queryBus->handle(
31+
new GetRegulationOrdersToDatexFormatQuery(),
32+
);
33+
34+
$dir = Path::getDirectory($this->datexFilePath);
35+
36+
if (!$this->filesystem->exists($dir)) {
37+
$this->filesystem->mkdir($dir, 0o755);
38+
}
39+
40+
$tmpFile = $this->datexFilePath . '.tmp';
41+
$handle = fopen($tmpFile, 'w');
42+
43+
ob_start(function (string $buffer) use ($handle): string {
44+
fwrite($handle, $buffer);
45+
46+
return '';
47+
}, 262144);
48+
49+
$this->twig->display('api/regulations.xml.twig', [
50+
'publicationTime' => $this->dateUtils->getNow(),
51+
'regulationOrders' => $regulationOrders,
52+
]);
53+
54+
ob_end_flush();
55+
fclose($handle);
56+
$this->filesystem->rename($tmpFile, $this->datexFilePath, overwrite: true);
57+
}
58+
59+
public function getCachedDatex(): string
60+
{
61+
if (!$this->filesystem->exists($this->datexFilePath)) {
62+
$this->generate();
63+
}
64+
65+
return $this->filesystem->readFile($this->datexFilePath);
66+
}
67+
}

src/Infrastructure/Controller/Api/Regulations/DeleteRegulationController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Application\Organization\Command\UpdateApiClientLastUsedAtCommand;
99
use App\Application\QueryBusInterface;
1010
use App\Application\Regulation\Command\DeleteRegulationCommand;
11+
use App\Application\Regulation\Command\GenerateDatexCommand;
1112
use App\Application\Regulation\Query\GetRegulationOrderRecordByIdentifierQuery;
1213
use App\Domain\Regulation\Exception\RegulationOrderRecordCannotBeDeletedException;
1314
use App\Domain\Regulation\Exception\RegulationOrderRecordNotFoundException;
@@ -103,6 +104,8 @@ public function __invoke(string $identifier): JsonResponse
103104
], Response::HTTP_FORBIDDEN);
104105
}
105106

107+
$this->commandBus->dispatchAsync(new GenerateDatexCommand());
108+
106109
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
107110
}
108111
}

src/Infrastructure/Controller/Api/Regulations/GetRegulationsController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Application\DateUtilsInterface;
88
use App\Application\QueryBusInterface;
9+
use App\Application\Regulation\DatexGeneratorInterface;
910
use App\Application\Regulation\Query\GetRegulationOrdersToDatexFormatQuery;
1011
use OpenApi\Attributes as OA;
1112
use Symfony\Component\HttpFoundation\Response;
@@ -19,6 +20,7 @@ public function __construct(
1920
private \Twig\Environment $twig,
2021
private DateUtilsInterface $dateUtils,
2122
private QueryBusInterface $queryBus,
23+
private DatexGeneratorInterface $datexGenerator,
2224
) {
2325
}
2426

@@ -37,6 +39,18 @@ public function __invoke(
3739
#[MapQueryParameter]
3840
bool $includeExpired = false,
3941
): Response {
42+
$isDefaultParams = $includePermanent && $includeTemporary && !$includeExpired;
43+
44+
if ($isDefaultParams) {
45+
$regulationOrders = $this->datexGenerator->getCachedDatex();
46+
47+
return new Response(
48+
$regulationOrders,
49+
Response::HTTP_OK,
50+
['Content-Type' => 'text/xml; charset=UTF-8'],
51+
);
52+
}
53+
4054
$regulationOrders = $this->queryBus->handle(
4155
new GetRegulationOrdersToDatexFormatQuery(
4256
includePermanent: $includePermanent,

src/Infrastructure/Controller/Api/Regulations/PublishRegulationController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Application\CommandBusInterface;
88
use App\Application\Organization\Command\UpdateApiClientLastUsedAtCommand;
99
use App\Application\QueryBusInterface;
10+
use App\Application\Regulation\Command\GenerateDatexCommand;
1011
use App\Application\Regulation\Command\PublishRegulationCommand;
1112
use App\Application\Regulation\Query\GetRegulationOrderRecordByIdentifierQuery;
1213
use App\Domain\Regulation\Enum\RegulationOrderRecordStatusEnum;
@@ -106,6 +107,8 @@ public function __invoke(string $identifier): JsonResponse
106107
], Response::HTTP_BAD_REQUEST);
107108
}
108109

110+
$this->commandBus->dispatchAsync(new GenerateDatexCommand());
111+
109112
return new JsonResponse([
110113
'identifier' => $regulationOrderRecord->getRegulationOrder()->getIdentifier(),
111114
'status' => RegulationOrderRecordStatusEnum::PUBLISHED->value,

0 commit comments

Comments
 (0)