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