Completed
Push — master ( fc5b3c...82a55c )
by Sebastian
03:07
created

SizeDiffPreviousPercent::simulate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
crap 1
1
<?php
2
namespace phpbu\App\Backup\Check;
3
4
use phpbu\App\Result;
5
use phpbu\App\Backup\Collector\Local;
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\Local $collector
31
     * @param  \phpbu\App\Result                 $result
32
     * @return bool
33
     * @throws \phpbu\App\Exception
34
     */
35 2
    public function pass(Target $target, $value, Local $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\Local $collector
62
     * @param  \phpbu\App\Result                 $result
63
     * @return bool
64
     */
65 1
    public function simulate(Target $target, $value, Local $collector, Result $result): bool
66
    {
67 1
        $result->debug('checking size difference ' . $value . '%' . PHP_EOL);
68 1
        return true;
69
    }
70
}
71