Completed
Push — master ( 6d0ead...1dc8c6 )
by Freek
07:17 queued 04:22
created

ListCommand::getTargetFileSystems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Spatie\Backup\Commands;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Facades\Storage;
9
10
class ListCommand extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'backup:list';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'List all backups';
25
26
    public function fire()
27
    {
28
        $path = config('laravel-backup.destination.path');
29
        $collection = new Collection();
30
31
        foreach ($this->getTargetFileSystems() as $filesystem) {
32
            $disk = Storage::disk($filesystem);
33
34
            foreach ($disk->files($path) as $file) {
35
                $collection->push([
36
                    'filesystem' => $filesystem,
37
                    'name' => $file,
38
                    'lastModified' => $disk->lastModified($file),
39
                ]);
40
            }
41
        }
42
43
        $rows = $collection
44
            ->sortByDesc('lastModified')
45
            ->filter(function ($value) {
46
                return ends_with($value['name'], '.zip');
47
            })
48
            ->map(function ($value) {
49
                $lastModified = Carbon::createFromTimestamp($value['lastModified']);
50
51
                $value['lastModified'] = $lastModified;
52
                $value['age'] = $lastModified->diffForHumans();
53
54
                return $value;
55
            });
56
57
        $this->table(['Filesystem', 'Filename', 'Created at', 'Age'], $rows);
58
    }
59
60
    /**
61
     * Get the filesystems to where the database should be dumped.
62
     *
63
     * @return array
64
     */
65
    protected function getTargetFileSystems()
66
    {
67
        $fileSystems = config('laravel-backup.destination.filesystem');
68
69
        if (is_array($fileSystems)) {
70
            return $fileSystems;
71
        }
72
73
        return [$fileSystems];
74
    }
75
76
}
77