CleanOldPersonalDataExportsCommand   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 7
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 28 2
A getDisk() 0 4 1
1
<?php
2
3
namespace Spatie\PersonalDataExport\Commands;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use Illuminate\Contracts\Filesystem\Filesystem;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\Str;
10
11
class CleanOldPersonalDataExportsCommand extends Command
12
{
13
    protected $signature = 'personal-data-export:clean';
14
15
    protected $description = 'Remove old personal downloads';
16
17
    public function handle()
18
    {
19
        $this->comment('Start deleting old personal downloads...');
20
21
        $oldZipFiles = collect($this->getDisk()->allFiles())
22
            ->filter(function (string $zipFilename) {
23
                return Str::endsWith($zipFilename, '.zip');
24
            })
25
            ->filter(function (string $zipFilename) {
26
                $zipFilenameParts = explode('_', $zipFilename);
27
28
                if (! isset($zipFilenameParts[1])) {
29
                    return false;
30
                }
31
32
                $dateCreated = Carbon::createFromTimestamp($zipFilenameParts[1]);
33
34
                $threshold = now()->subDays(config('personal-data-export.delete_after_days'));
35
36
                return $dateCreated->isBefore($threshold);
37
            })
38
            ->toArray();
39
40
        $this->getDisk()->delete($oldZipFiles);
41
42
        $this->comment(count($oldZipFiles).' old zip files have been deleted.');
43
        $this->info('All done!');
44
    }
45
46
    protected function getDisk(): Filesystem
47
    {
48
        return Storage::disk(config('personal-data-export.disk'));
49
    }
50
}
51