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

Clearable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 59.09%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 69
ccs 13
cts 22
cp 0.5909
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B setUpClearable() 0 17 5
A cleanup() 0 12 3
A simulateRemoteCleanup() 0 6 2
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
}