Completed
Pull Request — master (#140)
by
unknown
03:00
created

Clearable::setUpClearable()   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
dl 0
loc 17
ccs 7
cts 10
cp 0.7
rs 8.8571
c 0
b 0
f 0
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\Cleaner;
5
use phpbu\App\Configuration\Backup\Cleanup;
6
use phpbu\App\Factory;
7
8
trait Clearable
9
{
10
    /**
11
     * @var Cleanup
12
     */
13
    protected $cleanupConfig;
14
15
    /**
16
     * @var Cleaner
17
     */
18
    protected $cleaner;
19
20
    /**
21
     * Check sync clean configuration entities and set up a proper cleaner
22
     *
23
     * @param array $options
24
     * @throws \phpbu\App\Exception
25
     */
26 9
    public function setUpClearable(array $options)
27
    {
28 9
        $config = [];
29 9
        foreach ($options as $key => $value) {
30 9
            if (strpos($key, "cleanup.") === 0) {
31 9
                $config[str_replace('cleanup.', '', $key)] = $value;
32
            }
33
        }
34
35 9
        if (isset($config['type'])) {
36
            $skip = isset($config['skipOnFailure']) ? (bool) $config['skipOnFailure'] : true;
37
            // creating cleanup config
38
            $this->cleanupConfig = new Cleanup($config['type'], $skip, $config);
39
            // creating cleaner
40
            $this->cleaner = (new Factory())->createCleaner($this->cleanupConfig->type, $this->cleanupConfig->options);
41
        }
42 9
    }
43
44
    /**
45
     * @return string
46
     */
47 5
    public function getSimulateInfo(): string
48
    {
49 5
        if ($this->cleaner) {
50
            return "  sync cleanup: {$this->cleanupConfig->type}" . PHP_EOL;
51
        }
52
53 5
        return '';
54
    }
55
}