mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-09-03 11:15:04 +02:00
* refactor: migration from laravel-enum package to native php enums * style: fix StyleCI findings
96 lines
2.6 KiB
PHP
96 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\Storage\Base;
|
|
|
|
use App\Actions\ActionResult;
|
|
use App\Contracts\Actions\Storage\StorageResults;
|
|
use App\Enums\Actions\ActionStatus;
|
|
use App\Models\BaseModel;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* Class DeleteResults.
|
|
*/
|
|
readonly class DeleteResults implements StorageResults
|
|
{
|
|
/**
|
|
* Create a new action result instance.
|
|
*
|
|
* @param BaseModel $model
|
|
* @param array<string, bool> $deletions
|
|
*/
|
|
public function __construct(protected BaseModel $model, protected array $deletions = [])
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Write results to log.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function toLog(): void
|
|
{
|
|
if (empty($this->deletions)) {
|
|
Log::error('No deletions were attempted.');
|
|
}
|
|
foreach ($this->deletions as $fs => $result) {
|
|
$result
|
|
? Log::info("Deleted '{$this->model->getName()}' from disk '$fs'")
|
|
: Log::error("Failed to delete '{$this->model->getName()}' from disk '$fs'");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Write results to console output.
|
|
*
|
|
* @param Command $command
|
|
* @return void
|
|
*/
|
|
public function toConsole(Command $command): void
|
|
{
|
|
if (empty($this->deletions)) {
|
|
$command->error('No deletions were attempted.');
|
|
}
|
|
foreach ($this->deletions as $fs => $result) {
|
|
$result
|
|
? $command->info("Deleted '{$this->model->getName()}' from disk '$fs'")
|
|
: $command->error("Failed to delete '{$this->model->getName()}' from disk '$fs'");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Transform to Action Result.
|
|
*
|
|
* @return ActionResult
|
|
*/
|
|
public function toActionResult(): ActionResult
|
|
{
|
|
if (empty($this->deletions)) {
|
|
return new ActionResult(
|
|
ActionStatus::FAILED,
|
|
'No deletions were attempted. Please check that disks are configured.'
|
|
);
|
|
}
|
|
|
|
/** @var Collection $passed */
|
|
/** @var Collection $failed */
|
|
[$passed, $failed] = collect($this->deletions)->partition(fn (bool $result, string $fs) => $result);
|
|
|
|
if ($failed->isNotEmpty()) {
|
|
return new ActionResult(
|
|
ActionStatus::FAILED,
|
|
"Failed to delete '{$this->model->getName()}' from disks {$failed->keys()->join(', ', ' & ')}."
|
|
);
|
|
}
|
|
|
|
return new ActionResult(
|
|
ActionStatus::PASSED,
|
|
"Deleted '{$this->model->getName()}' from disks {$passed->keys()->join(', ', ' & ')}."
|
|
);
|
|
}
|
|
}
|