Completed
Push — master ( f8aac3...6e7b55 )
by Sebastian
07:15
created

Capacity::getDeletableBackups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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