Completed
Pull Request — master (#19)
by
unknown
01:37
created

DirectoryCleaner::setFileSystemDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\DirectoryCleanup;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Storage;
8
9
class DirectoryCleaner
10
{
11
    /** @var \Illuminate\Filesystem\Filesystem */
12
    protected $filesystem;
13
14
    /** @var string */
15
    protected $directory;
16
17
    /**
18
     * DirectoryCleaner constructor.
19
     */
20
    public function __construct()
21
    {
22
        // use our default storage by default
23
        $this->filesystem = Storage::disk();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Facades\Storage::disk() of type object<Illuminate\Contra...\Filesystem\Filesystem> is incompatible with the declared type object<Illuminate\Filesystem\Filesystem> of property $filesystem.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
24
    }
25
26
    /**
27
     * @param string $directory
28
     *
29
     * @return $this
30
     */
31
    public function setDirectory(string $directory)
32
    {
33
        $this->directory = $directory;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @param string $driver
40
     *
41
     * @return $this
42
     */
43
    public function setFileSystemDriver(string $driver)
44
    {
45
46
        $this->filesystem = Storage::disk($driver);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Facades\Storage::disk($driver) of type object<Illuminate\Contra...\Filesystem\Filesystem> is incompatible with the declared type object<Illuminate\Filesystem\Filesystem> of property $filesystem.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param int $minutes
53
     *
54
     * @return \Illuminate\Support\Collection
55
     */
56
    public function deleteFilesOlderThanMinutes(int $minutes): Collection
57
    {
58
        $timeInPast = Carbon::now()->subMinutes($minutes);
59
60
        return collect($this->filesystem->allFiles($this->directory))
61
            ->filter(function ($file) use ($timeInPast) {
62
                return Carbon
63
                    ::createFromTimestamp($this->filesystem->lastModified($file))
64
                    ->lt($timeInPast);
65
            })
66
            ->each(function ($file) {
67
                $this->filesystem->delete($file);
68
            });
69
    }
70
}
71