refactor element updates

This commit is contained in:
Yisroel Baum 2026-05-28 20:33:14 +03:00
parent 581852ecbc
commit d334745ade
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
19 changed files with 453 additions and 75 deletions

View file

@ -4,14 +4,20 @@ namespace App\Element\UseCases\UpdateElement;
use App\Element\Element;
use App\Element\ElementRepository;
use App\Element\UpdateElementDto;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException;
class UpdateElement
{
public function __construct(private ElementRepository $elementRepository)
{
public function __construct(
private UpdateTitle $updateTitle,
private UpdateDescription $updateDescription,
private UpdateIconImageUrl $updateIconImageUrl,
private UpdateRichText $updateRichText,
private UpdatePdfPath $updatePdfPath,
private UpdateYoutubeUrl $updateYoutubeUrl,
private ElementRepository $elementRepository,
) {
}
/**
@ -24,8 +30,53 @@ class UpdateElement
throw new BadRequestException('id is required');
}
if ($request->title === null || trim($request->title) === '') {
throw new BadRequestException('title is required');
$hasNoFields = $request->title === null
&& $request->description === null
&& $request->iconImageUrl === null
&& $request->richText === null
&& $request->pdfPath === null
&& $request->youtubeUrl === null;
if ($hasNoFields) {
throw new BadRequestException('nothing to update');
}
if ($request->title !== null) {
$this->updateTitle->execute(new UpdateTitleRequest(
id: $request->id,
title: $request->title,
));
}
if ($request->description !== null) {
$this->updateDescription->execute(new UpdateDescriptionRequest(
id: $request->id,
description: $request->description,
));
}
if ($request->iconImageUrl !== null) {
$this->updateIconImageUrl->execute(
new UpdateIconImageUrlRequest(
id: $request->id,
iconImageUrl: $request->iconImageUrl,
)
);
}
if ($request->richText !== null) {
$this->updateRichText->execute(new UpdateRichTextRequest(
id: $request->id,
richText: $request->richText,
));
}
if ($request->pdfPath !== null) {
$this->updatePdfPath->execute(new UpdatePdfPathRequest(
id: $request->id,
pdfPath: $request->pdfPath,
));
}
if ($request->youtubeUrl !== null) {
$this->updateYoutubeUrl->execute(new UpdateYoutubeUrlRequest(
id: $request->id,
youtubeUrl: $request->youtubeUrl,
));
}
$element = $this->elementRepository->find($request->id);
@ -33,25 +84,6 @@ class UpdateElement
throw new NotFoundException('Element not found');
}
return $this->elementRepository->update(
$element,
new UpdateElementDto(
title: $request->title,
description: $request->description ?? '',
iconImageUrl: $this->nullableString($request->iconImageUrl),
richText: $request->richText ?? '',
pdfPath: $this->nullableString($request->pdfPath),
youtubeUrl: $this->nullableString($request->youtubeUrl),
),
);
}
private function nullableString(?string $value): ?string
{
if ($value === null || $value === '') {
return null;
}
return $value;
return $element;
}
}