Abstraction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 54
ccs 12
cts 12
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A simulate() 0 4 2
A cleanup() 0 9 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
 * Cleaner Abstraction
10
 *
11
 * @package    phpbu
12
 * @subpackage Backup
13
 * @author     Sebastian Feldmann <[email protected]>
14
 * @copyright  Sebastian Feldmann <[email protected]>
15
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
16
 * @link       http://phpbu.de/
17
 * @since      Class available since Release 3.0.0
18
 */
19
abstract class Abstraction
20
{
21
    /**
22
     * Backup Result to handle events and IO
23
     *
24
     * @var \phpbu\App\Result
25
     */
26
    protected $result;
27
28
    /**
29
     * Cleanup your backup directory
30
     *
31
     * @see    \phpbu\App\Backup\Cleanup::cleanup()
32
     * @param  \phpbu\App\Backup\Target    $target
33
     * @param  \phpbu\App\Backup\Collector $collector
34
     * @param  \phpbu\App\Result           $result
35
     * @throws \phpbu\App\Exception
36
     */
37
    public function cleanup(Target $target, Collector $collector, Result $result)
38 21
    {
39
        $this->result = $result;
40 21
        foreach ($this->getFilesToDelete($target, $collector) as $file) {
41 21
            if (!$file->isWritable()) {
42 9
                throw new Exception(sprintf('can\'t delete file: %s', $file->getPathname()));
43 3
            }
44
            $result->debug(sprintf('delete %s', $file->getPathname()));
45 6
            $file->unlink();
46 6
        }
47
    }
48 17
49
    /**
50
     * Simulate the cleanup execution
51
     *
52
     * @param  \phpbu\App\Backup\Target    $target
53
     * @param  \phpbu\App\Backup\Collector $collector
54
     * @param  \phpbu\App\Result           $result
55
     * @throws \phpbu\App\Exception
56
     */
57
    public function simulate(Target $target, Collector $collector, Result $result)
58 2
    {
59
        foreach ($this->getFilesToDelete($target, $collector) as $file) {
60 2
            $result->debug(sprintf('delete %s', $file->getPathname()));
61 2
        }
62
    }
63 2
64
    /**
65
     * Return list of files to delete
66
     *
67
     * @param  \phpbu\App\Backup\Target    $target
68
     * @param  \phpbu\App\Backup\Collector $collector
69
     * @return \phpbu\App\Backup\File[]
70
     * @throws \phpbu\App\Exception
71
     */
72
    abstract protected function getFilesToDelete(Target $target, Collector $collector);
73
}
74