Completed
Pull Request — master (#145)
by Vitaly
03:53
created

Collector::isFilenameMatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
     * Backup target
22
     *
23
     * @var \phpbu\App\Backup\Target
24
     */
25
    protected $target;
26
27
    /**
28
     * Target filename regex
29
     *
30
     * @var string
31
     */
32
    protected $fileRegex;
33
34
    /**
35
     * Collection cache
36
     *
37
     * @var \phpbu\App\Backup\File[]
38
     */
39
    protected $files;
40
41
    /**
42
     * Setting up
43
     *
44
     * @param \phpbu\App\Backup\Target $target
45
     */
46 4
    public function setUp(Target $target)
47
    {
48 4
        $this->target    = $target;
49 4
        $this->fileRegex = Util\Path::datePlaceholdersToRegex($target->getFilenameRaw());
50 4
        $this->files     = [];
51 4
    }
52
53
    /**
54
     * Return true if target full path matches file and path regex.
55
     *
56
     * @param string $targetPath
57
     * @param string $rawPath
58
     * @return bool
59
     */
60 1
    protected function isFileMatch(string $targetPath, string $rawPath): bool
61
    {
62 1
        $pathRegex = Util\Path::datePlaceholdersToRegex($rawPath);
63 1
        $pathRegex .= $pathRegex ? '/' : '';
64 1
        $fileRegex = Util\Path::datePlaceholdersToRegex($this->target->getFilenameRaw());
65 1
        return preg_match('#'.$pathRegex . $fileRegex . '$#i', $targetPath);
66
    }
67
68
    /**
69
     * Returns true if filename matches the target regex
70
     *
71
     * @param string $filename
72
     * @return bool
73
     */
74 13
    protected function isFilenameMatch(string $filename): bool
75
    {
76 13
        return preg_match('#'.$this->fileRegex . '#i', $filename);
77
    }
78
79
    /**
80
     * Get all created backups.
81
     *
82
     * @return \phpbu\App\Backup\File[]
83
     */
84
    abstract public function getBackupFiles() : array;
85
}
86