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

Clearable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 48
ccs 10
cts 14
cp 0.7143
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B setUpClearable() 0 17 5
A getSimulateInfo() 0 8 2
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
}