Test Failed
Pull Request — master (#223)
by
unknown
04:38
created

Backup::executeCrypt()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 9.2728
c 0
b 0
f 0
cc 5
nc 9
nop 2
crap 5
1
<?php
2
namespace phpbu\App\Runner;
3
4
use phpbu\App\Backup\Compressor;
5
use phpbu\App\Backup\Sync\CleanableSyncStore;
6
use phpbu\App\Exception;
7
use phpbu\App\Backup\Cleaner;
8
use phpbu\App\Backup\Collector\Local;
9
use phpbu\App\Backup\Crypter;
10
use phpbu\App\Backup\Sync;
11
use phpbu\App\Backup\Target;
12
use phpbu\App\Configuration;
13
use phpbu\App\Result;
14
15
/**
16
 * Backup 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
 * @internal
25
 */
26
class Backup extends Compression
27
{
28
    /**
29
     * Backup failed
30
     *
31
     * @var bool
32
     */
33
    protected $failure;
34
35
    /**
36
     * Execute backups.
37
     *
38
     * @param  \phpbu\App\Configuration $configuration
39
     * @return \phpbu\App\Result
40
     * @throws \phpbu\App\Exception
41
     */
42 8
    public function run(Configuration $configuration) : Result
43
    {
44 8
        $this->configuration = $configuration;
45 8
        $stop                = false;
46
47 8
        $this->result->phpbuStart($configuration);
48
49
        // create backups
50
        /** @var \phpbu\App\Configuration\Backup $backup */
51 8
        foreach ($configuration->getBackups() as $backup) {
52 8
            if ($stop) {
53 2
                break;
54
            }
55
            // make sure the backup should be executed and is not excluded via the --limit option
56 8 View Code Duplication
            if (!$configuration->isBackupActive($backup->getName())) {
57
                $this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL);
58
                continue;
59
            }
60
            // setup target and collector, reset failure state
61 8
            $target        = $this->factory->createTarget($backup->getTarget());
62 8
            $collector     = new Local($target);
63 8
            $this->failure = false;
64
65
            try {
66
                /*      ___  ___  _______ ____  _____
67
                 *     / _ )/ _ |/ ___/ //_/ / / / _ \
68
                 *    / _  / __ / /__/ ,< / /_/ / ___/
69
                 *   /____/_/ |_\___/_/|_|\____/_/
70
                 */
71 8
                $this->executeSource($backup, $target);
72
73
                /*     _______ _____________ ______
74
                 *    / ___/ // / __/ ___/ //_/ __/
75
                 *   / /__/ _  / _// /__/ ,< _\ \
76
                 *   \___/_//_/___/\___/_/|_/___/
77
                 */
78 7
                $this->executeChecks($backup, $target, $collector);
79
80
                /*     __________  _____  ______
81
                 *    / ___/ _ \ \/ / _ \/_  __/
82
                 *   / /__/ , _/\  / ___/ / /
83
                 *   \___/_/|_| /_/_/    /_/
84
                 */
85 7
                $this->executeCrypt($backup, $target);
86
87
                /*      ______  ___  ___________
88
                 *     / __/\ \/ / |/ / ___/ __/
89
                 *    _\ \   \  /    / /___\ \
90
                 *   /___/   /_/_/|_/\___/___/
91
                 */
92 7
                $this->executeSyncs($backup, $target);
93
94
                /*     _______   _______   _  ____  _____
95
                 *    / ___/ /  / __/ _ | / |/ / / / / _ \
96
                 *   / /__/ /__/ _// __ |/    / /_/ / ___/
97
                 *   \___/____/___/_/ |_/_/|_/\____/_/
98
                 */
99 7
                $this->executeCleanup($backup, $target, $collector);
100 3
            } catch (\Exception $e) {
101 3
                $this->result->debug('exception: ' . $e->getMessage());
102 3
                $this->result->addError($e);
103 3
                $this->result->backupFailed($backup);
104 3
                if ($backup->stopOnFailure()) {
105 8
                    $stop = true;
106
                }
107
            }
108
        }
109 8
        $this->result->phpbuEnd();
110
111 8
        return $this->result;
112
    }
113
114
    /**
115
     * Execute the backup.
116
     *
117
     * @param  \phpbu\App\Configuration\Backup $conf
118
     * @param  \phpbu\App\Backup\Target        $target
119
     * @throws \Exception
120
     */
121 8 View Code Duplication
    protected function executeSource(Configuration\Backup $conf, Target $target)
122
    {
123 8
        $this->result->backupStart($conf);
124 8
        $source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options);
125 8
        $status = $source->backup($target, $this->result);
126 7
        $this->compress($status, $target, $this->result);
127 7
        $this->result->backupEnd($conf);
128 7
    }
129
130
    /**
131
     * Execute checks.
132
     *
133
     * @param  \phpbu\App\Configuration\Backup   $backup
134
     * @param  \phpbu\App\Backup\Target          $target
135
     * @param  \phpbu\App\Backup\Collector\Local $collector
136
     * @throws \Exception
137
     */
138 7
    protected function executeChecks(Configuration\Backup $backup, Target $target, Local $collector)
139
    {
140 7
        foreach ($backup->getChecks() as $config) {
141
            try {
142 7
                $this->result->checkStart($config);
143 7
                $check = $this->factory->createCheck($config->type);
144 7
                if ($check->pass($target, $config->value, $collector, $this->result)) {
145 5
                    $this->result->checkEnd($config);
146
                } else {
147 1
                    $this->failure = true;
148 6
                    $this->result->checkFailed($config);
149
                }
150 1
            } catch (Exception $e) {
151 1
                $this->failure = true;
152 1
                $this->result->addError($e);
153 7
                $this->result->checkFailed($config);
154
            }
155
        }
156 7
    }
157
158
    /**
159
     * Execute encryption.
160
     *
161
     * @param  \phpbu\App\Configuration\Backup $backup
162
     * @param  \phpbu\App\Backup\Target        $target
163
     * @throws \phpbu\App\Exception
164
     */
165 7
    protected function executeCrypt(Configuration\Backup $backup, Target $target)
166
    {
167 7
        if ($backup->hasCrypt()) {
168 7
            $config = $backup->getCrypt();
169
            try {
170 7
                $this->result->cryptStart($config);
171 7
                if ($this->failure && $config->skipOnFailure) {
172 2
                    $this->result->cryptSkipped($config);
173 2
                    return;
174
                }
175 5
                $crypter = $this->factory->createCrypter($config->type, $config->options);
176 5
                $crypter->crypt($target, $this->result);
177 4
                $target->setCrypter($crypter);
178 4
                $this->result->cryptEnd($config);
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
    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 1
            } catch (Sync\Exception $e) {
208 1
                $this->failure = true;
209 1
                $this->result->addError($e);
210 4
                $this->result->syncFailed($config);
211
            }
212
        }
213 4
    }
214
215
    /**
216
     * Execute the cleanup.
217
     *
218
     * @param  \phpbu\App\Configuration\Backup   $backup
219
     * @param  \phpbu\App\Backup\Target          $target
220
     * @param  \phpbu\App\Backup\Collector\Local $collector
221
     * @throws \phpbu\App\Exception
222
     */
223 7
    protected function executeCleanup(Configuration\Backup $backup, Target $target, Local $collector)
224
    {
225 7
        if ($backup->hasCleanup()) {
226
            /* @var \phpbu\App\Configuration\Backup\Cleanup $config */
227 7
            $config = $backup->getCleanup();
228
            try {
229 7
                $this->result->cleanupStart($config);
230 7
                if ($this->failure && $config->skipOnFailure) {
231 4
                    $this->result->cleanupSkipped($config);
232 4
                    return;
233
                }
234 3
                $cleaner = $this->factory->createCleaner($config->type, $config->options);
235 3
                $cleaner->cleanup($target, $collector, $this->result);
236 2
                $this->result->cleanupEnd($config);
237
238 2
                foreach ($backup->getSyncs() as $syncConfig) {
239 2
                    $this->result->cleanupStart($config);
240 2
                    $sync = $this->factory->createSync($syncConfig->type, $syncConfig->options);
241
                    if (!($sync instanceof CleanableSyncStore)) {
242
                        continue;
243
                    }
244
                    $syncCollector = $sync->createCollector($target);
245
                    $cleaner->cleanup($target, $syncCollector, $this->result);
246
                    $this->result->cleanupEnd($config);
247
                }
248
249 3
            } catch (Cleaner\Exception $e) {
250 1
                $this->failure = true;
251 1
                $this->result->addError($e);
252 1
                $this->result->cleanupFailed($config);
253
            }
254
        }
255 1
    }
256
257
    /**
258
     * Execute the compressor.
259
     * Returns the path to the created archive file.
260
     *
261
     * @param  \phpbu\App\Backup\Compressor\Compressible $compressor
262
     * @param  \phpbu\App\Backup\Target                  $target
263
     * @param  \phpbu\App\Result                         $result
264
     * @return string
265
     */
266
    protected function executeCompressor(Compressor\Compressible $compressor, Target $target, Result $result) : string
267
    {
268
        return $compressor->compress($target, $result);
269
    }
270
}
271