Completed
Push — master ( 3563e8...eabdc7 )
by Nikola
04:08
created

Fetch::execute()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 30
ccs 16
cts 16
cp 1
rs 8.8571
cc 3
eloc 16
nc 4
nop 0
crap 3
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
namespace RunOpenCode\Backup\Workflow;
14
15
use RunOpenCode\Backup\Contract\EventDispatcherAwareInterface;
16
use RunOpenCode\Backup\Contract\LoggerAwareInterface;
17
use RunOpenCode\Backup\Event\BackupEvent;
18
use RunOpenCode\Backup\Event\BackupEvents;
19
use RunOpenCode\Backup\Event\EventDispatcherAwareTrait;
20
use RunOpenCode\Backup\Exception\EmptySourceException;
21
use RunOpenCode\Backup\Log\LoggerAwareTrait;
22
23
/**
24
 * Class Fetch
25
 *
26
 * Activity "Fetch": fetch backups from sources.
27
 *
28
 * @package RunOpenCode\Backup\Workflow
29
 */
30
class Fetch extends BaseActivity implements LoggerAwareInterface, EventDispatcherAwareInterface
31
{
32
    use LoggerAwareTrait;
33
    use EventDispatcherAwareTrait;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 8
    public function execute()
39
    {
40
        try {
41
42 8
            $this->backup->addFiles($this->profile->getSource()->fetch());
43 6
            $this->getEventDispatcher()->dispatch(BackupEvents::FETCH, new BackupEvent($this, $this->profile, $this->backup, $this));
44
45 8
        } catch (\Exception $e) {
46
47 2
            $this->getLogger()->error(sprintf('Could not fetch source files for profile "%s".', $this->profile->getName()), array(
48 2
                'message' => $e->getMessage(),
49 2
                'code' => $e->getCode(),
50 2
                'file' => $e->getFile(),
51 2
                'line' => $e->getLine(),
52 2
                'trace' => $e->getTrace()
53 2
            ));
54
55 2
            throw $e;
56
        }
57
58 6
        if (count($this->backup->getFiles()) == 0) {
59
60 4
            throw new EmptySourceException('Nothing to backup.');
61
62
        } else {
63
64 2
            $this->getLogger()->info(sprintf('Source files successfully fetched, %s total files are scheduled for backup.', count($this->backup->getFiles())));
65
66
        }
67 2
    }
68
}
69