62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cqs\Actions\Ranges;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Lib\PublicS3Disk;
|
|
use App\Models\Range;
|
|
use App\Services\ImageOptimizerService;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Action<\App\Cqs\Actions\Ranges\CreateRangeInput, \App\Models\Range>
|
|
*/
|
|
final class CreateRange implements Action
|
|
{
|
|
public function __construct(
|
|
protected readonly StorageService $storageService,
|
|
protected readonly PublicS3Disk $s3Disk,
|
|
protected readonly ImageOptimizerService $imageOptimizerService,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param \App\Cqs\Actions\Ranges\CreateRangeInput $input
|
|
*/
|
|
#[\Override]
|
|
public function __invoke(ActionInput $input): Range
|
|
{
|
|
Assert::isInstanceOf($input, CreateRangeInput::class);
|
|
|
|
$attributes = [
|
|
'name' => $input->name,
|
|
'description' => $input->description,
|
|
'picture_alt' => $input->pictureMetadata->alt ?? '',
|
|
'picture_title' => $input->pictureMetadata->title ?? '',
|
|
'status' => $input->status,
|
|
];
|
|
|
|
if ($input->picture instanceof UploadedFile) {
|
|
$attributes['picture'] = $this->storeFile($input->picture);
|
|
}
|
|
|
|
$range = new Range();
|
|
$range->fill($attributes);
|
|
$range->saveOrFail();
|
|
|
|
return $range;
|
|
}
|
|
|
|
protected function storeFile(UploadedFile $file): StorageFilePath
|
|
{
|
|
$optimized = $this->imageOptimizerService->optimize($file);
|
|
|
|
return $this->storageService->storeFileOntoDisk($optimized, $this->s3Disk->disk);
|
|
}
|
|
}
|