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

115 lines
3.9 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 UpdateElement
{
public function __construct(
private UpdateTitle $updateTitle,
private UpdateDescription $updateDescription,
private UpdateIconImageUrl $updateIconImageUrl,
private UpdateRichText $updateRichText,
private UpdatePdfPath $updatePdfPath,
private UpdateYoutubeUrl $updateYoutubeUrl,
private UpdateElementIconImage $updateElementIconImage,
private UpdateElementPdf $updateElementPdf,
private ElementRepository $elementRepository,
) {
}
/**
* @throws BadRequestException
* @throws NotFoundException
*/
public function execute(UpdateElementRequest $request): Element
{
if ($request->id === null) {
throw new BadRequestException('id is required');
}
$hasNoFields = $request->title === null
&& $request->description === null
&& $request->iconImageUrl === null
&& $request->richText === null
&& $request->pdfPath === null
&& $request->youtubeUrl === null
&& $request->iconImageContents === null
&& $request->pdfContents === 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,
));
}
if ($request->iconImageContents !== null) {
$this->updateElementIconImage->execute(
new UpdateElementIconImageRequest(
id: $request->id,
iconImageContents: $request->iconImageContents,
iconImageOriginalName: $request->iconImageOriginalName,
iconImageMimeType: $request->iconImageMimeType,
iconImageSizeBytes: $request->iconImageSizeBytes,
)
);
}
if ($request->pdfContents !== null) {
$this->updateElementPdf->execute(
new UpdateElementPdfRequest(
id: $request->id,
pdfContents: $request->pdfContents,
pdfOriginalName: $request->pdfOriginalName,
pdfMimeType: $request->pdfMimeType,
pdfSizeBytes: $request->pdfSizeBytes,
)
);
}
$element = $this->elementRepository->find($request->id);
if ($element === null) {
throw new NotFoundException('Element not found');
}
return $element;
}
}