Completed
Push — master ( 1e16b2...1c5ba4 )
by Sebastian
02:36
created

Backup   B

Complexity

Total Complexity 27

Size/Duplication

Total Lines 237
Duplicated Lines 32.91 %

Coupling/Cohesion

Components 1
Dependencies 18

Test Coverage

Coverage 97.85%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 18
dl 78
loc 237
ccs 91
cts 93
cp 0.9785
rs 7.3333
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B executeSyncs() 21 21 5
B executeCleanup() 22 22 5
A executeCompressor() 0 4 1
B run() 4 72 6
A executeSource() 8 8 1
A executeChecks() 0 19 4
B executeCrypt() 22 22 5

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\Compressor;
5
use phpbu\App\Exception;
6
use phpbu\App\Backup\Cleaner;
7
use phpbu\App\Backup\Collector;
8
use phpbu\App\Backup\Crypter;
9
use phpbu\App\Backup\Sync;
10
use phpbu\App\Backup\Target;
11
use phpbu\App\Configuration;
12
use phpbu\App\Result;
13
14
/**
15
 * Backup Runner
16
 *
17
 * @package    phpbu
18
 * @author     Sebastian Feldmann <[email protected]>
19
 * @copyright  Sebastian Feldmann <[email protected]>
20
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
21
 * @link       https://phpbu.de/
22
 * @since      Class available since Release 5.1.0
23
 */
24
class Backup extends Compression
25
{
26
    /**
27
     * Backup failed
28
     *
29
     * @var bool
30
     */
31
    protected $failure;
32
33
    /**
34
     * Execute backups.
35
     *
36
     * @param  \phpbu\App\Configuration $configuration
37
     * @return \phpbu\App\Result
38
     * @throws \phpbu\App\Exception
39
     */
40 8
    public function run(Configuration $configuration) : Result
41
    {
42 8
        $this->configuration = $configuration;
43 8
        $stop                = false;
44
45 8
        $this->result->phpbuStart($configuration);
46
47
        // create backups
48
        /** @var \phpbu\App\Configuration\Backup $backup */
49 8
        foreach ($configuration->getBackups() as $backup) {
50 8
            if ($stop) {
51 1
                break;
52
            }
53
            // make sure the backup should be executed and is not excluded via the --limit option
54 8 View Code Duplication
            if (!$configuration->isBackupActive($backup->getName())) {
55 1
                $this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL);
56 1
                continue;
57
            }
58
            // setup target and collector, reset failure state
59 8
            $target        = $this->factory->createTarget($backup->getTarget());
60 8
            $collector     = new Collector($target);
61 8
            $this->failure = false;
62
63
            try {
64
                /*      ___  ___  _______ ____  _____
65
                 *     / _ )/ _ |/ ___/ //_/ / / / _ \
66
                 *    / _  / __ / /__/ ,< / /_/ / ___/
67
                 *   /____/_/ |_\___/_/|_|\____/_/
68
                 */
69 8
                $this->executeSource($backup, $target);
70
71
                /*     _______ _____________ ______
72
                 *    / ___/ // / __/ ___/ //_/ __/
73
                 *   / /__/ _  / _// /__/ ,< _\ \
74
                 *   \___/_//_/___/\___/_/|_/___/
75
                 */
76 7
                $this->executeChecks($backup, $target, $collector);
77
78
                /*     __________  _____  ______
79
                 *    / ___/ _ \ \/ / _ \/_  __/
80
                 *   / /__/ , _/\  / ___/ / /
81
                 *   \___/_/|_| /_/_/    /_/
82
                 */
83 7
                $this->executeCrypt($backup, $target);
84
85
                /*      ______  ___  ___________
86
                 *     / __/\ \/ / |/ / ___/ __/
87
                 *    _\ \   \  /    / /___\ \
88
                 *   /___/   /_/_/|_/\___/___/
89
                 */
90 7
                $this->executeSyncs($backup, $target);
91
92
                /*     _______   _______   _  ____  _____
93
                 *    / ___/ /  / __/ _ | / |/ / / / / _ \
94
                 *   / /__/ /__/ _// __ |/    / /_/ / ___/
95
                 *   \___/____/___/_/ |_/_/|_/\____/_/
96
                 */
97 7
                $this->executeCleanup($backup, $target, $collector);
98
99 1
            } catch (\Exception $e) {
100 1
                $this->result->debug('exception: ' . $e->getMessage());
101 1
                $this->result->addError($e);
102 1
                $this->result->backupFailed($backup);
103 1
                if ($backup->stopOnFailure()) {
104 8
                    $stop = true;
105
                }
106
            }
107
        }
108 8
        $this->result->phpbuEnd();
109
110 8
        return $this->result;
111
    }
112
113
    /**
114
     * Execute the backup.
115
     *
116
     * @param  \phpbu\App\Configuration\Backup $conf
117
     * @param  \phpbu\App\Backup\Target        $target
118
     * @throws \Exception
119
     */
120 8 View Code Duplication
    protected function executeSource(Configuration\Backup $conf, Target $target)
121
    {
122 8
        $this->result->backupStart($conf);
123 8
        $source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options);
124 8
        $status = $source->backup($target, $this->result);
125 7
        $this->compress($status, $target, $this->result);
126 7
        $this->result->backupEnd($conf);
127 7
    }
128
129
    /**
130
     * Execute checks.
131
     *
132
     * @param  \phpbu\App\Configuration\Backup $backup
133
     * @param  \phpbu\App\Backup\Target        $target
134
     * @param  \phpbu\App\Backup\Collector     $collector
135
     * @throws \Exception
136
     */
137 7
    protected function executeChecks(Configuration\Backup $backup, Target $target, Collector $collector)
138
    {
139 7
        foreach ($backup->getChecks() as $config) {
140
            try {
141 7
                $this->result->checkStart($config);
142 7
                $check = $this->factory->createCheck($config->type);
143 7
                if ($check->pass($target, $config->value, $collector, $this->result)) {
144 5
                    $this->result->checkEnd($config);
145
                } else {
146 1
                    $this->failure = true;
147 6
                    $this->result->checkFailed($config);
148
                }
149 1
            } catch (Exception $e) {
150 1
                $this->failure = true;
151 1
                $this->result->addError($e);
152 7
                $this->result->checkFailed($config);
153
            }
154
        }
155 7
    }
156
157
    /**
158
     * Execute encryption.
159
     *
160
     * @param  \phpbu\App\Configuration\Backup $backup
161
     * @param  \phpbu\App\Backup\Target        $target
162
     * @throws \phpbu\App\Exception
163
     */
164 7 View Code Duplication
    protected function executeCrypt(Configuration\Backup $backup, Target $target)
165
    {
166 7
        if ($backup->hasCrypt()) {
167 7
            $config = $backup->getCrypt();
168
            try {
169 7
                $this->result->cryptStart($config);
170 7
                if ($this->failure && $config->skipOnFailure) {
171 2
                    $this->result->cryptSkipped($config);
172 2
                    return;
173
                }
174 5
                $crypter = $this->factory->createCrypter($config->type, $config->options);
175 5
                $crypter->crypt($target, $this->result);
176 4
                $target->setCrypter($crypter);
177 4
                $this->result->cryptEnd($config);
178
179 1
            } catch (Crypter\Exception $e) {
180 1
                $this->failure = true;
181 1
                $this->result->addError($e);
182 1
                $this->result->cryptFailed($config);
183
            }
184
        }
185 5
    }
186
187
    /**
188
     * Execute all syncs.
189
     *
190
     * @param  \phpbu\App\Configuration\Backup $backup
191
     * @param  \phpbu\App\Backup\Target        $target
192
     * @throws \Exception
193
     */
194 7 View Code Duplication
    protected function executeSyncs(Configuration\Backup $backup, Target $target)
195
    {
196
        /* @var \phpbu\App\Configuration\Backup\Sync $sync */
197 7
        foreach ($backup->getSyncs() as $config) {
198
            try {
199 7
                $this->result->syncStart($config);
200 7
                if ($this->failure && $config->skipOnFailure) {
201 3
                    $this->result->syncSkipped($config);
202 3
                    return;
203
                }
204 4
                $sync = $this->factory->createSync($config->type, $config->options);
205 4
                $sync->sync($target, $this->result);
206 3
                $this->result->syncEnd($config);
207
208 1
            } catch (Sync\Exception $e) {
209 1
                $this->failure = true;
210 1
                $this->result->addError($e);
211 4
                $this->result->syncFailed($config);
212
            }
213
        }
214 4
    }
215
216
    /**
217
     * Execute the cleanup.
218
     *
219
     * @param  \phpbu\App\Configuration\Backup $backup
220
     * @param  \phpbu\App\Backup\Target        $target
221
     * @param  \phpbu\App\Backup\Collector     $collector
222
     * @throws \phpbu\App\Exception
223
     */
224 7 View Code Duplication
    protected function executeCleanup(Configuration\Backup $backup, Target $target, Collector $collector)
225
    {
226 7
        if ($backup->hasCleanup()) {
227
            /* @var \phpbu\App\Configuration\Backup\Cleanup $config */
228 7
            $config = $backup->getCleanup();
229
            try {
230 7
                $this->result->cleanupStart($config);
231 7
                if ($this->failure && $config->skipOnFailure) {
232 4
                    $this->result->cleanupSkipped($config);
233 4
                    return;
234
                }
235 3
                $cleaner = $this->factory->createCleaner($config->type, $config->options);
236 3
                $cleaner->cleanup($target, $collector, $this->result);
237 2
                $this->result->cleanupEnd($config);
238
239 1
            } catch (Cleaner\Exception $e) {
240 1
                $this->failure = true;
241 1
                $this->result->addError($e);
242 1
                $this->result->cleanupFailed($config);
243
            }
244
        }
245 3
    }
246
247
    /**
248
     * Execute the compressor.
249
     * Returns the path to the created archive file.
250
     *
251
     * @param  \phpbu\App\Backup\Compressor\Executable $compressor
252
     * @param  \phpbu\App\Backup\Target                $target
253
     * @param  \phpbu\App\Result                       $result
254
     * @return string
255
     */
256
    protected function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string
257
    {
258
        return $compressor->compress($target, $result);
259
    }
260
}
261