Completed
Push — master ( 2c9ca9...7241ae )
by Sebastian
08:02
created

Collector::isMatchingDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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