Files
mareike/app/Providers/UploadFileProvider.php

53 lines
1.6 KiB
PHP

<?php
namespace App\Providers;
use App\Models\CostUnit;
use App\ValueObjects\InvoiceFile;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
class UploadFileProvider {
private UploadedFile $file;
private CostUnit $costUnit;
public function __construct(UploadedFile $file, CostUnit $costUnit) {
$this->file = $file;
$this->costUnit = $costUnit;
}
public function saveUploadedFile() : ?InvoiceFile {
try {
$directory = sprintf(
'%1$s/invoices/%2$s',
app('tenant')->slug,
$this->costUnit->id
);
$filename = $this->normalizeFilename($this->file->getClientOriginalName());
$path = $this->file->storeAs(
$directory,
$filename
);
$invoiceFile = new InvoiceFile();
$invoiceFile->filename = $filename;
$invoiceFile->path = $path;
return $invoiceFile;
} catch (\Exception $e) {
return null;
}
}
private function normalizeFilename(string $filename) : string {
return strtolower($filename)
|> function (string $filename) : string { return str_replace(' ', '_', $filename); }
|> function (string $filename) : string { return str_replace('ä', 'ae', $filename); }
|> function (string $filename) : string { return str_replace('ö', 'oe', $filename); }
|> function (string $filename) : string { return str_replace('ü', 'ue', $filename); }
|> function (string $filename) : string { return str_replace('ß', 'ss', $filename); };
}
}