50 lines
1.2 KiB
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 UpdateYoutubeUrl
|
|
{
|
|
public function __construct(private ElementRepository $elementRepository)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
* @throws NotFoundException
|
|
*/
|
|
public function execute(UpdateYoutubeUrlRequest $request): Element
|
|
{
|
|
if ($request->id === null) {
|
|
throw new BadRequestException('id is required');
|
|
}
|
|
|
|
if ($request->youtubeUrl === null) {
|
|
throw new BadRequestException('youtubeUrl is required');
|
|
}
|
|
|
|
$element = $this->elementRepository->find($request->id);
|
|
if ($element === null) {
|
|
throw new NotFoundException('Element not found');
|
|
}
|
|
|
|
$element->setYoutubeUrl($this->nullableString(
|
|
$request->youtubeUrl,
|
|
));
|
|
|
|
return $this->elementRepository->update($element);
|
|
}
|
|
|
|
private function nullableString(string $value): ?string
|
|
{
|
|
if ($value === '') {
|
|
return null;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|