Completed
Push — master ( 8eb905...4f1d49 )
by Sebastian
03:16
created

Quantity   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 71
ccs 27
cts 27
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 13 4
A getFilesToDelete() 0 17 3
A isCapacityExceeded() 0 8 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\Target;
7
use phpbu\App\Result;
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
     */
38 7
    public function setup(array $options)
39
    {
40 7
        if (!isset($options['amount'])) {
41 1
            throw new Exception('option \'amount\' is missing');
42
        }
43 6
        if (!is_numeric($options['amount'])) {
44 1
            throw new Exception(sprintf('invalid value for \'amount\': %s', $options['amount']));
45
        }
46 5
        if ($options['amount'] < 1) {
47 1
            throw new Exception(sprintf('value for \'amount\' must be greater 0, %s given', $options['amount']));
48
        }
49 4
        $this->amount = intval($options['amount']);
50 4
    }
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[]
58
     * @throws \phpbu\App\Exception
59
     */
60
    protected function getFilesToDelete(Target $target, Collector $collector)
61 4
    {
62
        $files  = $collector->getBackupFiles();
63 4
        $delete = [];
64
65 4
        if ($this->isCapacityExceeded($files)) {
66
            // oldest backups first
67 3
            ksort($files);
68
69 3
            while ($this->isCapacityExceeded($files)) {
70 3
                $file     = array_shift($files);
71 3
                $delete[] = $file;
72 3
            }
73 1
        }
74
75 2
        return $delete;
76 2
    }
77 2
78 2
    /**
79 3
     * Returns true when the capacity is exceeded.
80
     *
81
     * @param  array
82
     * @return boolean
83
     */
84
    private function isCapacityExceeded(array $files)
85
    {
86 4
        $totalFiles                  = count($files);
87
        $totalFilesPlusCurrentBackup = $totalFiles + 1;
88 4
89 4
        return $totalFiles > 0
90
               && $totalFilesPlusCurrentBackup > $this->amount;
91
    }
92
}
93