From 98388394f0921cddd5b8716793c1f4a68820ffea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Viguier?= Date: Thu, 11 Apr 2024 14:06:53 +0200 Subject: [PATCH] [stacked 11] cleaner approach of the pipe execution. (#2375) --- app/Actions/Photo/Create.php | 37 ++++++++++++++------------- app/DTO/PhotoCreate/StandaloneDTO.php | 10 ++++---- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/app/Actions/Photo/Create.php b/app/Actions/Photo/Create.php index 1e2f8c3cfe8..b89df0cfc52 100644 --- a/app/Actions/Photo/Create.php +++ b/app/Actions/Photo/Create.php @@ -157,22 +157,7 @@ private function handleStandalone(InitDTO $initDTO): Photo Standalone\CreateSizeVariants::class, ]; - try { - return app(Pipeline::class) - ->send($dto) - ->through($pipes) - ->thenReturn() - ->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->getPhoto()->delete(); - } catch (\Throwable) { - // Sic! If anything goes wrong here, we still throw the original exception - } - throw $e; - } + return $this->executePipeOnDTO($pipes, $dto)->getPhoto(); } private function handleVideoLivePartner(InitDTO $initDTO): Photo @@ -186,12 +171,28 @@ private function handleVideoLivePartner(InitDTO $initDTO): Photo Shared\Save::class, ]; + return $this->executePipeOnDTO($pipes, $dto)->getPhoto(); + } + + /** + * Execute the pipes on the DTO. + * + * @template T of VideoPartnerDTO|StandaloneDTO + * + * @param array $pipes + * @param T $dto + * + * @return T + * + * @throws LycheeException + */ + private function executePipeOnDTO(array $pipes, VideoPartnerDTO|StandaloneDTO $dto): VideoPartnerDTO|StandaloneDTO + { try { return app(Pipeline::class) ->send($dto) ->through($pipes) - ->thenReturn() - ->getPhoto(); + ->thenReturn(); } catch (LycheeException $e) { // If source file could not be put into final destination, remove // freshly created photo from DB to avoid having "zombie" entries. diff --git a/app/DTO/PhotoCreate/StandaloneDTO.php b/app/DTO/PhotoCreate/StandaloneDTO.php index 62954574225..79821b13941 100644 --- a/app/DTO/PhotoCreate/StandaloneDTO.php +++ b/app/DTO/PhotoCreate/StandaloneDTO.php @@ -39,11 +39,6 @@ public function __construct( ) { } - public function getPhoto(): Photo - { - return $this->photo; - } - public static function ofInit(InitDTO $initDTO): StandaloneDTO { return new StandaloneDTO( @@ -57,4 +52,9 @@ public static function ofInit(InitDTO $initDTO): StandaloneDTO shallDeleteImported: $initDTO->importMode->shallDeleteImported, ); } + + public function getPhoto(): Photo + { + return $this->photo; + } }