Completed
Pull Request — master (#140)
by
unknown
03:51
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\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 Clearable
23
{
24
    /**
25
     * @var Cleaner
26
     */
27
    protected $cleanupConfig;
28
29
    /**
30
     * @var 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 16
    public function setUpClearable(array $options)
41
    {
42 16
        $config = [];
43 16
        foreach ($options as $key => $value) {
44 16
            if (strpos($key, "cleanup.") === 0) {
45 16
                $config[str_replace('cleanup.', '', $key)] = $value;
46
            }
47
        }
48
49 16
        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);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \phpbu\App\Configura...type'], $skip, $config) of type object<phpbu\App\Configuration\Backup\Cleanup> is incompatible with the declared type object<phpbu\App\Backup\Cleaner> of property $cleanupConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
            // creating cleaner
54
            $this->cleaner = (new Factory())->createCleaner($this->cleanupConfig->type, $this->cleanupConfig->options);
55
        }
56 16
    }
57
58
    /**
59
     * Execute the remote clean up if needed
60
     *
61
     * @param \phpbu\App\Backup\Target $target
62
     * @param \phpbu\App\Result        $result
63
     * @throws \phpbu\App\Exception
64
     */
65 1
    public function cleanup(Target $target, Result $result)
66
    {
67 1
        if (!$this->cleaner) {
68 1
            return;
69
        }
70
71
        if (!method_exists($this, 'createCollector')) {
72
            throw new \phpbu\App\Exception('"createCollector" method must be implemented in Sync class that uses Clearable');
73
        }
74
        $collector = $this->createCollector($target);
0 ignored issues
show
Bug introduced by
It seems like createCollector() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
75
        $this->cleaner->cleanup($target, $collector, $result);
76
    }
77
78
    /**
79
     * Simulate remote cleanup.
80
     *
81
     * @param Target $target
82
     * @param Result $result
83
     */
84 6
    public function simulateRemoteCleanup(Target $target, Result $result)
85
    {
86 6
        if ($this->cleaner) {
87
            $result->debug("  sync cleanup: {$this->cleanupConfig->type}" . PHP_EOL);
88
        }
89
    }
90
}