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

DirectoryCleaner   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 38.46 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 25
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setDirectory() 0 6 1
A deleteDirectoriesOlderThanMinutes() 13 13 1
A deleteFilesOlderThanMinutes() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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