Completed
Push — master ( 86094f...5707a0 )
by Sebastian
03:08
created

Cleanable::setUpCleanable()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 11
cp 0.6364
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 6
nop 1
crap 4.7691
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Backup\Collector;
5
use phpbu\App\Backup\Target;
6
use phpbu\App\Configuration\Backup\Cleanup;
7
use phpbu\App\Factory;
8
use phpbu\App\Result;
9
10
/**
11
 * Clearable trait
12
 *
13
 * @package    phpbu
14
 * @subpackage Sync
15
 * @author     Sebastian Feldmann <[email protected]>
16
 * @author     Vitaly Baev <[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 5.1.0
21
 */
22
trait Cleanable
23
{
24
    /**
25
     * Cleaner configuration.
26
     *
27
     * @var \phpbu\App\Configuration\Backup\Cleanup
28
     */
29
    protected $cleanupConfig;
30
31
    /**
32
     * The cleaner instance executing the actual cleaning process.
33
     *
34
     * @var \phpbu\App\Backup\Cleaner
35
     */
36
    protected $cleaner;
37
38
    /**
39
     * Simulation indicator.
40
     *
41
     * @var bool
42
     */
43
    protected $isSimulation = false;
44
45
    /**
46
     * Check sync clean configuration entities and set up a proper cleaner
47
     *
48
     * @param  array $options
49
     * @throws \phpbu\App\Exception
50
     */
51 19
    public function setUpCleanable(array $options)
52
    {
53 19
        $config = [];
54 19
        foreach ($options as $key => $value) {
55 19
            if (strpos($key, "cleanup.") === 0) {
56 19
                $config[str_replace('cleanup.', '', $key)] = $value;
57
            }
58
        }
59
60 19
        if (isset($config['type'])) {
61
            $this->cleanupConfig = new Cleanup($config['type'], false, $config);
62
            $this->cleaner       = (new Factory())->createCleaner(
63
                $this->cleanupConfig->type,
64
                $this->cleanupConfig->options
65
            );
66
        }
67 19
    }
68
69
    /**
70
     * Creates collector for remote cleanup
71
     *
72
     * @param Target $target
73
     * @return Collector
74
     */
75
    abstract protected function createCollector(Target $target): Collector;
76
77
    /**
78
     * Execute the remote clean up if needed
79
     *
80
     * @param \phpbu\App\Backup\Target $target
81
     * @param \phpbu\App\Result        $result
82
     */
83 2
    public function cleanup(Target $target, Result $result)
84
    {
85 2
        if (!$this->cleaner) {
86 2
            return;
87
        }
88
89
        $collector = $this->createCollector($target);
90
        $this->cleaner->cleanup($target, $collector, $result);
91
    }
92
93
    /**
94
     * Simulate remote cleanup.
95
     *
96
     * @param Target $target
97
     * @param Result $result
98
     */
99 6
    public function simulateRemoteCleanup(Target $target, Result $result)
100
    {
101 6
        if ($this->cleaner) {
102
            $result->debug("  sync cleanup: {$this->cleanupConfig->type}" . PHP_EOL);
103
        }
104 6
    }
105
}
106