Completed
Pull Request — master (#140)
by
unknown
05:29
created

Clearable::getSimulateInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
    public function setUpClearable(array $options)
27
    {
28
        $config = [];
29
        foreach ($options as $key => $value) {
30
            if (strpos($key, "cleanup.") === 0) {
31
                $config[str_replace('cleanup.', '', $key)] = $value;
32
            }
33
        }
34
35
        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
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getSimulateInfo(): string
48
    {
49
        if ($this->cleaner) {
50
            return "  sync cleanup: {$this->cleanupConfig->type}" . PHP_EOL;
51
        }
52
53
        return '';
54
    }
55
}