Add short and long PDF paths to elements, keep pdfPath as a short PDF compatibility alias, and route generic file uploads by fileType.
184 lines
6.1 KiB
PHP
184 lines
6.1 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
|
|
{
|
|
private const FILE_TYPE_ICON_IMAGE = 'iconImage';
|
|
|
|
private const FILE_TYPE_SHORT_PDF = 'shortPdf';
|
|
|
|
private const FILE_TYPE_LONG_PDF = 'longPdf';
|
|
|
|
public function __construct(
|
|
private UpdateTitle $updateTitle,
|
|
private UpdateDescription $updateDescription,
|
|
private UpdateIconImageUrl $updateIconImageUrl,
|
|
private UpdateRichText $updateRichText,
|
|
private UpdateShortPdfPath $updateShortPdfPath,
|
|
private UpdateLongPdfPath $updateLongPdfPath,
|
|
private UpdateYoutubeUrl $updateYoutubeUrl,
|
|
private UpdateIconImage $updateIconImage,
|
|
private UpdateShortPdf $updateShortPdf,
|
|
private UpdateLongPdf $updateLongPdf,
|
|
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->shortPdfPath === null
|
|
&& $request->longPdfPath === null
|
|
&& $request->pdfPath === null
|
|
&& $request->youtubeUrl === null
|
|
&& $request->fileType === null
|
|
&& $request->fileContents === null
|
|
&& $request->fileOriginalName === null
|
|
&& $request->fileMimeType === null
|
|
&& $request->fileSizeBytes === 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->shortPdfPath !== null) {
|
|
$this->updateShortPdfPath->execute(new UpdateShortPdfPathRequest(
|
|
id: $request->id,
|
|
shortPdfPath: $request->shortPdfPath,
|
|
));
|
|
} elseif ($request->pdfPath !== null) {
|
|
$this->updateShortPdfPath->execute(new UpdateShortPdfPathRequest(
|
|
id: $request->id,
|
|
shortPdfPath: $request->pdfPath,
|
|
));
|
|
}
|
|
if ($request->longPdfPath !== null) {
|
|
$this->updateLongPdfPath->execute(new UpdateLongPdfPathRequest(
|
|
id: $request->id,
|
|
longPdfPath: $request->longPdfPath,
|
|
));
|
|
}
|
|
if ($request->youtubeUrl !== null) {
|
|
$this->updateYoutubeUrl->execute(new UpdateYoutubeUrlRequest(
|
|
id: $request->id,
|
|
youtubeUrl: $request->youtubeUrl,
|
|
));
|
|
}
|
|
if ($this->hasFileInput($request)) {
|
|
$this->updateFile($request);
|
|
}
|
|
|
|
$element = $this->elementRepository->find($request->id);
|
|
if ($element === null) {
|
|
throw new NotFoundException('Element not found');
|
|
}
|
|
|
|
return $element;
|
|
}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
* @throws NotFoundException
|
|
*/
|
|
private function updateFile(UpdateElementRequest $request): void
|
|
{
|
|
if ($request->fileType === null) {
|
|
throw new BadRequestException('fileType is required');
|
|
}
|
|
|
|
if ($request->fileType === self::FILE_TYPE_ICON_IMAGE) {
|
|
$this->updateIconImage->execute(
|
|
new UpdateIconImageRequest(
|
|
id: $request->id,
|
|
fileContents: $request->fileContents,
|
|
fileOriginalName: $request->fileOriginalName,
|
|
fileMimeType: $request->fileMimeType,
|
|
fileSizeBytes: $request->fileSizeBytes,
|
|
)
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
if ($request->fileType === self::FILE_TYPE_SHORT_PDF) {
|
|
$this->updateShortPdf->execute(
|
|
new UpdateShortPdfRequest(
|
|
id: $request->id,
|
|
fileContents: $request->fileContents,
|
|
fileOriginalName: $request->fileOriginalName,
|
|
fileMimeType: $request->fileMimeType,
|
|
fileSizeBytes: $request->fileSizeBytes,
|
|
)
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
if ($request->fileType === self::FILE_TYPE_LONG_PDF) {
|
|
$this->updateLongPdf->execute(
|
|
new UpdateLongPdfRequest(
|
|
id: $request->id,
|
|
fileContents: $request->fileContents,
|
|
fileOriginalName: $request->fileOriginalName,
|
|
fileMimeType: $request->fileMimeType,
|
|
fileSizeBytes: $request->fileSizeBytes,
|
|
)
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
throw new BadRequestException('fileType is invalid');
|
|
}
|
|
|
|
private function hasFileInput(UpdateElementRequest $request): bool
|
|
{
|
|
return $request->fileType !== null
|
|
|| $request->fileContents !== null
|
|
|| $request->fileOriginalName !== null
|
|
|| $request->fileMimeType !== null
|
|
|| $request->fileSizeBytes !== null;
|
|
}
|
|
}
|