Completed
Push — master ( ccde76...68a462 )
by Sebastian
12s
created

Local::getBackupFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
namespace phpbu\App\Backup\Collector;
3
4
use DirectoryIterator;
5
use phpbu\App\Backup\Collector;
6
use phpbu\App\Backup\File\Local as FileLocal;
7
use phpbu\App\Backup\Target;
8
use SplFileInfo;
9
use phpbu\App\Util\Arr;
10
use phpbu\App\Util\Str;
11
12
/**
13
 * Collector
14
 *
15
 * @package    phpbu
16
 * @subpackage Backup
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       http://phpbu.de/
21
 * @since      Class available since Release 1.0.0
22
 */
23
class Local extends Collector
24
{
25
    /**
26
     * Backup target
27
     *
28
     * @var \phpbu\App\Backup\Target
29
     */
30
    protected $target;
31
32
    /**
33
     * Target filename regex
34
     *
35
     * @var string
36
     */
37
    protected $fileRegex;
38
39
    /**
40
     * Collection cache
41
     *
42
     * @var \phpbu\App\Backup\File\Local[]
43
     */
44
    protected $files;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param \phpbu\App\Backup\Target $target
50
     */
51 22
    public function __construct(Target $target)
52
    {
53 22
        $this->target = $target;
54 22
    }
55
56
    /**
57
     * Get all created backups.
58
     *
59
     * @return \phpbu\App\Backup\File\Local[]
60
     */
61 9
    public function getBackupFiles() : array
62
    {
63 9
        if (null === $this->files) {
64
            // create regex to match only created backup files
65 9
            $this->fileRegex = Str::datePlaceholdersToRegex($this->target->getFilenameRaw());
66 9
            $this->files     = [];
67
            // collect all matching backup files
68 9
            $this->collect($this->target->getPathThatIsNotChanging(), 0);
69
        }
70 9
        return $this->files;
71
    }
72
73
    /**
74
     * Recursive backup collecting.
75
     *
76
     * @param string $path
77
     * @param int    $depth
78
     */
79 9
    protected function collect(string $path, int $depth)
80
    {
81 9
        $dirIterator = new DirectoryIterator($path);
82
        // collect all matching sub directories and get all the backup files
83 9
        if ($depth < $this->target->countChangingPathElements()) {
84 5
            foreach ($dirIterator as $file) {
85 5
                if ($file->isDot()) {
86 5
                    continue;
87
                }
88 5
                if ($this->isValidDirectory($file, $depth)) {
89 5
                    $this->collect($file->getPathname(), $depth + 1);
90
                }
91
            }
92
        } else {
93
            /** @var \phpbu\App\Backup\File\Local $file */
94 9
            $this->collectFiles($dirIterator);
95
        }
96 9
    }
97
98
    /**
99
     * Collect backup files in directory.
100
     *
101
     * @param \DirectoryIterator $dirIterator
102
     */
103 9
    protected function collectFiles(DirectoryIterator $dirIterator)
104
    {
105 9
        foreach ($dirIterator as $i => $file) {
106 9
            if ($file->isDir()) {
107 9
                continue;
108
            }
109
            // skip currently created backup
110 9
            if ($file->getPathname() == $this->target->getPathname()) {
111 3
                continue;
112
            }
113 9
            if ($this->isFilenameMatch($file->getFilename())) {
114 9
                $index               = date('YmdHis', $file->getMTime()) . '-' . $i . '-' . $file->getPathname();
115 9
                $this->files[$index] = new FileLocal($file->getFileInfo());
116
            }
117
        }
118 9
    }
119
120
    /**
121
     * Check if the iterated file is part of a valid target path.
122
     *
123
     * @param  \SplFileInfo $file
124
     * @param  int          $depth
125
     * @return bool
126
     */
127 5
    protected function isValidDirectory(SplFileInfo $file, int $depth)
128
    {
129 5
        return $file->isDir() && $this->isMatchingDirectory($file->getBasename(), $depth);
130
    }
131
132
    /**
133
     * Does a directory match the respective target path.
134
     *
135
     * @param  string $dir
136
     * @param  int    $depth
137
     * @return bool
138
     */
139 5
    protected function isMatchingDirectory(string $dir, int $depth)
140
    {
141 5
        $dirTarget = Arr::getValue($this->target->getChangingPathElements(), $depth);
142 5
        $dirRegex  = Str::datePlaceholdersToRegex($dirTarget);
143 5
        return preg_match('#' . $dirRegex . '#i', $dir);
144
    }
145
}
146