From a408ed4a94322327c7276152100ab6a3cef2293e Mon Sep 17 00:00:00 2001 From: ChrisToxz Date: Sat, 15 Jul 2023 00:28:07 +0200 Subject: [PATCH] feat(commands): ss:check command to cross-check if all Slips and/or Folders are matching with each other. If not matching, you get an option to clean it up! --- app/Console/Commands/CrossCheckSlips.php | 89 ++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 app/Console/Commands/CrossCheckSlips.php diff --git a/app/Console/Commands/CrossCheckSlips.php b/app/Console/Commands/CrossCheckSlips.php new file mode 100644 index 0000000..1380a2a --- /dev/null +++ b/app/Console/Commands/CrossCheckSlips.php @@ -0,0 +1,89 @@ +choice('Check Slips, folders or both?', ['both', 'slips', 'folders'], 0); + + $slips = Slip::all(); + $folders = collect(Storage::disk('slips')->directories()); + + $invalid_folders = []; + $invalid_slips = []; + + if ($choice === 'slips' or $choice === 'both') { + $this->info('Checking Slips...'); + foreach ($slips as $slip) { + if (!$folders->contains($slip->token)) { + $invalid_slips[] = $slip->token; + } + } + } + + if ($choice === 'folders' or $choice === 'both') { + $this->info('Checking Folders...'); + foreach ($folders as $folder) { + if (!$slips->contains(function ($key) use ($folder) { + return $key->token === $folder; + })) { + $invalid_folders[] = $folder; + } + } + } + + $count_folders = count($invalid_folders); + $count_slips = count($invalid_slips); + + + if ($count_folders or $count_slips > 0) { + $this->alert('Slips and/or folders not matching'); + $this->error("{$count_slips} folder(s) not found (Slip found, but no folder in database)"); + $this->line(implode(",", $invalid_slips)); + $this->error("{$count_folders} slip(s) not found (Folder found, but no Slip in database)"); + $this->line(implode(",", $invalid_folders)); + + + if ($this->confirm('Do you want to clean up those Slips and/or folders?', + false)) { + foreach ($invalid_folders as $folder) { + $this->info("Deleting folder {$folder}"); + Storage::disk('slips')->deleteDirectory($folder); + } + + foreach ($invalid_slips as $slip) { + $this->info("Deleting slip {$slip}"); + Slip::where('token', $slip)->delete(); + } + $this->alert('Cleaned!'); + } + } else { + $this->info('All Slips and Folders are matching, nothing to clean up!'); + } + + } +}