Capacity::getFilesToDelete()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
nc 6
nop 2
dl 0
loc 31
ccs 13
cts 14
cp 0.9286
crap 6.0131
rs 9.2222
c 1
b 0
f 0
1
<?php
2
namespace phpbu\App\Backup\Cleaner;
3
4
use phpbu\App\Backup\Collector;
5
use phpbu\App\Backup\Target;
6
use phpbu\App\Util\Str;
7
use RuntimeException;
8
9
/**
10
 * Cleanup backup directory.
11
 *
12
 * Removes oldest backup till the given capacity 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 Capacity extends Abstraction implements Simulator
23
{
24
    /**
25
     * Original XML value
26
     *
27
     * @var string
28
     */
29
    protected $capacityRaw;
30
31
    /**
32
     * Capacity in bytes.
33
     *
34
     * @var int|float
35
     */
36
    protected $capacityBytes;
37
38
    /**
39
     * Setup the the Cleaner.
40
     *
41
     * @see    \phpbu\App\Backup\Cleanup::setup()
42
     * @param  array $options
43
     * @throws \phpbu\App\Backup\Cleaner\Exception
44
     */
45
    public function setup(array $options)
46
    {
47
        if (!isset($options['size'])) {
48 9
            throw new Exception('option \'size\' is missing');
49
        }
50 9
        try {
51 1
            $bytes = Str::toBytes($options['size']);
52
        } catch (RuntimeException $e) {
53
            throw new Exception($e->getMessage());
54 8
        }
55 1
        $this->capacityRaw   = $options['size'];
56 1
        $this->capacityBytes = $bytes;
57
    }
58 7
59 7
    /**
60 7
     * Return list of files to delete.
61
     *
62
     * @param  \phpbu\App\Backup\Target    $target
63
     * @param  \phpbu\App\Backup\Collector $collector
64
     * @return \phpbu\App\Backup\File[]
65
     * @throws \phpbu\App\Exception
66
     */
67
    protected function getFilesToDelete(Target $target, Collector $collector)
68
    {
69
        $files  = $collector->getBackupFiles();
70 6
        $size   = $target->getSize();
71
        $delete = [];
72 6
73 6
74 6
75
        // sum up the size of all backups
76
        /** @var \phpbu\App\Backup\File $file */
77
        foreach ($files as $file) {
78
            $size += $file->getSize();
79
        }
80 6
81 6
        // check if backups exceed capacity?
82
        if ($this->isCapacityExceeded($size)) {
83
            // sort backups by date, oldest first, key 'YYYYMMDDHHIISS-NR-PATH'
84
            ksort($files);
85 6
86
            while ($this->isCapacityExceeded($size) && count($files) > 0) {
87 5
                // get oldest backup from list, move it to delete list
88
                $file = array_shift($files);
89 5
                if ($file === null) {
90
                    break;
91 5
                }
92 5
                $size -= $file->getSize();
93
                $delete[] = $file;
94
            }
95 5
        }
96 5
97
        return $delete;
98
    }
99
100 6
    /**
101
     * Is a given size bigger than the configured capacity limit.
102
     *
103
     * @param  int|double $currentCapacity
104
     * @return bool
105
     */
106
    protected function isCapacityExceeded($currentCapacity): bool
107
    {
108
        return $currentCapacity > $this->capacityBytes;
109 6
    }
110
}
111