Completed
Push — master ( 11831e...ee744f )
by Nikola
08:31
created

Workflow   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 20.83%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B execute() 0 44 5
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\EventDispatcherAwareInterface;
18
use RunOpenCode\Backup\Contract\LoggerAwareInterface;
19
use RunOpenCode\Backup\Contract\ProfileInterface;
20
use RunOpenCode\Backup\Contract\WorkflowActivityInterface;
21
use RunOpenCode\Backup\Contract\WorkflowInterface;
22
use RunOpenCode\Backup\Event\BackupEvent;
23
use RunOpenCode\Backup\Event\BackupEvents;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
26
/**
27
 * Class Workflow
28
 *
29
 * Workflow is entry point of backup workflow that executes workflow activities in given sequence.
30
 *
31
 * @package RunOpenCode\Backup\Workflow
32
 */
33
class Workflow implements WorkflowInterface
34
{
35
    /**
36
     * @var WorkflowActivityInterface[]
37
     */
38
    private $activities;
39
40
    /**
41
     * @var EventDispatcherInterface
42
     */
43
    private $eventDispatcher;
44
45
    /**
46
     * @var LoggerInterface
47
     */
48
    private $logger;
49
50 6
    public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger, array $activities)
51
    {
52 6
        $this->eventDispatcher = $eventDispatcher;
53 6
        $this->logger = $logger;
54 6
        $this->activities = $activities;
55 6
    }
56
57
    public function execute(ProfileInterface $profile)
58
    {
59
        $backup = new Backup($profile->getName());
60
61
        $this->logger->info(sprintf('About to execute backup for profile: "%s".', $profile->getName()));
62
        $this->eventDispatcher->dispatch(BackupEvents::BEGIN, new BackupEvent($this, $profile, $backup));
63
64
        /**
65
         * @var WorkflowActivityInterface $activity
66
         */
67
        foreach ($this->activities as $activity) {
68
69
            $activity
70
                ->setBackup($backup)
71
                ->setProfile($profile);
72
73
            /**
74
             * @var LoggerAwareInterface $activity
75
             */
76
            if ($activity instanceof LoggerAwareInterface) {
77
                $activity->setLogger($this->logger);
78
            }
79
80
            /**
81
             * @var EventDispatcherAwareInterface $activity
82
             */
83
            if ($activity instanceof EventDispatcherAwareInterface) {
84
                $activity->setEventDispatcher($this->eventDispatcher);
85
            }
86
87
            try {
88
                /**
89
                 * @var WorkflowActivityInterface $activity
90
                 */
91
                $activity->execute();
92
93
            } catch (\Exception $e) {
94
95
                $this->eventDispatcher->dispatch(BackupEvents::ERROR, new BackupEvent($this, $profile, $backup, $activity));
96
97
                throw $e;
98
            }
99
        }
100
    }
101
}
102