Completed
Pull Request — master (#145)
by Vitaly
06:17
created

Quantity::isCapacityExceeded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
namespace phpbu\App\Backup\Cleaner;
3
4
use phpbu\App\Backup\Cleaner;
5
use phpbu\App\Backup\Collector;
6
use phpbu\App\Backup\File;
7
use phpbu\App\Backup\Target;
8
9
/**
10
 * Cleanup backup directory.
11
 *
12
 * Removes oldest backup till the given quantity isn't exceeded anymore.
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       http://phpbu.de/
20
 * @since      Class available since Release 1.0.0
21
 */
22
class Quantity extends Abstraction implements Cleaner
23
{
24
    /**
25
     * Amount of backups to keep
26
     *
27
     * @var integer
28
     */
29
    protected $amount;
30
31
    /**
32
     * Setup the Cleaner.
33
     *
34
     * @see    \phpbu\App\Backup\Cleanup::setup()
35
     * @param  array $options
36
     * @throws \phpbu\App\Backup\Cleaner\Exception
37 7
     */
38
    public function setup(array $options)
39 7
    {
40 1
        if (!isset($options['amount'])) {
41
            throw new Exception('option \'amount\' is missing');
42 6
        }
43 1
        if (!is_numeric($options['amount'])) {
44
            throw new Exception(sprintf('invalid value for \'amount\': %s', $options['amount']));
45 5
        }
46 1
        if ($options['amount'] < 1) {
47
            throw new Exception(sprintf('value for \'amount\' must be greater 0, %s given', $options['amount']));
48 4
        }
49 4
        $this->amount = intval($options['amount']);
50
    }
51
52
    /**
53
     * Return list of files to delete.
54
     *
55
     * @param  \phpbu\App\Backup\Target    $target
56
     * @param  \phpbu\App\Backup\Collector $collector
57
     * @return \phpbu\App\Backup\File\Local[]
58
     * @throws \phpbu\App\Exception
59 4
     */
60
    protected function getFilesToDelete(Target $target, Collector $collector)
61 4
    {
62 4
        $files  = $collector->getBackupFiles();
63
        $delete = [];
64 4
65
        if ($this->isCapacityExceeded($files)) {
66 3
            // oldest backups first
67
            uasort($files, function (File $fileA, File $fileB) {
68 3
                if ($fileA->getMTime() == $fileB->getMTime()) {
69 3
                    return 0;
70 3
                }
71
                return ($fileA->getMTime() < $fileB->getMTime()) ? -1 : 1;
72
            });
73
74 4
            while ($this->isCapacityExceeded($files)) {
75
                $file     = array_shift($files);
76
                $delete[] = $file;
77
            }
78
        }
79
80
        return $delete;
81
    }
82
83 4
    /**
84
     * Returns true when the capacity is exceeded.
85 4
     *
86 4
     * @param  array
87
     * @return boolean
88 4
     */
89 4
    private function isCapacityExceeded(array $files)
90
    {
91
        $totalFiles                  = count($files);
92
        $totalFilesPlusCurrentBackup = $totalFiles + 1;
93
94
        return $totalFiles > 0
95
               && $totalFilesPlusCurrentBackup > $this->amount;
96
    }
97
}
98