Completed
Pull Request — master (#10)
by
unknown
06:59
created

DirectoryCleaner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\DirectoryCleanup;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Collection;
7
use Illuminate\Filesystem\Filesystem;
8
9
class DirectoryCleaner
10
{
11
    /** @var \Illuminate\Filesystem\Filesystem */
12
    protected $filesystem;
13
14
    /** @var string */
15
    protected $directory;
16
17
    /**
18
     * @param \Illuminate\Filesystem\Filesystem $filesystem
19
     */
20
    public function __construct(Filesystem $filesystem)
21
    {
22
        $this->filesystem = $filesystem;
23
    }
24
25
    /**
26
     * @param string $directory
27
     *
28
     * @return $this
29
     */
30
    public function setDirectory(string $directory)
31
    {
32
        $this->directory = $directory;
33
34
        return $this;
35
    }
36
37
    /**
38
     * @param int $minutes
39
     *
40
     * @return \Illuminate\Support\Collection
41
     */
42 View Code Duplication
    public function deleteDirectoriesOlderThanMinutes(int $minutes) : Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $timeInPast = Carbon::now()->subMinutes($minutes);
45
46
        return collect($this->filesystem->directories($this->directory))
47
            ->filter(function ($file) use ($timeInPast) {
48
                return Carbon::createFromTimestamp(filemtime($file))
49
                             ->lt($timeInPast);
50
            })
51
            ->each(function ($file) {
52
                $this->filesystem->deleteDirectory($file);
53
            });
54
    }
55
56
    /**
57
     * @param int $minutes
58
     *
59
     * @return \Illuminate\Support\Collection
60
     */
61 View Code Duplication
    public function deleteFilesOlderThanMinutes(int $minutes) : Collection {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
        $timeInPast = Carbon::now()->subMinutes($minutes);
63
64
        return collect($this->filesystem->files($this->directory))
65
            ->filter(function ($file) use ($timeInPast) {
66
                return Carbon::createFromTimestamp(filemtime($file))
67
                    ->lt($timeInPast);
68
            })
69
            ->each(function ($file) {
70
                $this->filesystem->delete($file);
71
            });
72
    }
73
}
74