ldc-cocotte/app/Traits/TracksPendingStoragePaths.php

76 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace App\Traits;
use App\Contracts\ActionInput;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
trait TracksPendingStoragePaths
{
/** @var array<int, list<\Uwil\Storage\Lib\ValueObjects\StorageFilePath>> */
private array $pendingUploadedPaths = [];
/** @var array<int, list<\Uwil\Storage\Lib\ValueObjects\StorageFilePath>> */
private array $pendingObsoletePaths = [];
protected function trackUploadedPath(ActionInput $input, StorageFilePath $path): void
{
$key = spl_object_id($input);
$this->pendingUploadedPaths[$key][] = $path;
}
protected function trackReplacedPath(
ActionInput $input,
?StorageFilePath $oldPath,
?StorageFilePath $newPath,
): void {
if ($oldPath === null || $newPath === null || $oldPath->value === $newPath->value) {
return;
}
$this->trackObsoletePath($input, $oldPath);
}
protected function trackObsoletePath(ActionInput $input, ?StorageFilePath $path): void
{
if ($path === null) {
return;
}
$key = spl_object_id($input);
$this->pendingObsoletePaths[$key][] = $path;
}
/**
* @return list<\Uwil\Storage\Lib\ValueObjects\StorageFilePath>
*/
protected function pullUploadedPaths(ActionInput $input): array
{
$key = spl_object_id($input);
$paths = $this->pendingUploadedPaths[$key] ?? [];
$this->clearTrackedPaths($input);
return $paths;
}
/**
* @return list<\Uwil\Storage\Lib\ValueObjects\StorageFilePath>
*/
protected function pullObsoletePaths(ActionInput $input): array
{
$key = spl_object_id($input);
$paths = $this->pendingObsoletePaths[$key] ?? [];
$this->clearTrackedPaths($input);
return $paths;
}
protected function clearTrackedPaths(ActionInput $input): void
{
$key = spl_object_id($input);
unset($this->pendingUploadedPaths[$key], $this->pendingObsoletePaths[$key]);
}
}