Completed
Push — master ( 4df15a...6cc647 )
by Sebastian
03:01
created

Simulate   B

Complexity

Total Complexity 18

Size/Duplication

Total Lines 152
Duplicated Lines 31.58 %

Coupling/Cohesion

Components 1
Dependencies 18

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 18
dl 48
loc 152
ccs 59
cts 59
cp 1
rs 7.3333
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 27 3
A simulateSource() 12 12 2
A simulateChecks() 11 11 3
A simulateCrypt() 12 12 3
A simulateSyncs() 0 10 3
A simulateCleanup() 13 13 3
A executeCompressor() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace phpbu\App\Runner;
3
4
use phpbu\App\Backup\Source;
5
use phpbu\App\Backup\Check;
6
use phpbu\App\Backup\Crypter;
7
use phpbu\App\Backup\Sync;
8
use phpbu\App\Backup\Cleaner;
9
use phpbu\App\Backup\Compressor;
10
use phpbu\App\Backup\Collector;
11
use phpbu\App\Backup\Target;
12
use phpbu\App\Configuration;
13
use phpbu\App\Result;
14
15
/**
16
 * Simulate Runner
17
 *
18
 * @package    phpbu
19
 * @author     Sebastian Feldmann <[email protected]>
20
 * @copyright  Sebastian Feldmann <[email protected]>
21
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
22
 * @link       https://phpbu.de/
23
 * @since      Class available since Release 5.1.0
24
 */
25
class Simulate extends Compression
26
{
27
    /**
28
     * Execute backups.
29
     *
30
     * @param  \phpbu\App\Configuration $configuration
31
     * @return \phpbu\App\Result
32
     * @throws \Exception
33
     */
34 5
    public function run(Configuration $configuration) : Result
35
    {
36 5
        $this->configuration = $configuration;
37 5
        $this->result->phpbuStart($configuration);
38
39
        // create backups
40
        /** @var \phpbu\App\Configuration\Backup $backup */
41 5
        foreach ($configuration->getBackups() as $backup) {
42
            // make sure the backup should be executed and is not excluded via the --limit option
43 5
            if (!$configuration->isBackupActive($backup->getName())) {
44 1
                $this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL);
45 1
                continue;
46
            }
47
            // setup target and collector
48 5
            $target    = $this->factory->createTarget($backup->getTarget());
49 5
            $collector = new Collector($target);
50
51 5
            $this->simulateSource($backup, $target);
52 5
            $this->simulateChecks($backup, $target, $collector);
53 5
            $this->simulateCrypt($backup, $target);
54 5
            $this->simulateSyncs($backup, $target);
55 5
            $this->simulateCleanup($backup, $target, $collector);
56
        }
57 5
        $this->result->phpbuEnd();
58
59 5
        return $this->result;
60
    }
61
62
    /**
63
     * Simulate the backup.
64
     *
65
     * @param  \phpbu\App\Configuration\Backup $conf
66
     * @param  \phpbu\App\Backup\Target        $target
67
     * @throws \Exception
68
     */
69 5 View Code Duplication
    protected function simulateSource(Configuration\Backup $conf, Target $target)
70
    {
71 5
        $this->result->backupStart($conf);
72
        /* @var \phpbu\App\Runner\Source $runner */
73 5
        $source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options);
74
75 5
        if ($source instanceof Source\Simulator) {
76 5
            $status = $source->simulate($target, $this->result);
77 5
            $this->compress($status, $target, $this->result);
78
        }
79 5
        $this->result->backupEnd($conf);
80 5
    }
81
82
    /**
83
     * Simulate checks.
84
     *
85
     * @param  \phpbu\App\Configuration\Backup $backup
86
     * @param  \phpbu\App\Backup\Target        $target
87
     * @param  \phpbu\App\Backup\Collector     $collector
88
     * @throws \Exception
89
     */
90 5 View Code Duplication
    protected function simulateChecks(Configuration\Backup $backup, Target $target, Collector $collector)
91
    {
92 5
        foreach ($backup->getChecks() as $config) {
93 5
            $this->result->checkStart($config);
94 5
            $check = $this->factory->createCheck($config->type);
95 5
            if ($check instanceof Check\Simulator) {
96 5
                $check->simulate($target, $config->value, $collector, $this->result);
97
            }
98 5
            $this->result->checkEnd($config);
99
        }
100 5
    }
101
102
    /**
103
     * Simulate encryption.
104
     *
105
     * @param  \phpbu\App\Configuration\Backup $backup
106
     * @param  \phpbu\App\Backup\Target        $target
107
     * @throws \phpbu\App\Exception
108
     */
109 5 View Code Duplication
    protected function simulateCrypt(Configuration\Backup $backup, Target $target)
110
    {
111 5
        if ($backup->hasCrypt()) {
112 5
            $crypt = $backup->getCrypt();
113 5
            $this->result->cryptStart($crypt);
114 5
            $crypter = $this->factory->createCrypter($crypt->type, $crypt->options);
115 5
            if ($crypter instanceof Crypter\Simulator) {
116 5
                $crypter->simulate($target, $this->result);
117
            }
118 5
            $this->result->cryptEnd($crypt);
119
        }
120 5
    }
121
122
    /**
123
     * Simulate all syncs.
124
     *
125
     * @param  \phpbu\App\Configuration\Backup $backup
126
     * @param  \phpbu\App\Backup\Target        $target
127
     * @throws \Exception
128
     */
129 5
    protected function simulateSyncs(Configuration\Backup $backup, Target $target)
130
    {
131
        /* @var \phpbu\App\Configuration\Backup\Sync $sync */
132 5
        foreach ($backup->getSyncs() as $sync) {
133 5
            $sync = $this->factory->createSync($sync->type, $sync->options);
134 5
            if ($sync instanceof Sync\Simulator) {
135 5
                $sync->simulate($target, $this->result);
136
            }
137
        }
138 5
    }
139
140
    /**
141
     * Simulate the cleanup.
142
     *
143
     * @param  \phpbu\App\Configuration\Backup $backup
144
     * @param  \phpbu\App\Backup\Target        $target
145
     * @param  \phpbu\App\Backup\Collector     $collector
146
     * @throws \phpbu\App\Exception
147
     */
148 5 View Code Duplication
    protected function simulateCleanup(Configuration\Backup $backup, Target $target, Collector $collector)
149
    {
150
        /* @var \phpbu\App\Configuration\Backup\Cleanup $cleanup */
151 5
        if ($backup->hasCleanup()) {
152 5
            $cleanup = $backup->getCleanup();
153 5
            $cleaner = $this->factory->createCleaner($cleanup->type, $cleanup->options);
154 5
            $this->result->cleanupStart($cleanup);
155 5
            if ($cleaner instanceof Cleaner\Simulator) {
156 5
                $cleaner->simulate($target, $collector, $this->result);
157
            }
158 5
            $this->result->cleanupEnd($cleanup);
159
        }
160 5
    }
161
162
    /**
163
     * Execute the compressor.
164
     * Returns the path to the created archive file.
165
     *
166
     * @param  \phpbu\App\Backup\Compressor\Executable $compressor
167
     * @param  \phpbu\App\Backup\Target                $target
168
     * @param  \phpbu\App\Result                       $result
169
     * @return string
170
     */
171 3
    protected function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string
172
    {
173 3
        $result->debug($compressor->getExecutable($target)->getCommand());
174 3
        return $compressor->getArchiveFile($target);
175
    }
176
}
177