Completed
Pull Request — master (#140)
by
unknown
10:09 queued 03:44
created

Clearable::createCollector()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Backup\Cleaner;
5
use phpbu\App\Backup\Collector;
6
use phpbu\App\Backup\Target;
7
use phpbu\App\Configuration\Backup\Cleanup;
8
use phpbu\App\Factory;
9
use phpbu\App\Result;
10
11
/**
12
 * Clearable trait
13
 *
14
 * @package    phpbu
15
 * @subpackage Sync
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @author     Vitaly Baev <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       http://phpbu.de/
21
 * @since      Class available since Release 5.1.0
22
 */
23
trait Clearable
24
{
25
    /**
26
     * @var Cleanup
27
     */
28
    protected $cleanupConfig;
29
30
    /**
31
     * @var Cleaner
32
     */
33
    protected $cleaner;
34
35
    /**
36
     * Check sync clean configuration entities and set up a proper cleaner
37
     *
38
     * @param array $options
39
     * @throws \phpbu\App\Exception
40
     */
41 16
    public function setUpClearable(array $options)
42
    {
43 16
        $config = [];
44 16
        foreach ($options as $key => $value) {
45 16
            if (strpos($key, "cleanup.") === 0) {
46 16
                $config[str_replace('cleanup.', '', $key)] = $value;
47
            }
48
        }
49
50 16
        if (isset($config['type'])) {
51
            $skip = isset($config['skipOnFailure']) ? (bool) $config['skipOnFailure'] : true;
52
            // creating cleanup config
53
            $this->cleanupConfig = new Cleanup($config['type'], $skip, $config);
54
            // creating cleaner
55
            $this->cleaner = (new Factory())->createCleaner($this->cleanupConfig->type, $this->cleanupConfig->options);
56
        }
57 16
    }
58
59
    /**
60
     * Creates collector for remote cleanup
61
     *
62
     * @param Target $target
63
     * @return Collector
64
     */
65
    abstract protected function createCollector(Target $target): Collector;
66
67
    /**
68
     * Execute the remote clean up if needed
69
     *
70
     * @param \phpbu\App\Backup\Target $target
71
     * @param \phpbu\App\Result        $result
72
     */
73 1
    public function cleanup(Target $target, Result $result)
74
    {
75 1
        if (!$this->cleaner) {
76 1
            return;
77
        }
78
79
        $collector = $this->createCollector($target);
80
        $this->cleaner->cleanup($target, $collector, $result);
81
    }
82
83
    /**
84
     * Simulate remote cleanup.
85
     *
86
     * @param Target $target
87
     * @param Result $result
88
     */
89 6
    public function simulateRemoteCleanup(Target $target, Result $result)
90
    {
91 6
        if ($this->cleaner) {
92
            $result->debug("  sync cleanup: {$this->cleanupConfig->type}" . PHP_EOL);
93
        }
94 6
    }
95
}
96