Completed
Push — master ( 4f1d49...8186c0 )
by Sebastian
07:15
created

Capacity   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 12
c 3
b 0
f 1
lcom 1
cbo 7
dl 0
loc 101
rs 10
ccs 33
cts 33
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 16 4
A simulate() 0 6 1
C getFilesToDelete() 0 31 7
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\Result;
7
use phpbu\App\Util\Str;
8
use RuntimeException;
9
10
/**
11
 * Cleanup backup directory.
12
 *
13
 * Removes oldest backup till the given capacity isn't exceeded anymore.
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 Capacity extends Abstraction implements Simulator
24
{
25
    /**
26
     * Original XML value
27
     *
28
     * @var string
29
     */
30
    protected $capacityRaw;
31
32
    /**
33
     * Capacity in bytes.
34
     *
35
     * @var mixed <integer|double>
36
     */
37
    protected $capacityBytes;
38
39
    /**
40
     * Delete current backup as well
41
     *
42
     * @var boolean
43
     */
44
    protected $deleteTarget;
45
46
    /**
47
     * Setup the the Cleaner.
48
     *
49
     * @see    \phpbu\App\Backup\Cleanup::setup()
50
     * @param  array $options
51
     * @throws \phpbu\App\Backup\Cleaner\Exception
52
     */
53
    public function setup(array $options)
54 7
    {
55
        if (!isset($options['size'])) {
56 7
            throw new Exception('option \'size\' is missing');
57 1
        }
58
        try {
59
            $bytes = Str::toBytes($options['size']);
60 6
        } catch (RuntimeException $e) {
61 6
            throw new Exception($e->getMessage());
62 1
        }
63
        $this->deleteTarget  = isset($options['deleteTarget'])
64 5
                             ? Str::toBoolean($options['deleteTarget'], false)
65 5
                             : false;
66 5
        $this->capacityRaw   = $options['size'];
67 5
        $this->capacityBytes = $bytes;
68 5
    }
69 5
70
    /**
71
     * Simulate the cleanup execution.
72
     *
73
     * @param \phpbu\App\Backup\Target    $target
74
     * @param \phpbu\App\Backup\Collector $collector
75
     * @param \phpbu\App\Result           $result
76
     */
77
    public function simulate(Target $target, Collector $collector, Result $result)
78
    {
79
        $target->setSize('20000000');
80 4
        $result->debug('assuming backup size 20MB');
81
        parent::simulate($target, $collector, $result);
82 4
    }
83 4
84
    /**
85
     * Return list of files to delete.
86 4
     *
87 4
     * @param  \phpbu\App\Backup\Target    $target
88 4
     * @param  \phpbu\App\Backup\Collector $collector
89
     * @return \phpbu\App\Backup\File[]
90
     * @throws \phpbu\App\Exception
91 4
     */
92
    protected function getFilesToDelete(Target $target, Collector $collector)
93 3
    {
94
        $files  = $collector->getBackupFiles();
95 3
        $size   = $target->getSize();
96 3
        $delete = [];
97 3
98 3
        /** @var \phpbu\App\Backup\File $file */
99 1
        foreach ($files as $file) {
100
            $size += $file->getSize();
101 2
        }
102 2
103 2
        // backups exceed capacity?
104
        if ($size > $this->capacityBytes) {
105
            // oldest backups first
106
            ksort($files);
107 2
108 1
            while ($size > $this->capacityBytes && count($files) > 0) {
109 1
                $file     = array_shift($files);
110 2
                $size    -= $file->getSize();
111 3
                $delete[] = $file;
112
            }
113
114
            // deleted all old backups but still exceeding the space limit
115
            // delete the currently created backup as well
116
            if ($this->deleteTarget && $size > $this->capacityBytes) {
117
                $delete[] = $target->toFile();
118
            }
119
        }
120
121
        return $delete;
122
    }
123
}
124