Backup::executeSource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
namespace phpbu\App\Runner;
3
4
use phpbu\App\Backup\Compressor;
5
use phpbu\App\Backup\Source;
6
use phpbu\App\Exception;
7
use phpbu\App\Backup\Cleaner;
8
use phpbu\App\Backup\Collector;
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 8
     */
42
    public function run(Configuration $configuration) : Result
43 8
    {
44 8
        $this->configuration = $configuration;
45
        $stop                = false;
46 8
47
        $this->result->phpbuStart($configuration);
48
49
        // create backups
50 8
        /** @var \phpbu\App\Configuration\Backup $backup */
51 8
        foreach ($configuration->getBackups() as $backup) {
52 1
            if ($stop) {
53
                break;
54
            }
55 8
            // make sure the backup should be executed and is not excluded via the --limit option
56 1
            if (!$configuration->isBackupActive($backup->getName())) {
57 1
                $this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL);
58
                continue;
59
            }
60 8
            // setup target and collector, reset failure state
61 8
            $target        = $this->factory->createTarget($backup->getTarget());
62 8
            $source        = $this->factory->createSource($backup->getSource()->type, $backup->getSource()->options);
63
            $this->failure = false;
64
65
            try {
66
                /*      ___  ___  _______ ____  _____
67
                 *     / _ )/ _ |/ ___/ //_/ / / / _ \
68
                 *    / _  / __ / /__/ ,< / /_/ / ___/
69
                 *   /____/_/ |_\___/_/|_|\____/_/
70 8
                 */
71
                $this->executeSource($backup, $target, $source);
72
73
                /*     _______ _____________ ______
74
                 *    / ___/ // / __/ ___/ //_/ __/
75
                 *   / /__/ _  / _// /__/ ,< _\ \
76
                 *   \___/_//_/___/\___/_/|_/___/
77 7
                 */
78
                $this->executeChecks($backup, $target, new Collector\Local($target));
79
80
                /*     __________  _____  ______
81
                 *    / ___/ _ \ \/ / _ \/_  __/
82
                 *   / /__/ , _/\  / ___/ / /
83
                 *   \___/_/|_| /_/_/    /_/
84 7
                 */
85
                $this->executeCrypt($backup, $target);
86
87
                /*      ______  ___  ___________
88
                 *     / __/\ \/ / |/ / ___/ __/
89
                 *    _\ \   \  /    / /___\ \
90
                 *   /___/   /_/_/|_/\___/___/
91 7
                 */
92
                $this->executeSyncs($backup, $target);
93
94
                /*     _______   _______   _  ____  _____
95
                 *    / ___/ /  / __/ _ | / |/ / / / / _ \
96
                 *   / /__/ /__/ _// __ |/    / /_/ / ___/
97
                 *   \___/____/___/_/ |_/_/|_/\____/_/
98 7
                 */
99 1
                $this->executeCleanup($backup, $target, new Collector\Local($target));
100 1
            } catch (\Exception $e) {
101 1
                $this->result->debug('exception: ' . $e->getMessage());
102 1
                $this->result->addError($e);
103 1
                $this->result->backupFailed($backup, $target, $source);
104 8
                if ($backup->stopOnFailure()) {
105
                    $stop = true;
106
                }
107
            }
108 8
        }
109
        $this->result->phpbuEnd();
110 8
111
        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
     * @param Source $source
120 8
     * @throws Exception
121
     */
122 8
    protected function executeSource(Configuration\Backup $conf, Target $target, Source $source)
123 8
    {
124 8
        $this->result->backupStart($conf, $target, $source);
125 7
        $status = $source->backup($target, $this->result);
126 7
        $this->compress($status, $target, $this->result);
127 7
        $this->result->backupEnd($conf, $target, $source);
128
    }
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 7
     */
138
    protected function executeChecks(Configuration\Backup $backup, Target $target, Collector\Local $collector)
139 7
    {
140
        foreach ($backup->getChecks() as $config) {
141 7
            try {
142 7
                $this->result->checkStart($config);
143 7
                $check = $this->factory->createCheck($config->type);
144 5
                if ($check->pass($target, $config->value, $collector, $this->result)) {
145
                    $this->result->checkEnd($config);
146 1
                } else {
147 6
                    $this->failure = true;
148
                    $this->result->checkFailed($config);
149 1
                }
150 1
            } catch (Exception $e) {
151 1
                $this->failure = true;
152 7
                $this->result->addError($e);
153
                $this->result->checkFailed($config);
154
            }
155 7
        }
156
    }
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 7
     */
165
    protected function executeCrypt(Configuration\Backup $backup, Target $target)
166 7
    {
167 7
        if ($backup->hasCrypt()) {
168
            $config = $backup->getCrypt();
169 7
            try {
170 7
                $this->result->cryptStart($config);
171 2
                if ($this->failure && $config->skipOnFailure) {
172 2
                    $this->result->cryptSkipped($config);
173
                    return;
174 5
                }
175 5
                $crypter = $this->factory->createCrypter($config->type, $config->options);
176 4
                $crypter->crypt($target, $this->result);
177 4
                $target->setCrypter($crypter);
178 1
                $this->result->cryptEnd($config);
179 1
            } catch (Crypter\Exception $e) {
180 1
                $this->failure = true;
181 1
                $this->result->addError($e);
182
                $this->result->cryptFailed($config);
183
            }
184 5
        }
185
    }
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 7
     */
194
    protected function executeSyncs(Configuration\Backup $backup, Target $target)
195
    {
196 7
        /* @var \phpbu\App\Configuration\Backup\Sync $sync */
197
        foreach ($backup->getSyncs() as $config) {
198 7
            try {
199 7
                $this->result->syncStart($config);
200 3
                if ($this->failure && $config->skipOnFailure) {
201 3
                    $this->result->syncSkipped($config);
202
                    return;
203 4
                }
204 4
                $sync = $this->factory->createSync($config->type, $config->options);
205 3
                $sync->sync($target, $this->result);
206 1
                $this->result->syncEnd($config);
207 1
            } catch (Sync\Exception $e) {
208 1
                $this->failure = true;
209 4
                $this->result->addError($e);
210
                $this->result->syncFailed($config);
211
            }
212 4
        }
213
    }
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 7
     */
223
    protected function executeCleanup(Configuration\Backup $backup, Target $target, Collector\Local $collector)
224 7
    {
225
        if ($backup->hasCleanup()) {
226 7
            /* @var \phpbu\App\Configuration\Backup\Cleanup $config */
227
            $config = $backup->getCleanup();
228 7
            try {
229 7
                $this->result->cleanupStart($config);
230 4
                if ($this->failure && $config->skipOnFailure) {
231 4
                    $this->result->cleanupSkipped($config);
232
                    return;
233 3
                }
234 3
                $cleaner = $this->factory->createCleaner($config->type, $config->options);
235 2
                $cleaner->cleanup($target, $collector, $this->result);
236 1
                $this->result->cleanupEnd($config);
237 1
            } catch (Cleaner\Exception $e) {
238 1
                $this->failure = true;
239 1
                $this->result->addError($e);
240
                $this->result->cleanupFailed($config);
241
            }
242 3
        }
243
    }
244
245
    /**
246
     * Execute the compressor.
247
     * Returns the path to the created archive file.
248
     *
249
     * @param  \phpbu\App\Backup\Compressor\Compressible $compressor
250
     * @param  \phpbu\App\Backup\Target                  $target
251
     * @param  \phpbu\App\Result                         $result
252
     * @return string
253
     */
254
    protected function executeCompressor(Compressor\Compressible $compressor, Target $target, Result $result) : string
255
    {
256
        return $compressor->compress($target, $result);
257
    }
258
}
259