Rabbi_Gerzi/backend/app/Element/UseCases/UpdateElement/UpdateIconImageUrl.php

50 lines
1.2 KiB
PHP

<?php
namespace App\Element\UseCases\UpdateElement;
use App\Element\Element;
use App\Element\ElementRepository;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException;
class UpdateIconImageUrl
{
public function __construct(private ElementRepository $elementRepository)
{
}
/**
* @throws BadRequestException
* @throws NotFoundException
*/
public function execute(UpdateIconImageUrlRequest $request): Element
{
if ($request->id === null) {
throw new BadRequestException('id is required');
}
if ($request->iconImageUrl === null) {
throw new BadRequestException('iconImageUrl is required');
}
$element = $this->elementRepository->find($request->id);
if ($element === null) {
throw new NotFoundException('Element not found');
}
$element->setIconImageUrl($this->nullableString(
$request->iconImageUrl,
));
return $this->elementRepository->update($element);
}
private function nullableString(string $value): ?string
{
if ($value === '') {
return null;
}
return $value;
}
}