Lorsqu'un super-admin crée un établissement via l'interface, le système doit automatiquement créer la base tenant, exécuter les migrations, créer le premier utilisateur admin et envoyer l'invitation — le tout de manière asynchrone pour ne pas bloquer la réponse HTTP. Ce mécanisme rend chaque établissement opérationnel dès sa création sans intervention manuelle sur l'infrastructure.
81 lines
1.8 KiB
PHP
81 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Scolarite\Infrastructure\Storage;
|
|
|
|
use App\Scolarite\Application\Port\FileStorage;
|
|
|
|
use function dirname;
|
|
use function file_put_contents;
|
|
use function fopen;
|
|
use function is_dir;
|
|
use function is_file;
|
|
use function is_string;
|
|
use function mkdir;
|
|
|
|
use Override;
|
|
|
|
use function realpath;
|
|
|
|
use RuntimeException;
|
|
|
|
use function sprintf;
|
|
use function str_starts_with;
|
|
use function unlink;
|
|
|
|
final readonly class LocalFileStorage implements FileStorage
|
|
{
|
|
public function __construct(
|
|
private string $storagePath,
|
|
) {
|
|
}
|
|
|
|
#[Override]
|
|
public function upload(string $path, mixed $content, string $mimeType): string
|
|
{
|
|
$fullPath = $this->storagePath . '/' . $path;
|
|
$dir = dirname($fullPath);
|
|
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0o755, true);
|
|
}
|
|
|
|
$data = is_string($content) ? $content : '';
|
|
|
|
file_put_contents($fullPath, $data);
|
|
|
|
return $path;
|
|
}
|
|
|
|
#[Override]
|
|
public function delete(string $path): void
|
|
{
|
|
$fullPath = $this->storagePath . '/' . $path;
|
|
|
|
if (is_file($fullPath)) {
|
|
unlink($fullPath);
|
|
}
|
|
}
|
|
|
|
#[Override]
|
|
public function readStream(string $path): mixed
|
|
{
|
|
$fullPath = $this->storagePath . '/' . $path;
|
|
$realPath = realpath($fullPath);
|
|
$realStoragePath = realpath($this->storagePath);
|
|
|
|
if ($realPath === false || $realStoragePath === false || !str_starts_with($realPath, $realStoragePath)) {
|
|
throw new RuntimeException(sprintf('Impossible de lire le fichier : %s', $path));
|
|
}
|
|
|
|
$stream = fopen($realPath, 'r');
|
|
|
|
if ($stream === false) {
|
|
throw new RuntimeException(sprintf('Impossible de lire le fichier : %s', $path));
|
|
}
|
|
|
|
return $stream;
|
|
}
|
|
}
|