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

Workflow   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 93.75%

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 10
c 7
b 0
f 3
lcom 1
cbo 8
dl 0
loc 125
ccs 45
cts 48
cp 0.9375
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B execute() 0 54 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
    private $logger;
51
52 26
    public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger, array $activities)
53
    {
54 26
        $this->eventDispatcher = $eventDispatcher;
55 26
        $this->logger = $logger;
56 26
        $this->activities = $activities;
57 26
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 4
    public function execute(ProfileInterface $profile)
63
    {
64 4
        $backup = new Backup($profile->getName());
65
66 4
        $this->logger->info(sprintf('About to execute backup for profile: "%s".', $profile->getName()));
67 4
        $this->eventDispatcher->dispatch(BackupEvents::BEGIN, new BackupEvent($this, $profile, $backup));
68
69 4
        $terminate = function() use ($profile) {
70
71
            try {
72
73 4
                $this->eventDispatcher->dispatch(BackupEvents::TERMINATE, new BackupEvent($profile));
74 2
                $this->logger->info(sprintf('Backup for profile "%s" successfully terminated.', $profile->getName()));
75
76 4
            } catch (\Exception $e) {
77
78 2
                $this->logger->alert(sprintf('Could not terminate backup process for profile "%s".', $profile->getName()));
79
80
            }
81 4
        };
82
83 4
        \Closure::bind($terminate, $this);
84
85
        try {
86
87
            /**
88
             * @var WorkflowActivityInterface $activity
89
             */
90 4
            foreach ($this->activities as $activity) {
91 4
                $this->executeActivity($activity, $profile, $backup);
92
            }
93
94
            $terminate();
95
96 4
        } catch (EmptySourceException $e) {
97
98 2
            $this->logger->info(sprintf('Backup for profile "%s" didn\'t yield any file for backup.', $profile->getName()));
99
100 2
            $terminate();
101
102 4
        } catch (\Exception $e) {
103
104 2
            $this->eventDispatcher->dispatch(BackupEvents::ERROR, new BackupEvent($this, $profile));
105 2
            $this->logger->critical(sprintf('There has been an error while executing backup profile "%s".', $profile->getName()), array(
106 4
                'message' => $e->getMessage(),
107 2
                'code' => $e->getCode(),
108 2
                'file' => $e->getFile(),
109 2
                'line' => $e->getLine(),
110 2
                'trace' => $e->getTrace()
111 2
            ));
112
113 2
            $terminate();
114
        }
115 4
    }
116
117
    /**
118
     * Execute workflow activity.
119
     *
120
     * @param WorkflowActivityInterface $activity Activity to execute.
121
     * @param ProfileInterface $profile Profile for which activity is being executed.
122
     * @param BackupInterface $backup Backup for which activity is being executed.
123
     *
124
     * @throws \Exception
125
     */
126 4
    protected function executeActivity(WorkflowActivityInterface $activity, ProfileInterface $profile, BackupInterface $backup)
127
    {
128
        $activity
129 4
            ->setBackup($backup)
130 4
            ->setProfile($profile);
131
132
        /**
133
         * @var LoggerAwareInterface $activity
134
         */
135 4
        if ($activity instanceof LoggerAwareInterface) {
136 4
            $activity->setLogger($this->logger);
137 4
        }
138
139
        /**
140
         * @var EventDispatcherAwareInterface $activity
141
         */
142 4
        if ($activity instanceof EventDispatcherAwareInterface) {
143 4
            $activity->setEventDispatcher($this->eventDispatcher);
144 4
        }
145
146
        try {
147
            /**
148
             * @var WorkflowActivityInterface $activity
149
             */
150 4
            $activity->execute();
151
152 4
        } catch (\Exception $e) {
153
154 4
            $this->eventDispatcher->dispatch(BackupEvents::ERROR, new BackupEvent($this, $profile, $backup, $activity));
155
156 4
            throw $e;
157
        }
158
    }
159
}
160