Completed
Push — master ( e73bbc...d6ad0c )
by Sebastian
03:01
created

Runner::executeSyncs()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 8.7624
cc 5
eloc 14
nc 7
nop 2
1
<?php
2
namespace phpbu\App;
3
4
use phpbu\App\Backup;
5
use phpbu\App\Backup\Collector;
6
use phpbu\App\Backup\Compressor;
7
use phpbu\App\Backup\Target;
8
9
/**
10
 * Runner actually executes all backup jobs.
11
 *
12
 * @package    phpbu
13
 * @subpackage App
14
 * @author     Sebastian Feldmann <[email protected]>
15
 * @copyright  Sebastian Feldmann <[email protected]>
16
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
17
 * @link       http://phpbu.de/
18
 * @since      Class available since Release 1.0.0
19
 */
20
class Runner
21
{
22
    /**
23
     * phpbu Factory
24
     *
25
     * @var \phpbu\App\Factory
26
     */
27
    protected $factory;
28
29
    /**
30
     * Application result
31
     *
32
     * @var \phpbu\App\Result
33
     */
34
    protected $result;
35
36
    /**
37
     * Backup failed
38
     *
39
     * @var boolean
40
     */
41
    protected $failure;
42
43
    /**
44
     * App Configuration
45
     *
46
     * @var \phpbu\App\Configuration
47
     */
48
    protected $configuration;
49
50
    /**
51
     * Constructor
52
     *
53
     * @param \phpbu\App\Factory $factory
54
     */
55
    public function __construct(Factory $factory)
56
    {
57
        $this->factory = $factory;
58
    }
59
60
    /**
61
     * Factory getter
62
     *
63
     * @return \phpbu\App\Factory
64
     */
65
    public function getFactory()
66
    {
67
        return $this->factory;
68
    }
69
70
    /**
71
     * Run phpbu
72
     *
73
     * @param  \phpbu\App\Configuration $configuration
74
     * @param  \phpbu\App\Factory
75
     * @return \phpbu\App\Result
76
     */
77
    public function run(Configuration $configuration)
78
    {
79
        $stop                = false;
80
        $this->result        = new Result();
81
        $this->configuration = $configuration;
82
83
        $this->setupEnvironment($configuration);
84
        $this->setupLoggers($configuration);
85
        $this->result->phpbuStart($configuration);
86
87
        // create backups
88
        /** @var \phpbu\App\Configuration\Backup $backup */
89
        foreach ($configuration->getBackups() as $backup) {
90
            if ($stop) {
91
                break;
92
            }
93
            // setup target and collector, reset failure state
94
            $target        = $this->createTarget($backup->getTarget());
95
            $collector     = new Collector($target);
96
            $this->failure = false;
97
98
            try {
99
                /*      ___  ___  _______ ____  _____
100
                 *     / _ )/ _ |/ ___/ //_/ / / / _ \
101
                 *    / _  / __ / /__/ ,< / /_/ / ___/
102
                 *   /____/_/ |_\___/_/|_|\____/_/
103
                 */
104
                $this->executeSource($backup, $target);
105
106
                /*     _______ _____________ ______
107
                 *    / ___/ // / __/ ___/ //_/ __/
108
                 *   / /__/ _  / _// /__/ ,< _\ \
109
                 *   \___/_//_/___/\___/_/|_/___/
110
                 */
111
                $this->executeChecks($backup, $target, $collector);
112
113
                /*     __________  _____  ______
114
                 *    / ___/ _ \ \/ / _ \/_  __/
115
                 *   / /__/ , _/\  / ___/ / /
116
                 *   \___/_/|_| /_/_/    /_/
117
                 */
118
                $this->executeCrypt($backup, $target);
119
120
                /*      ______  ___  ___________
121
                 *     / __/\ \/ / |/ / ___/ __/
122
                 *    _\ \   \  /    / /___\ \
123
                 *   /___/   /_/_/|_/\___/___/
124
                 */
125
                $this->executeSyncs($backup, $target);
126
127
                /*     _______   _______   _  ____  _____
128
                 *    / ___/ /  / __/ _ | / |/ / / / / _ \
129
                 *   / /__/ /__/ _// __ |/    / /_/ / ___/
130
                 *   \___/____/___/_/ |_/_/|_/\____/_/
131
                 */
132
                $this->executeCleanup($backup, $target, $collector);
133
134
            } catch (\Exception $e) {
135
                $this->result->debug('exception: ' . $e->getMessage());
136
                $this->result->addError($e);
137
                $this->result->backupFailed($backup);
138
                if ($backup->stopOnFailure()) {
139
                    $stop = true;
140
                }
141
            }
142
        }
143
        $this->result->phpbuEnd();
144
145
        return $this->result;
146
    }
147
148
    /**
149
     * This executes a bootstrap runner to handle ini settings and the bootstrap file inclusion.
150
     *
151
     * @param  \phpbu\App\Configuration $configuration
152
     * @throws \phpbu\App\Exception
153
     */
154
    protected function setupEnvironment(Configuration $configuration)
155
    {
156
        $runner = $this->factory->createRunner('Bootstrap', $this->configuration->isSimulation());
157
        $runner->run($configuration);
158
    }
159
160
    /**
161
     * Create and register all configured loggers.
162
     *
163
     * @param  \phpbu\App\Configuration $configuration
164
     */
165
    protected function setupLoggers(Configuration $configuration)
166
    {
167
        foreach ($configuration->getLoggers() as $log) {
168
            // this is a already fully setup Listener so just add it
169
            if ($log instanceof Listener) {
170
                $logger = $log;
171
            } else {
172
                // this is a configuration blueprint for a logger, so create and add it
173
                /** @var \phpbu\App\Configuration\Logger $log */
174
                /** @var \phpbu\App\Listener $logger */
175
                $logger = $this->factory->createLogger($log->type, $log->options);
176
            }
177
            $this->result->addListener($logger);
178
        }
179
    }
180
181
    /**
182
     * Create a target.
183
     *
184
     * @param  \phpbu\App\Configuration\Backup\Target $conf
185
     * @return \phpbu\App\Backup\Target
186
     * @throws \phpbu\App\Exception
187
     */
188
    protected function createTarget(Configuration\Backup\Target $conf)
189
    {
190
        $target = new Target($conf->dirname, $conf->filename);
191
        $target->setupPath();
192
        // add possible compressor
193
        if (!empty($conf->compression)) {
194
            $compression = Target\Compression\Factory::create($conf->compression);
195
            $target->setCompression($compression);
196
        }
197
        return $target;
198
    }
199
200
    /**
201
     * Execute the backup.
202
     *
203
     * @param  \phpbu\App\Configuration\Backup $conf
204
     * @param  \phpbu\App\Backup\Target        $target
205
     * @throws \Exception
206
     */
207
    protected function executeSource(Configuration\Backup $conf, Target $target)
208
    {
209
        $this->result->backupStart($conf);
210
        /* @var \phpbu\App\Runner\Source $runner */
211
        $source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options);
212
        $runner = $this->factory->createRunner('source', $this->configuration->isSimulation());
213
        $runner->run($source, $target, $this->result);
214
        $this->result->backupEnd($conf);
215
    }
216
217
    /**
218
     * Execute checks.
219
     *
220
     * @param  \phpbu\App\Configuration\Backup $backup
221
     * @param  \phpbu\App\Backup\Target        $target
222
     * @param  \phpbu\App\Backup\Collector     $collector
223
     * @throws \Exception
224
     */
225
    protected function executeChecks(Configuration\Backup $backup, Target $target, Collector $collector)
226
    {
227
        /* @var \phpbu\App\Runner\Check $runner */
228
        /* @var \phpbu\App\Configuration\Backup\Check $check */
229
        $runner = $this->factory->createRunner('check', $this->configuration->isSimulation());
230
        foreach ($backup->getChecks() as $config) {
231
            try {
232
                $this->result->checkStart($config);
233
                $check = $this->factory->createCheck($config->type);
234
                if ($runner->run($check, $target, $config->value, $collector, $this->result)) {
235
                    $this->result->checkEnd($config);
236
                } else {
237
                    $this->failure = true;
238
                    $this->result->checkFailed($config);
239
                }
240
            } catch (Exception $e) {
241
                $this->failure = true;
242
                $this->result->addError($e);
243
                $this->result->checkFailed($config);
244
            }
245
        }
246
    }
247
248
    /**
249
     * Execute encryption.
250
     *
251
     * @param \phpbu\App\Configuration\Backup $backup
252
     * @param \phpbu\App\Backup\Target        $target
253
     */
254
    protected function executeCrypt(Configuration\Backup $backup, Target $target)
255
    {
256
        if ($backup->hasCrypt()) {
257
            $crypt = $backup->getCrypt();
258
            try {
259
                $this->result->cryptStart($crypt);
260
                if ($this->failure && $crypt->skipOnFailure) {
261
                    $this->result->cryptSkipped($crypt);
262
                } else {
263
                    /* @var \phpbu\App\Runner\Crypter $runner */
264
                    $runner  = $this->factory->createRunner('crypter', $this->configuration->isSimulation());
265
                    $runner->run($this->factory->createCrypter($crypt->type, $crypt->options), $target, $this->result);
266
                }
267
            } catch (Backup\Crypter\Exception $e) {
268
                $this->failure = true;
269
                $this->result->addError($e);
270
                $this->result->cryptFailed($crypt);
271
            }
272
        }
273
    }
274
275
    /**
276
     * Execute all syncs.
277
     *
278
     * @param  \phpbu\App\Configuration\Backup $backup
279
     * @param  \phpbu\App\Backup\Target        $target
280
     * @throws \Exception
281
     */
282
    protected function executeSyncs(Configuration\Backup $backup, Target $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
283
    {
284
        /* @var \phpbu\App\Runner\Crypter $runner */
285
        /* @var \phpbu\App\Configuration\Backup\Sync $sync */
286
        $runner  = $this->factory->createRunner('sync', $this->configuration->isSimulation());
287
        foreach ($backup->getSyncs() as $sync) {
288
            try {
289
                $this->result->syncStart($sync);
290
                if ($this->failure && $sync->skipOnFailure) {
291
                    $this->result->syncSkipped($sync);
292
                } else {
293
                    $runner->run($this->factory->createSync($sync->type, $sync->options), $target, $this->result);
294
                    $this->result->syncEnd($sync);
295
                }
296
            } catch (Backup\Sync\Exception $e) {
297
                $this->failure = true;
298
                $this->result->addError($e);
299
                $this->result->syncFailed($sync);
300
            }
301
        }
302
    }
303
304
    /**
305
     * Execute the cleanup.
306
     *
307
     * @param  \phpbu\App\Configuration\Backup $backup
308
     * @param  \phpbu\App\Backup\Target        $target
309
     * @param  \phpbu\App\Backup\Collector     $collector
310
     * @throws \Exception
311
     */
312
    protected function executeCleanup(Configuration\Backup $backup, Target $target, Collector $collector)
313
    {
314
        /* @var \phpbu\App\Runner\Cleaner $runner */
315
        /* @var \phpbu\App\Configuration\Backup\Cleanup $cleanup */
316
        if ($backup->hasCleanup()) {
317
            $cleanup = $backup->getCleanup();
318
            try {
319
                $runner = $this->factory->createRunner('cleaner', $this->configuration->isSimulation());
320
                $this->result->cleanupStart($cleanup);
321
                if ($this->failure && $cleanup->skipOnFailure) {
322
                    $this->result->cleanupSkipped($cleanup);
323
                } else {
324
                    $cleaner = $this->factory->createCleaner($cleanup->type, $cleanup->options);
325
                    $runner->run($cleaner, $target, $collector, $this->result);
326
                    $this->result->cleanupEnd($cleanup);
327
                }
328
            } catch (Backup\Cleaner\Exception $e) {
329
                $this->failure = true;
330
                $this->result->addError($e);
331
                $this->result->cleanupFailed($cleanup);
332
            }
333
        }
334
    }
335
}
336