1 | <?php |
||
2 | namespace phpbu\App\Backup\Collector; |
||
3 | |||
4 | use phpbu\App\Backup\File; |
||
5 | |||
6 | /** |
||
7 | * Abstraction 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 Abstraction |
||
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 | * Indicates if current execution is a simulation. |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $isSimulation = false; |
||
47 | |||
48 | /** |
||
49 | * Index count for collected backups. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $index = 0; |
||
54 | |||
55 | /** |
||
56 | * Indicate that this is a simulation and make sure the collector includes a fake target. |
||
57 | * |
||
58 | * @param bool $isSimulation |
||
59 | */ |
||
60 | public function setSimulation(bool $isSimulation) |
||
61 | { |
||
62 | 8 | $this->isSimulation = $isSimulation; |
|
63 | } |
||
64 | 8 | ||
65 | 8 | /** |
|
66 | * Get all created backups. |
||
67 | * |
||
68 | * @return \phpbu\App\Backup\File[] |
||
69 | */ |
||
70 | public function getBackupFiles() : array |
||
71 | { |
||
72 | 30 | if (null === $this->files) { |
|
73 | $this->files = []; |
||
74 | 30 | $this->collectBackups(); |
|
75 | 30 | // if current run is a simulation |
|
76 | 30 | // add a fake target to the collected files |
|
77 | if ($this->isSimulation) { |
||
78 | $file = new File\Simulation( |
||
79 | 30 | time(), |
|
80 | 1 | 20000000, |
|
81 | 1 | $this->target->getPath()->getPath(), |
|
82 | 1 | $this->target->getFilename() |
|
83 | 1 | ); |
|
84 | 1 | $this->files[$this->getFileIndex($file)] = $file; |
|
85 | } |
||
86 | 1 | } |
|
87 | return $this->files; |
||
88 | } |
||
89 | 30 | ||
90 | /** |
||
91 | * Collect all created backups. |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | abstract protected function collectBackups(); |
||
96 | |||
97 | /** |
||
98 | * Return an array index for a given file for key sorting the list later. |
||
99 | * |
||
100 | * @param \phpbu\App\Backup\File $file |
||
101 | * @return string |
||
102 | */ |
||
103 | protected function getFileIndex(File $file) : string |
||
104 | { |
||
105 | 20 | return $file->getMTime() . '-' . $file->getFilename() . '-' . $this->index++; |
|
106 | } |
||
107 | 20 | ||
108 | /** |
||
109 | * Returns true if filename matches the target regex |
||
110 | * |
||
111 | * @param string $filename |
||
112 | * @return bool |
||
113 | */ |
||
114 | protected function isFilenameMatch(string $filename) : bool |
||
115 | { |
||
116 | 11 | return preg_match('#' . $this->fileRegex . '#i', $filename); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
117 | } |
||
118 | } |
||
119 |