Passed
Branch release-2.x (69d3cf)
by Slye
03:37 queued 01:46
created

FileManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A _getFiles() 0 9 3
A removeOldFilesByIntervalDays() 0 8 3
A getInstance() 0 7 2
1
<?php
2
3
namespace NDC\DatabaseBackup;
4
5
/**
6
 * Class FileManager
7
 * @package NDC\DatabaseBackup
8
 */
9
class FileManager
10
{
11
    /**
12
     * @var array
13
     */
14
    private $params;
15
    /**
16
     * @var array
17
     */
18
    private $files = [];
19
    /**
20
     * @var self
21
     */
22
    private static $_instance;
23
24
    /**
25
     * @return FileManager|null
26
     */
27
    public static function getInstance(): ?self
28
    {
29
30
        if (self::$_instance === null) {
31
            self::$_instance = new self;
32
        }
33
        return self::$_instance;
34
    }
35
36
    /**
37
     * FileManager constructor.
38
     */
39
    public function __construct()
40
    {
41
        System::getInstance();
0 ignored issues
show
Bug introduced by
The type NDC\DatabaseBackup\System was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
        $this->params = [
43
            'files_path' => env('FILES_PATH_TO_SAVE_BACKUP', './Backups'),
44
            'days_interval' => env('FILES_DAYS_HISTORY', 3)
45
        ];
46
        $this->_getFiles();
47
        env('FILES_DAYS_HISTORY', 3) > 0 ?: $this->removeOldFilesByIntervalDays();
48
    }
49
50
    /**
51
     * @return void
52
     */
53
    private function _getFiles(): void
54
    {
55
        $files = scandir($this->params['files_path'], SCANDIR_SORT_ASCENDING);
56
        $this->files = array_filter($files, function ($k) {
57
            $return = [];
58
            if ($k !== '..' && $k !== '.') {
59
                $return[] = $k;
60
            }
61
            return $return;
62
        });
63
    }
64
65
    /**
66
     * @return void
67
     */
68
    private function removeOldFilesByIntervalDays(): void
69
    {
70
        $time_before = time() - $this->params['days_interval'] * 86400;
71
        foreach ($this->files as $file) {
72
            $f = explode('-', $file);
73
            $f = str_replace('.sql', '', $f);
74
            if ($f[1] < $time_before) {
75
                @unlink($this->params['files_path'] . '/' . $file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

75
                /** @scrutinizer ignore-unhandled */ @unlink($this->params['files_path'] . '/' . $file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
76
            }
77
        }
78
    }
79
}
80