Completed
Pull Request — master (#145)
by Vitaly
02:41
created

Collector::isFileMatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
namespace phpbu\App\Backup;
3
4
use phpbu\App\Util;
5
6
/**
7
 * Collector class.
8
 *
9
 * @package    phpbu
10
 * @subpackage Backup
11
 * @author     Sebastian Feldmann <[email protected]>
12
 * @author     Vitaly Baev <[email protected]>
13
 * @copyright  Sebastian Feldmann <[email protected]>
14
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
15
 * @link       http://phpbu.de/
16
 * @since      Class available since Release 5.1.0
17
 */
18
abstract class Collector
19
{
20
    /**
21
     * Absolute path to the directory where to store the backup.
22
     *
23
     * @var string
24
     */
25
    protected $path;
26
27
    /**
28
     * Path to the backup with potential date placeholders like %d.
29
     *
30
     * @var string
31
     */
32
    protected $pathRaw;
33
34
    /**
35
     * Part of the path without placeholders
36
     *
37
     * @var string
38
     */
39
    protected $pathNotChanging;
40
41
    /**
42
     * Backup target
43
     *
44
     * @var \phpbu\App\Backup\Target
45
     */
46
    protected $target;
47
48
    /**
49
     * Target filename regex
50
     *
51
     * @var string
52
     */
53
    protected $fileRegex;
54
55
    /**
56
     * Collection cache
57
     *
58
     * @var \phpbu\App\Backup\File[]
59
     */
60
    protected $files;
61
62
    /**
63
     * Setting up
64
     *
65
     * @param \phpbu\App\Backup\Target $target
66
     */
67 4
    public function setUp(Target $target)
68
    {
69 4
        $this->target    = $target;
70 4
        $this->fileRegex = Util\Path::datePlaceholdersToRegex($target->getFilenameRaw());
71 4
        $this->files     = [];
72 4
    }
73
74
    /**
75
     * Return true if target full path matches file and path regex.
76
     *
77
     * @param string $targetPath Full path to the remote file to check
78
     * @return bool
79
     */
80 1
    protected function isFileMatch(string $targetPath): bool
81
    {
82 1
        $rawPath = Util\Path::withoutLeadingSlash($this->pathRaw);
83 1
        $pathRegex = Util\Path::datePlaceholdersToRegex($rawPath);
84 1
        $pathRegex .= $pathRegex ? '/' : '';
85 1
        $fileRegex = Util\Path::datePlaceholdersToRegex($this->target->getFilenameRaw());
86 1
        return preg_match('#' . $pathRegex . $fileRegex . '$#i', $targetPath);
87
    }
88
89
    /**
90
     * Returns true if filename matches the target regex
91
     *
92
     * @param string $filename
93
     * @return bool
94
     */
95 13
    protected function isFilenameMatch(string $filename): bool
96
    {
97 13
        return preg_match('#' . $this->fileRegex . '#i', $filename);
98
    }
99
100
    /**
101
     * Directory setter.
102
     *
103
     * @param  string $path
104
     * @param  int    $time
105
     */
106 1 View Code Duplication
    protected function setPath($path, $time = null)
107
    {
108
        // remove trailing slashes
109 1
        $path                  = rtrim($path, DIRECTORY_SEPARATOR);
110 1
        $this->pathRaw         = $path;
111 1
        $this->pathNotChanging = $path;
112
113 1
        if (Util\Path::isContainingPlaceholder($path)) {
114
            $this->detectPathNotChanging($path);
115
            // replace potential date placeholder
116
            $path = Util\Path::replaceDatePlaceholders($path, $time);
117
        }
118
119 1
        $this->path = $path;
120 1
    }
121
122
    /**
123
     * Find path elements that can't change because of placeholder usage.
124
     *
125
     * @param string $path
126
     */
127 View Code Duplication
    protected function detectPathNotChanging(string $path)
128
    {
129
        $partsNotChanging     = [];
130
        $foundChangingElement = false;
131
132
        foreach (Util\Path::getDirectoryListFromAbsolutePath($path) as $depth => $dir) {
133
            // already found placeholder or found one right now
134
            // path isn't static anymore so don't add directory to path not changing
135
            if ($foundChangingElement || Util\Path::isContainingPlaceholder($dir)) {
136
                $foundChangingElement = true;
137
                continue;
138
            }
139
            // do not add the / element leading slash will be re-added later
140
            if ($dir !== '/') {
141
                $partsNotChanging[] = $dir;
142
            }
143
        }
144
        $this->pathNotChanging = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $partsNotChanging);
145
    }
146
147
    /**
148
     * Get all created backups.
149
     *
150
     * @return \phpbu\App\Backup\File[]
151
     */
152
    abstract public function getBackupFiles() : array;
153
}
154