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

DirectoryCleaner::setFileSystemDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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\Support\Facades\Storage */
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\Support\Facades\Storage> 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
        $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\Support\Facades\Storage> 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...
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param int $minutes
52
     *
53
     * @return \Illuminate\Support\Collection
54
     */
55
    public function deleteFilesOlderThanMinutes(int $minutes) : Collection
56
    {
57
        $timeInPast = Carbon::now()->subMinutes($minutes);
58
59
        return collect($this->filesystem->allFiles($this->directory))
0 ignored issues
show
Bug introduced by
The method allFiles() does not seem to exist on object<Illuminate\Support\Facades\Storage>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
            ->filter(function ($file) use ($timeInPast) {
61
                return Carbon::createFromTimestamp($this->filesystem->lastModified($file))
0 ignored issues
show
Bug introduced by
The method lastModified() does not seem to exist on object<Illuminate\Support\Facades\Storage>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
                    ->lt($timeInPast);
63
            })
64
            ->each(function ($file) {
65
                $this->filesystem->delete($file);
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Illuminate\Support\Facades\Storage>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
            });
67
    }
68
}
69