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

Abstraction::simulate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 3
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
8
/**
9
 * Cleanup Abstraction.
10
 *
11
 * Removes oldest backup till the given capacity isn't exceeded anymore.
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
15
 * @author     Sebastian Feldmann <[email protected]>
16
 * @copyright  Sebastian Feldmann <[email protected]>
17
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
18
 * @link       http://phpbu.de/
19
 * @since      Class available since Release 1.0.0
20
 */
21
abstract class Abstraction
22
{
23
    /**
24
     * Cleanup your backup directory.
25
     *
26
     * @see    \phpbu\App\Backup\Cleanup::cleanup()
27
     * @param  \phpbu\App\Backup\Target    $target
28
     * @param  \phpbu\App\Backup\Collector $collector
29
     * @param  \phpbu\App\Result           $result
30
     * @throws \phpbu\App\Backup\Cleaner\Exception
31
     */
32
    public function cleanup(Target $target, Collector $collector, Result $result)
33
    {
34
        foreach ($this->getFilesToDelete($target, $collector) as $file) {
35
            if (!$file->isWritable()) {
36
                throw new Exception(sprintf('can\'t delete file: %s', $file->getPathname()));
37
            }
38
            $result->debug(sprintf('delete %s', $file->getPathname()));
39
            $file->unlink();
40
        }
41
    }
42
43
    /**
44
     * Simulate the cleanup execution.
45
     *
46
     * @param \phpbu\App\Backup\Target    $target
47
     * @param \phpbu\App\Backup\Collector $collector
48
     * @param \phpbu\App\Result           $result
49
     */
50
    public function simulate(Target $target, Collector $collector, Result $result)
51
    {
52
        foreach ($this->getFilesToDelete($target, $collector) as $file) {
53
            $result->debug(sprintf('delete %s', $file->getPathname()));
54
        }
55
    }
56
57
    /**
58
     * Return list of files to delete.
59
     *
60
     * @param  \phpbu\App\Backup\Target    $target
61
     * @param  \phpbu\App\Backup\Collector $collector
62
     * @return \phpbu\App\Backup\File[]
63
     * @throws \phpbu\App\Exception
64
     */
65
    abstract protected function getFilesToDelete(Target $target, Collector $collector);
66
}
67