FileManager::_getFiles()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace NDC;
6
7
/**
8
 * Class FileManager
9
 * @package NDC
10
 */
11
class FileManager
12
{
13
    /**
14
     * @var array
15
     */
16
    private array $params;
17
    /**
18
     * @var array
19
     */
20
    private array $files = [];
21
22
    /**
23
     * FileManager constructor.
24
     */
25
    public function __construct()
26
    {
27
        (new System())->loadConfigurationEnvironment();
28
        $this->params = [
29
            'files_path' => $_ENV['FILES_PATH_TO_SAVE_BACKUP'],
30
            'days_interval' => (int)$_ENV['FILES_DAYS_HISTORY']
31
        ];
32
        $this->_getFiles();
33
    }
34
35
    /**
36
     * @return void
37
     */
38
    private function _getFiles(): void
39
    {
40
        $files = scandir($this->params['files_path'], SCANDIR_SORT_ASCENDING);
41
        $this->files = array_filter(
42
            $files,
0 ignored issues
show
Bug introduced by
It seems like $files can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

42
            /** @scrutinizer ignore-type */ $files,
Loading history...
43
            function ($k) {
44
                $return = [];
45
                if ($k !== '..' && $k !== '.') {
46
                    $return[] = $k;
47
                }
48
                return $return;
49
            }
50
        );
51
    }
52
53
    /**
54
     * @return void
55
     */
56
    public function removeOldFilesByIntervalDays(): void
57
    {
58
        $time_before = time() - $this->params['days_interval'] * 86400;
59
        foreach ($this->files as $file) {
60
            $f = explode('-', $file);
61
            $f = str_replace('.sql', '', $f);
62
            if ($f[1] < $time_before) {
63
                @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

63
                /** @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...
64
            }
65
        }
66
    }
67
}
68