Skip to content

Commit

Permalink
Export logo
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchois committed Nov 27, 2024
1 parent f2cc008 commit 18e2295
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 25 deletions.
Binary file modified data/regulation-order-template.docx
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __invoke(GetGeneralInfoQuery $query): GeneralInfoView
uuid: $row['uuid'],
identifier: $row['identifier'],
organizationName: $row['organizationName'],
organizationLogo: $row['organizationLogo'],
organizationUuid: $row['organizationUuid'],
status: $row['status'],
regulationOrderUuid: $row['regulationOrderUuid'],
Expand Down
1 change: 1 addition & 0 deletions src/Application/Regulation/View/GeneralInfoView.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function __construct(
public readonly string $uuid,
public readonly string $identifier,
public readonly string $organizationName,
public readonly ?string $organizationLogo,
public readonly ?string $organizationUuid,
public readonly string $status,
public readonly string $regulationOrderUuid,
Expand Down
6 changes: 5 additions & 1 deletion src/Application/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ public function write(string $folder, UploadedFile $file): string;

public function delete(string $path): void;

public function get(string $path): string;
public function getUrl(string $path): string;

public function read(string $path): ?string;

public function getMimeType(string $path): ?string;
}
26 changes: 22 additions & 4 deletions src/Infrastructure/Adapter/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use League\Flysystem\FilesystemOperator;
use Symfony\Component\HttpFoundation\File\UploadedFile;

final class Storage implements StorageInterface
final readonly class Storage implements StorageInterface
{
public function __construct(
private readonly FilesystemOperator $storage,
private readonly string $mediaLocation,
private FilesystemOperator $storage,
private string $mediaLocation,
) {
}

Expand All @@ -36,8 +36,26 @@ public function delete(string $path): void
$this->storage->delete($path);
}

public function get(string $path): string
public function getUrl(string $path): string
{
return \sprintf('%s/%s', $this->mediaLocation, $path);
}

public function read(string $path): ?string
{
if (!$this->storage->fileExists($path)) {
return null;
}

return base64_encode($this->storage->read($path));
}

public function getMimeType(string $path): ?string
{
if (!$this->storage->fileExists($path)) {
return null;
}

return $this->storage->mimeType($path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __invoke(Request $request, string $uuid): Response
);
}

$logo = $organization->getLogo() ? $this->storage->get($organization->getLogo()) : null;
$logo = $organization->getLogo() ? $this->storage->getUrl($organization->getLogo()) : null;

return new Response(
content: $this->twig->render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
public function __invoke(string $uuid): Response
{
$organization = $this->getOrganization($uuid);
$logo = $organization->getLogo() ? $this->storage->get($organization->getLogo()) : null;
$logo = $organization->getLogo() ? $this->storage->getUrl($organization->getLogo()) : null;

return new Response(
content: $this->twig->render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Application\Regulation\Query\Measure\GetMeasuresQuery;
use App\Application\Regulation\Query\Visa\GetVisasAndReasonsByRegulationOrderQuery;
use App\Application\Regulation\View\GeneralInfoView;
use App\Application\StorageInterface;
use App\Domain\Regulation\Specification\CanOrganizationAccessToRegulation;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\HeaderUtils;
Expand All @@ -24,6 +25,7 @@ public function __construct(
CanOrganizationAccessToRegulation $canOrganizationAccessToRegulation,
Security $security,
private readonly \Twig\Environment $twig,
private readonly StorageInterface $storage,
private readonly string $projectDir,
) {
parent::__construct($queryBus, $security, $canOrganizationAccessToRegulation);
Expand All @@ -46,13 +48,24 @@ public function __invoke(string $uuid): Response
$visasAndReasons = $this->queryBus->handle(new GetVisasAndReasonsByRegulationOrderQuery($generalInfo->regulationOrderUuid));
$signingAuthority = $this->queryBus->handle(new GetSigningAuthorityByOrganizationQuery($generalInfo->organizationUuid));
$measures = $this->queryBus->handle(new GetMeasuresQuery($uuid));

$logo = null;
$logoMimeType = null;

if ($path = $generalInfo->organizationLogo) {
$logo = $this->storage->read($path);
$logoMimeType = $this->storage->getMimeType($path);
}

$content = $this->twig->render(
name: 'regulation/export.md.twig',
context: [
'visasAndReasons' => $visasAndReasons,
'generalInfo' => $generalInfo,
'measures' => $measures,
'signingAuthority' => $signingAuthority,
'logo' => $logo,
'logoMimeType' => $logoMimeType,
],
);

Expand All @@ -61,7 +74,6 @@ public function __invoke(string $uuid): Response
->from('markdown')
->input($content)
->option('reference-doc', $this->projectDir . '/data/regulation-order-template.docx')
->option('resource-path', $this->projectDir . '/public/images')
->to('docx')
->run(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public function findGeneralInformation(string $uuid): ?array
ro.identifier,
org.name as organizationName,
org.uuid as organizationUuid,
org.logo as organizationLogo,
roc.status,
ro.uuid as regulationOrderUuid,
ro.category,
Expand Down
19 changes: 3 additions & 16 deletions templates/regulation/export.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,10 @@
[{{ signingAuthority.address|raw|nl2br }}]{custom-style="Dialog_ACompleter"}
:::
{% endif %}
<br/>

::: {custom-style="Dialog_Encart"}
[Information importante :]{custom-style="Dialog_GrasSouligne"}

Ce document est un [modèle à compléter et à modifier]{custom-style="Dialog_Gras"}
par vos soins. Il constitue simplement une aide à la rédaction de l'arrêté, mais
ne garantit pas l'exactitude des informations. Il vous appartient de vérifier que
toutes les informations sont correctes.

• Les informations en [bleu]{custom-style="Dialog_VariableGras"} sont issues du formulaire, assurez-vous qu'elles soient toujours en cohérence avec les informations renseignées dans DiaLog.

• Les informations en [italique]{custom-style="Dialog_ACompleter"} sont à compléter.

Vous pouvez supprimer cet encart une fois votre arrêté complété.
:::

{% if logo %}
![](data:{{ logoMimeType }};base64,{{ logo }}){ height=100px }
{% endif %}
<br/>

::: {custom-style="Dialog_TitrePrincipal"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testGetGeneralInformation(): void
uuid: '3d1c6ec7-28f5-4b6b-be71-b0920e85b4bf',
identifier: 'FO1/2024',
organizationName: 'DiaLog',
organizationLogo: '/path/to/logo.jpg',
organizationUuid: 'a8439603-40f7-4b1e-8a35-cee9e53b98d4',
status: RegulationOrderRecordStatusEnum::DRAFT->value,
regulationOrderUuid: 'fce8177b-3737-4b4e-933d-fe29d0092c89',
Expand All @@ -42,6 +43,7 @@ public function testGetGeneralInformation(): void
'identifier' => $generalInfo->identifier,
'organizationName' => $generalInfo->organizationName,
'organizationUuid' => $generalInfo->organizationUuid,
'organizationLogo' => '/path/to/logo.jpg',
'status' => $generalInfo->status,
'regulationOrderUuid' => 'fce8177b-3737-4b4e-933d-fe29d0092c89',
'category' => $generalInfo->category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testView(): void
uuid: '3d1c6ec7-28f5-4b6b-be71-b0920e85b4bf',
identifier: 'FO1/2024',
organizationName: 'DiaLog',
organizationLogo: '/path/to/logo.jpg',
organizationUuid: 'a8439603-40f7-4b1e-8a35-cee9e53b98d4',
status: RegulationOrderRecordStatusEnum::DRAFT->value,
regulationOrderUuid: 'ed97924b-bdc5-421a-b6e8-ac3ee6b16a7e',
Expand All @@ -34,6 +35,7 @@ public function testView(): void
uuid: '3d1c6ec7-28f5-4b6b-be71-b0920e85b4bf',
identifier: 'FO1/2024',
organizationName: 'DiaLog',
organizationLogo: '/path/to/logo.jpg',
organizationUuid: 'a8439603-40f7-4b1e-8a35-cee9e53b98d4',
status: RegulationOrderRecordStatusEnum::PUBLISHED->value,
regulationOrderUuid: '8a32e881-a683-4caa-976f-6882296bc29b',
Expand Down
76 changes: 75 additions & 1 deletion tests/Unit/Infrastructure/Adapter/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,81 @@ public function testGet(): void
$filesystemOperator = $this->createMock(FilesystemOperator::class);
$storage = new Storage($filesystemOperator, '/path/to/medias');

$this->assertSame('/path/to/medias/logo.jpeg', $storage->get('logo.jpeg'));
$this->assertSame('/path/to/medias/logo.jpeg', $storage->getUrl('logo.jpeg'));
}

public function testRead(): void
{
$filesystemOperator = $this->createMock(FilesystemOperator::class);
$storage = new Storage($filesystemOperator, '/path/to/medias');

$filesystemOperator
->expects(self::once())
->method('fileExists')
->with('logo.jpeg')
->willReturn(true);

$filesystemOperator
->expects(self::once())
->method('read')
->willReturn('logoResource');

$this->assertSame('bG9nb1Jlc291cmNl', $storage->read('logo.jpeg'));
}

public function testReadFileNotFound(): void
{
$filesystemOperator = $this->createMock(FilesystemOperator::class);
$storage = new Storage($filesystemOperator, '/path/to/medias');

$filesystemOperator
->expects(self::once())
->method('fileExists')
->with('logo.jpeg')
->willReturn(false);

$filesystemOperator
->expects(self::never())
->method('read');

$this->assertNull($storage->read('logo.jpeg'));
}

public function testGetMimeType(): void
{
$filesystemOperator = $this->createMock(FilesystemOperator::class);
$storage = new Storage($filesystemOperator, '/path/to/medias');

$filesystemOperator
->expects(self::once())
->method('fileExists')
->with('logo.jpeg')
->willReturn(true);

$filesystemOperator
->expects(self::once())
->method('mimeType')
->willReturn('image/jpeg');

$this->assertSame('image/jpeg', $storage->getMimeType('logo.jpeg'));
}

public function testGetMimeTypeFileNotFound(): void
{
$filesystemOperator = $this->createMock(FilesystemOperator::class);
$storage = new Storage($filesystemOperator, '/path/to/medias');

$filesystemOperator
->expects(self::once())
->method('fileExists')
->with('logo.jpeg')
->willReturn(false);

$filesystemOperator
->expects(self::never())
->method('mimeType');

$this->assertNull($storage->getMimeType('logo.jpeg'));
}

public function testCantDelete(): void
Expand Down

0 comments on commit 18e2295

Please sign in to comment.