Completed
Push — master ( 433609...f8aac3 )
by Sebastian
04:23 queued 01:16
created

Cleanable::setUpCleanable()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.675

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 7
cts 10
cp 0.7
rs 8.8571
cc 5
eloc 9
nc 9
nop 1
crap 5.675
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
     * @var \phpbu\App\Configuration\Backup\Cleanup
26
     */
27
    protected $cleanupConfig;
28
29
    /**
30
     * @var \phpbu\App\Backup\Cleaner
31
     */
32
    protected $cleaner;
33
34
    /**
35
     * Check sync clean configuration entities and set up a proper cleaner
36
     *
37
     * @param  array $options
38
     * @throws \phpbu\App\Exception
39
     */
40 17
    public function setUpCleanable(array $options)
41
    {
42 17
        $config = [];
43 17
        foreach ($options as $key => $value) {
44 17
            if (strpos($key, "cleanup.") === 0) {
45 17
                $config[str_replace('cleanup.', '', $key)] = $value;
46
            }
47
        }
48
49 17
        if (isset($config['type'])) {
50
            $skip = isset($config['skipOnFailure']) ? (bool) $config['skipOnFailure'] : true;
51
            // creating cleanup config
52
            $this->cleanupConfig = new Cleanup($config['type'], $skip, $config);
53
            // creating cleaner
54
            $this->cleaner = (new Factory())->createCleaner($this->cleanupConfig->type, $this->cleanupConfig->options);
55
        }
56 17
    }
57
58
    /**
59
     * Creates collector for remote cleanup
60
     *
61
     * @param Target $target
62
     * @return Collector
63
     */
64
    abstract protected function createCollector(Target $target): Collector;
65
66
    /**
67
     * Execute the remote clean up if needed
68
     *
69
     * @param \phpbu\App\Backup\Target $target
70
     * @param \phpbu\App\Result        $result
71
     */
72 1
    public function cleanup(Target $target, Result $result)
73
    {
74 1
        if (!$this->cleaner) {
75 1
            return;
76
        }
77
78
        $collector = $this->createCollector($target);
79
        $this->cleaner->cleanup($target, $collector, $result);
80
    }
81
82
    /**
83
     * Simulate remote cleanup.
84
     *
85
     * @param Target $target
86
     * @param Result $result
87
     */
88 6
    public function simulateRemoteCleanup(Target $target, Result $result)
89
    {
90 6
        if ($this->cleaner) {
91
            $result->debug("  sync cleanup: {$this->cleanupConfig->type}" . PHP_EOL);
92
        }
93 6
    }
94
}
95