Completed
Pull Request — master (#145)
by Vitaly
04:36
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
    use Path;
21
22
    /**
23
     * Backup target
24
     *
25
     * @var \phpbu\App\Backup\Target
26
     */
27
    protected $target;
28
29
    /**
30
     * Target filename regex
31
     *
32
     * @var string
33
     */
34
    protected $fileRegex;
35
36
    /**
37
     * Collection cache
38
     *
39
     * @var \phpbu\App\Backup\File[]
40
     */
41
    protected $files;
42
43
    /**
44
     * Setting up
45
     *
46
     * @param \phpbu\App\Backup\Target $target
47
     */
48 4
    public function setUp(Target $target)
49
    {
50 4
        $this->target    = $target;
51 4
        $this->fileRegex = Util\Path::datePlaceholdersToRegex($target->getFilenameRaw());
52 4
        $this->files     = [];
53 4
    }
54
55
    /**
56
     * Return true if target full path matches file and path regex.
57
     *
58
     * @param string $targetPath Full path to the remote file to check
59
     * @return bool
60
     */
61 1
    protected function isFileMatch(string $targetPath): bool
62
    {
63 1
        $rawPath = Util\Path::withoutLeadingSlash($this->pathRaw);
64 1
        $pathRegex = Util\Path::datePlaceholdersToRegex($rawPath);
65 1
        $pathRegex .= $pathRegex ? '/' : '';
66 1
        $fileRegex = Util\Path::datePlaceholdersToRegex($this->target->getFilenameRaw());
67 1
        return preg_match('#' . $pathRegex . $fileRegex . '$#i', $targetPath);
68
    }
69
70
    /**
71
     * Returns true if filename matches the target regex
72
     *
73
     * @param string $filename
74
     * @return bool
75
     */
76 13
    protected function isFilenameMatch(string $filename): bool
77
    {
78 13
        return preg_match('#' . $this->fileRegex . '#i', $filename);
79
    }
80
81
    /**
82
     * Get all created backups.
83
     *
84
     * @return \phpbu\App\Backup\File[]
85
     */
86
    abstract public function getBackupFiles() : array;
87
}
88