Completed
Pull Request — master (#145)
by Vitaly
04:36
created

Collector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 70
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A isFileMatch() 0 8 2
A isFilenameMatch() 0 4 1
getBackupFiles() 0 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
    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