From 23a312c9a42858080af7487f00507b62d9d4534b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Viguier?= Date: Thu, 11 Apr 2024 14:03:52 +0200 Subject: [PATCH] [stacked 8] Refactor Save/Notify to use interface DTO. (#2372) --- app/Actions/Photo/Create.php | 6 +++--- app/Actions/Photo/Pipes/Shared/NotifyAlbums.php | 13 ++++++------- app/Actions/Photo/Pipes/Shared/Save.php | 14 ++++++++------ app/Contracts/PhotoCreate/DuplicatePipe.php | 2 -- app/Contracts/PhotoCreate/InitPipe.php | 4 +--- app/Contracts/PhotoCreate/PhotoDTO.php | 10 ++++++++++ app/Contracts/PhotoCreate/PhotoPipe.php | 17 +++++++++++++++++ app/Contracts/PhotoCreate/StandalonePipe.php | 2 -- app/DTO/PhotoCreate/DuplicateDTO.php | 8 +++++++- app/DTO/PhotoCreate/StandaloneDTO.php | 8 +++++++- 10 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 app/Contracts/PhotoCreate/PhotoDTO.php create mode 100644 app/Contracts/PhotoCreate/PhotoPipe.php diff --git a/app/Actions/Photo/Create.php b/app/Actions/Photo/Create.php index 009994a53ec..298762b3179 100644 --- a/app/Actions/Photo/Create.php +++ b/app/Actions/Photo/Create.php @@ -122,7 +122,7 @@ private function handleDuplicate(InitDTO $initDTO): Photo ->send($dto) ->through($pipes) ->thenReturn() - ->photo; + ->getPhoto(); } catch (PhotoResyncedException|PhotoSkippedException $e) { // duplicate case. Just rethrow. throw $e; @@ -155,12 +155,12 @@ private function handleStandalone(InitDTO $initDTO): Photo ->send($dto) ->through($pipes) ->thenReturn() - ->photo; + ->getPhoto(); } catch (LycheeException $e) { // If source file could not be put into final destination, remove // freshly created photo from DB to avoid having "zombie" entries. try { - $dto->photo->delete(); + $dto->getPhoto()->delete(); } catch (\Throwable) { // Sic! If anything goes wrong here, we still throw the original exception } diff --git a/app/Actions/Photo/Pipes/Shared/NotifyAlbums.php b/app/Actions/Photo/Pipes/Shared/NotifyAlbums.php index 345880eaaac..ac87cf7725e 100644 --- a/app/Actions/Photo/Pipes/Shared/NotifyAlbums.php +++ b/app/Actions/Photo/Pipes/Shared/NotifyAlbums.php @@ -3,20 +3,19 @@ namespace App\Actions\Photo\Pipes\Shared; use App\Actions\User\Notify; -use App\Contracts\PhotoCreate\SharedPipe; -use App\DTO\PhotoCreate\DuplicateDTO; -use App\DTO\PhotoCreate\StandaloneDTO; +use App\Contracts\PhotoCreate\PhotoDTO; +use App\Contracts\PhotoCreate\PhotoPipe; /** * Notify by email if a picture has been added. */ -class NotifyAlbums implements SharedPipe +class NotifyAlbums implements PhotoPipe { - public function handle(DuplicateDTO|StandaloneDTO $state, \Closure $next): DuplicateDTO|StandaloneDTO + public function handle(PhotoDTO $state, \Closure $next): PhotoDTO { - if ($state->photo->album_id !== null) { + if ($state->getPhoto()->album_id !== null) { $notify = new Notify(); - $notify->do($state->photo); + $notify->do($state->getPhoto()); } return $next($state); diff --git a/app/Actions/Photo/Pipes/Shared/Save.php b/app/Actions/Photo/Pipes/Shared/Save.php index c67aed901e3..109986b678c 100644 --- a/app/Actions/Photo/Pipes/Shared/Save.php +++ b/app/Actions/Photo/Pipes/Shared/Save.php @@ -2,15 +2,17 @@ namespace App\Actions\Photo\Pipes\Shared; -use App\Contracts\PhotoCreate\SharedPipe; -use App\DTO\PhotoCreate\DuplicateDTO; -use App\DTO\PhotoCreate\StandaloneDTO; +use App\Contracts\PhotoCreate\PhotoDTO; +use App\Contracts\PhotoCreate\PhotoPipe; -class Save implements SharedPipe +/** + * Persist current Photo object into database. + */ +class Save implements PhotoPipe { - public function handle(DuplicateDTO|StandaloneDTO $state, \Closure $next): DuplicateDTO|StandaloneDTO + public function handle(PhotoDTO $state, \Closure $next): PhotoDTO { - $state->photo->save(); + $state->getPhoto()->save(); return $next($state); } diff --git a/app/Contracts/PhotoCreate/DuplicatePipe.php b/app/Contracts/PhotoCreate/DuplicatePipe.php index 585b845d745..35fd5d643d5 100644 --- a/app/Contracts/PhotoCreate/DuplicatePipe.php +++ b/app/Contracts/PhotoCreate/DuplicatePipe.php @@ -6,8 +6,6 @@ /** * Basic definition of a Duplicate Photo pipe. - * - * This allows to clarify which steps are applied in which order. */ interface DuplicatePipe { diff --git a/app/Contracts/PhotoCreate/InitPipe.php b/app/Contracts/PhotoCreate/InitPipe.php index a3826e8675c..a15949dcd04 100644 --- a/app/Contracts/PhotoCreate/InitPipe.php +++ b/app/Contracts/PhotoCreate/InitPipe.php @@ -5,9 +5,7 @@ use App\DTO\PhotoCreate\InitDTO; /** - * Basic definition of a Photo creation pipe. - * - * This allows to clarify which steps are applied in which order. + * Initial pipes, could be seen as pre-processing steps. */ interface InitPipe { diff --git a/app/Contracts/PhotoCreate/PhotoDTO.php b/app/Contracts/PhotoCreate/PhotoDTO.php new file mode 100644 index 00000000000..5dcb53ea694 --- /dev/null +++ b/app/Contracts/PhotoCreate/PhotoDTO.php @@ -0,0 +1,10 @@ +intendedOwnerId = $initDTO->intendedOwnerId; } + public function getPhoto(): Photo + { + return $this->photo; + } + public function setHasBeenResync(bool $val): void { $this->hasBeenReSynced = $val; diff --git a/app/DTO/PhotoCreate/StandaloneDTO.php b/app/DTO/PhotoCreate/StandaloneDTO.php index 50e9140be8c..d74921b3cbe 100644 --- a/app/DTO/PhotoCreate/StandaloneDTO.php +++ b/app/DTO/PhotoCreate/StandaloneDTO.php @@ -6,13 +6,14 @@ use App\Contracts\Image\StreamStats; use App\Contracts\Models\AbstractAlbum; use App\Contracts\Models\AbstractSizeVariantNamingStrategy; +use App\Contracts\PhotoCreate\PhotoDTO; use App\Image\Files\FlysystemFile; use App\Image\Files\NativeLocalFile; use App\Image\Files\TemporaryLocalFile; use App\Metadata\Extractor; use App\Models\Photo; -class StandaloneDTO +class StandaloneDTO implements PhotoDTO { public readonly bool $shallImportViaSymlink; public readonly bool $shallDeleteImported; @@ -52,4 +53,9 @@ public function __construct(InitDTO $initDTO) $this->shallImportViaSymlink = $initDTO->importMode->shallImportViaSymlink; $this->shallDeleteImported = $initDTO->importMode->shallDeleteImported; } + + public function getPhoto(): Photo + { + return $this->photo; + } }