Skip to content

Commit

Permalink
[stacked 8] Refactor Save/Notify to use interface DTO. (#2372)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Apr 11, 2024
1 parent 069e735 commit 23a312c
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 25 deletions.
6 changes: 3 additions & 3 deletions app/Actions/Photo/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 6 additions & 7 deletions app/Actions/Photo/Pipes/Shared/NotifyAlbums.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions app/Actions/Photo/Pipes/Shared/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 0 additions & 2 deletions app/Contracts/PhotoCreate/DuplicatePipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

/**
* Basic definition of a Duplicate Photo pipe.
*
* This allows to clarify which steps are applied in which order.
*/
interface DuplicatePipe
{
Expand Down
4 changes: 1 addition & 3 deletions app/Contracts/PhotoCreate/InitPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
10 changes: 10 additions & 0 deletions app/Contracts/PhotoCreate/PhotoDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Contracts\PhotoCreate;

use App\Models\Photo;

interface PhotoDTO
{
public function getPhoto(): Photo;
}
17 changes: 17 additions & 0 deletions app/Contracts/PhotoCreate/PhotoPipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Contracts\PhotoCreate;

/**
* Basic definition of a Photo creation pipe.
*/
interface PhotoPipe
{
/**
* @param PhotoDTO $state
* @param \Closure(PhotoDTO $state): PhotoDTO $next
*
* @return PhotoDTO
*/
public function handle(PhotoDTO $state, \Closure $next): PhotoDTO;
}
2 changes: 0 additions & 2 deletions app/Contracts/PhotoCreate/StandalonePipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

/**
* Basic definition of a Standalone Photo pipe.
*
* This allows to clarify which steps are applied in which order.
*/
interface StandalonePipe
{
Expand Down
8 changes: 7 additions & 1 deletion app/DTO/PhotoCreate/DuplicateDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace App\DTO\PhotoCreate;

use App\Contracts\Models\AbstractAlbum;
use App\Contracts\PhotoCreate\PhotoDTO;
use App\Metadata\Extractor;
use App\Models\Photo;

/**
* DTO used when dealing with duplicates.
* We only keep the needed datas.
*/
class DuplicateDTO
class DuplicateDTO implements PhotoDTO
{
public readonly bool $shallResyncMetadata;
public readonly bool $shallSkipDuplicates;
Expand Down Expand Up @@ -44,6 +45,11 @@ public function __construct(InitDTO $initDTO)
$this->intendedOwnerId = $initDTO->intendedOwnerId;
}

public function getPhoto(): Photo
{
return $this->photo;
}

public function setHasBeenResync(bool $val): void
{
$this->hasBeenReSynced = $val;
Expand Down
8 changes: 7 additions & 1 deletion app/DTO/PhotoCreate/StandaloneDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 23a312c

Please sign in to comment.