1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Check; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Result; |
5
|
|
|
use phpbu\App\Backup\Collector; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
use phpbu\App\Util\Math; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* SizeDiffPreviousPercent class |
11
|
|
|
* |
12
|
|
|
* Checks if a backup filesize differs more than a given percent value as compared to the previous backup. |
13
|
|
|
* If no previous backup exists this check will pass. |
14
|
|
|
* |
15
|
|
|
* @package phpbu |
16
|
|
|
* @subpackage Backup |
17
|
|
|
* @author Sebastian Feldmann <[email protected]> |
18
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
20
|
|
|
* @link http://phpbu.de/ |
21
|
|
|
* @since Class available since Release 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class SizeDiffPreviousPercent implements Simulator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Execute check. |
27
|
|
|
* |
28
|
|
|
* @param \phpbu\App\Backup\Target $target |
29
|
|
|
* @param string $value |
30
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
31
|
|
|
* @param \phpbu\App\Result $result |
32
|
|
|
* @return bool |
33
|
|
|
* @throws \phpbu\App\Exception |
34
|
|
|
*/ |
35
|
2 |
|
public function pass(Target $target, $value, Collector $collector, Result $result) : bool |
36
|
|
|
{ |
37
|
|
|
// throws App\Exception if file doesn't exist |
38
|
2 |
|
$backupSize = $target->getSize(); |
39
|
2 |
|
$history = $collector->getBackupFiles(); |
40
|
2 |
|
$historyCount = count($history); |
41
|
2 |
|
$pass = true; |
42
|
|
|
|
43
|
2 |
|
if ($historyCount > 0) { |
44
|
|
|
// oldest backups first |
45
|
2 |
|
ksort($history); |
46
|
2 |
|
$prevFile = array_pop($history); |
47
|
2 |
|
$prevSize = $prevFile->getSize(); |
48
|
2 |
|
$diffPercent = Math::getDiffInPercent($backupSize, $prevSize); |
49
|
|
|
|
50
|
2 |
|
$pass = $diffPercent < $value; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
return $pass; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Simulate the check execution. |
58
|
|
|
* |
59
|
|
|
* @param \phpbu\App\Backup\Target $target |
60
|
|
|
* @param string $value |
61
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
62
|
|
|
* @param \phpbu\App\Result $result |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function simulate(Target $target, $value, Collector $collector, Result $result): bool |
66
|
|
|
{ |
67
|
|
|
$result->debug('checking size difference ' . $value . '%' . PHP_EOL); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|