87 lines
2.5 KiB
PHP
87 lines
2.5 KiB
PHP
<?php
|
|
namespace App\Services;
|
|
|
|
use App\Models\Amministratore;
|
|
use App\Models\Stabile;
|
|
use InvalidArgumentException;
|
|
|
|
class TenantArchivePathService
|
|
{
|
|
public function amministratoreCode(Amministratore | string $amministratore): string
|
|
{
|
|
if (is_string($amministratore)) {
|
|
$code = trim($amministratore);
|
|
|
|
if ($code === '') {
|
|
throw new InvalidArgumentException('Codice amministratore non valido.');
|
|
}
|
|
|
|
return $code;
|
|
}
|
|
|
|
$code = trim((string) ($amministratore->codice_amministratore ?: $amministratore->codice_univoco ?: ''));
|
|
|
|
if ($code === '') {
|
|
throw new InvalidArgumentException('Amministratore senza codice archivio canonico.');
|
|
}
|
|
|
|
return $code;
|
|
}
|
|
|
|
public function stabileCode(Stabile | string $stabile): string
|
|
{
|
|
if (is_string($stabile)) {
|
|
$code = trim($stabile);
|
|
|
|
if ($code === '') {
|
|
throw new InvalidArgumentException('Codice stabile non valido.');
|
|
}
|
|
|
|
return $code;
|
|
}
|
|
|
|
$code = trim((string) ($stabile->codice_stabile ?? ''));
|
|
|
|
if ($code === '') {
|
|
throw new InvalidArgumentException('Stabile senza codice archivio canonico.');
|
|
}
|
|
|
|
return $code;
|
|
}
|
|
|
|
public function amministratoreRelativePath(Amministratore | string $amministratore, ?string $suffix = null): string
|
|
{
|
|
$basePath = 'amministratori/' . $this->amministratoreCode($amministratore);
|
|
|
|
return $this->appendSuffix($basePath, $suffix);
|
|
}
|
|
|
|
public function amministratoreAbsolutePath(Amministratore | string $amministratore, ?string $suffix = null): string
|
|
{
|
|
return storage_path('app/' . $this->amministratoreRelativePath($amministratore, $suffix));
|
|
}
|
|
|
|
public function stabileRelativePath(Stabile $stabile, ?string $suffix = null): string
|
|
{
|
|
$basePath = $this->amministratoreRelativePath($stabile->amministratore) . '/stabili/' . $this->stabileCode($stabile);
|
|
|
|
return $this->appendSuffix($basePath, $suffix);
|
|
}
|
|
|
|
public function stabileAbsolutePath(Stabile $stabile, ?string $suffix = null): string
|
|
{
|
|
return storage_path('app/' . $this->stabileRelativePath($stabile, $suffix));
|
|
}
|
|
|
|
private function appendSuffix(string $basePath, ?string $suffix): string
|
|
{
|
|
$normalizedSuffix = trim((string) $suffix, '/');
|
|
|
|
if ($normalizedSuffix === '') {
|
|
return $basePath;
|
|
}
|
|
|
|
return $basePath . '/' . $normalizedSuffix;
|
|
}
|
|
}
|