Completed
Push — master ( 5707a0...8ebb66 )
by Sebastian
02:46
created

Cleanable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 85
ccs 21
cts 22
cp 0.9545
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
createCollector() 0 1 ?
A setUpCleanable() 0 17 4
A cleanup() 0 10 2
A simulateRemoteCleanup() 0 6 2
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 22
    public function setUpCleanable(array $options)
52
    {
53 22
        $config = [];
54 22
        foreach ($options as $key => $value) {
55 22
            if (strpos($key, "cleanup.") === 0) {
56 22
                $config[str_replace('cleanup.', '', $key)] = $value;
57
            }
58
        }
59
60 22
        if (isset($config['type'])) {
61 1
            $this->cleanupConfig = new Cleanup($config['type'], false, $config);
62 1
            $this->cleaner       = (new Factory())->createCleaner(
63 1
                $this->cleanupConfig->type,
64 1
                $this->cleanupConfig->options
65
            );
66
        }
67 22
    }
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 4
    public function cleanup(Target $target, Result $result)
84
    {
85 4
        if (!$this->cleaner) {
86 3
            return;
87
        }
88
89 1
        $collector = $this->createCollector($target);
90 1
        $result->debug("  sync cleanup: {$this->cleanupConfig->type}" . PHP_EOL);
91 1
        $this->cleaner->cleanup($target, $collector, $result);
92 1
    }
93
94
    /**
95
     * Simulate remote cleanup.
96
     *
97
     * @param Target $target
98
     * @param Result $result
99
     */
100 6
    public function simulateRemoteCleanup(Target $target, Result $result)
101
    {
102 6
        if ($this->cleaner) {
103
            $result->debug("  sync cleanup: {$this->cleanupConfig->type}" . PHP_EOL);
104
        }
105 6
    }
106
}
107