Completed
Push — master ( 7b9c22...3563e8 )
by Nikola
03:28
created

Workflow   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 20.83%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 10
c 6
b 0
f 3
lcom 1
cbo 8
dl 0
loc 117
ccs 5
cts 24
cp 0.2083
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B execute() 0 46 5
B executeActivity() 0 33 4
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 Psr\Log\LoggerInterface;
16
use RunOpenCode\Backup\Backup\Backup;
17
use RunOpenCode\Backup\Contract\BackupInterface;
18
use RunOpenCode\Backup\Contract\EventDispatcherAwareInterface;
19
use RunOpenCode\Backup\Contract\LoggerAwareInterface;
20
use RunOpenCode\Backup\Contract\ProfileInterface;
21
use RunOpenCode\Backup\Contract\WorkflowActivityInterface;
22
use RunOpenCode\Backup\Contract\WorkflowInterface;
23
use RunOpenCode\Backup\Event\BackupEvent;
24
use RunOpenCode\Backup\Event\BackupEvents;
25
use RunOpenCode\Backup\Exception\EmptySourceException;
26
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27
28
/**
29
 * Class Workflow
30
 *
31
 * Workflow is entry point of backup workflow that executes workflow activities in given sequence.
32
 *
33
 * @package RunOpenCode\Backup\Workflow
34
 */
35
class Workflow implements WorkflowInterface
36
{
37
    /**
38
     * @var WorkflowActivityInterface[]
39
     */
40
    private $activities;
41
42
    /**
43
     * @var EventDispatcherInterface
44
     */
45
    private $eventDispatcher;
46
47
    /**
48
     * @var LoggerInterface
49
     */
50 14
    private $logger;
51
52 14
    public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger, array $activities)
53 14
    {
54 14
        $this->eventDispatcher = $eventDispatcher;
55 14
        $this->logger = $logger;
56
        $this->activities = $activities;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function execute(ProfileInterface $profile)
63
    {
64
        $backup = new Backup($profile->getName());
65
66
        $this->logger->info(sprintf('About to execute backup for profile: "%s".', $profile->getName()));
67
        $this->eventDispatcher->dispatch(BackupEvents::BEGIN, new BackupEvent($this, $profile, $backup));
68
69
        try {
70
71
            /**
72
             * @var WorkflowActivityInterface $activity
73
             */
74
            foreach ($this->activities as $activity) {
75
                $this->executeActivity($activity, $profile, $backup);
76
            }
77
78
        } catch (EmptySourceException $e) {
79
80
            $this->logger->info(sprintf('Backup for profile "%s" didn\'t yield any file for backup.', $profile->getName()));
81
82
        } catch (\Exception $e) {
83
84
            $this->eventDispatcher->dispatch(BackupEvents::ERROR, new BackupEvent($this, $profile));
85
            $this->logger->critical(sprintf('There has been an error while executing backup profile "%s".', $profile->getName()), array(
86
                'message' => $e->getMessage(),
87
                'code' => $e->getCode(),
88
                'file' => $e->getFile(),
89
                'line' => $e->getLine(),
90
                'trace' => $e->getTrace()
91
            ));
92
93
        } finally {
94
95
            try {
96
97
                $this->eventDispatcher->dispatch(BackupEvents::TERMINATE, new BackupEvent($profile));
98
                $this->logger->info(sprintf('Backup for profile "%s" successfully terminated.', $profile->getName()));
99
100
            } catch (\Exception $e) {
101
102
                $this->logger->alert(sprintf('Could not terminate backup process for profile "%s".', $profile->getName()));
103
104
            }
105
106
        }
107
    }
108
109
    /**
110
     * Execute workflow activity.
111
     *
112
     * @param WorkflowActivityInterface $activity Activity to execute.
113
     * @param ProfileInterface $profile Profile for which activity is being executed.
114
     * @param BackupInterface $backup Backup for which activity is being executed.
115
     *
116
     * @throws \Exception
117
     */
118
    protected function executeActivity(WorkflowActivityInterface $activity, ProfileInterface $profile, BackupInterface $backup)
119
    {
120
        $activity
121
            ->setBackup($backup)
122
            ->setProfile($profile);
123
124
        /**
125
         * @var LoggerAwareInterface $activity
126
         */
127
        if ($activity instanceof LoggerAwareInterface) {
128
            $activity->setLogger($this->logger);
129
        }
130
131
        /**
132
         * @var EventDispatcherAwareInterface $activity
133
         */
134
        if ($activity instanceof EventDispatcherAwareInterface) {
135
            $activity->setEventDispatcher($this->eventDispatcher);
136
        }
137
138
        try {
139
            /**
140
             * @var WorkflowActivityInterface $activity
141
             */
142
            $activity->execute();
143
144
        } catch (\Exception $e) {
145
146
            $this->eventDispatcher->dispatch(BackupEvents::ERROR, new BackupEvent($this, $profile, $backup, $activity));
147
148
            throw $e;
149
        }
150
    }
151
}
152