netgescon-day0/app/Services/TenantArchivePathService.php

134 lines
4.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\Amministratore;
use App\Models\Fornitore;
use App\Models\Stabile;
use Illuminate\Support\Facades\Storage;
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::disk('local')->path($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::disk('local')->path($this->stabileRelativePath($stabile, $suffix));
}
public function fornitoreCode(Fornitore | string $fornitore): string
{
if (is_string($fornitore)) {
$code = trim($fornitore);
if ($code === '') {
throw new InvalidArgumentException('Codice fornitore non valido.');
}
return $code;
}
$code = trim((string) ($fornitore->codice_univoco ?: ''));
if ($code === '') {
throw new InvalidArgumentException('Fornitore senza codice archivio canonico.');
}
return $code;
}
public function fornitoreRelativePath(Fornitore | string $fornitore, ?string $suffix = null): string
{
$basePath = 'fornitori/' . $this->fornitoreCode($fornitore);
return $this->appendSuffix($basePath, $suffix);
}
public function fornitoreAbsolutePath(Fornitore | string $fornitore, ?string $suffix = null): string
{
return Storage::disk('local')->path($this->fornitoreRelativePath($fornitore, $suffix));
}
public function fornitoreLegacyRelativePath(Fornitore $fornitore, ?string $suffix = null): string
{
$basePath = $this->amministratoreRelativePath($fornitore->amministratore) . '/fornitori/' . $this->fornitoreCode($fornitore);
return $this->appendSuffix($basePath, $suffix);
}
public function fornitoreLegacyAbsolutePath(Fornitore $fornitore, ?string $suffix = null): string
{
return Storage::disk('local')->path($this->fornitoreLegacyRelativePath($fornitore, $suffix));
}
private function appendSuffix(string $basePath, ?string $suffix): string
{
$normalizedSuffix = trim((string) $suffix, '/');
if ($normalizedSuffix === '') {
return $basePath;
}
return $basePath . '/' . $normalizedSuffix;
}
}