78 lines
2.3 KiB
PHP
78 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Set\UseCases\UpdateSet;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\NotFoundException;
|
|
use App\Set\Set;
|
|
use App\Set\SetRepository;
|
|
|
|
class UpdateSet
|
|
{
|
|
public function __construct(
|
|
private UpdateName $updateName,
|
|
private UpdateDescription $updateDescription,
|
|
private UpdateIconImage $updateIconImage,
|
|
private SetRepository $setRepository,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
* @throws NotFoundException
|
|
*/
|
|
public function execute(UpdateSetRequest $request): Set
|
|
{
|
|
if ($request->id === null) {
|
|
throw new BadRequestException('setId is required');
|
|
}
|
|
|
|
if ($this->hasNoFields($request)) {
|
|
throw new BadRequestException('nothing to update');
|
|
}
|
|
|
|
if ($request->name !== null) {
|
|
$this->updateName->execute(new UpdateNameRequest(
|
|
id: $request->id,
|
|
name: $request->name,
|
|
));
|
|
}
|
|
if ($request->description !== null) {
|
|
$this->updateDescription->execute(new UpdateDescriptionRequest(
|
|
id: $request->id,
|
|
description: $request->description,
|
|
));
|
|
}
|
|
if ($this->hasIconImageInput($request)) {
|
|
$this->updateIconImage->execute(new UpdateIconImageRequest(
|
|
id: $request->id,
|
|
fileContents: $request->iconImageContents,
|
|
fileOriginalName: $request->iconImageOriginalName,
|
|
fileMimeType: $request->iconImageMimeType,
|
|
fileSizeBytes: $request->iconImageSizeBytes,
|
|
));
|
|
}
|
|
|
|
$set = $this->setRepository->find($request->id);
|
|
if ($set === null) {
|
|
throw new NotFoundException('Set not found');
|
|
}
|
|
|
|
return $set;
|
|
}
|
|
|
|
private function hasNoFields(UpdateSetRequest $request): bool
|
|
{
|
|
return $request->name === null
|
|
&& $request->description === null
|
|
&& ! $this->hasIconImageInput($request);
|
|
}
|
|
|
|
private function hasIconImageInput(UpdateSetRequest $request): bool
|
|
{
|
|
return $request->iconImageContents !== null
|
|
|| $request->iconImageOriginalName !== null
|
|
|| $request->iconImageMimeType !== null
|
|
|| $request->iconImageSizeBytes !== null;
|
|
}
|
|
}
|