1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use \Finite\Event\TransitionEvent; |
4
|
|
|
|
5
|
|
|
class DNDeploymentHandlers extends Object { |
|
|
|
|
6
|
|
|
|
7
|
|
|
public function onAnyTransition(TransitionEvent $e) { |
8
|
|
|
$object = $e->getStateMachine()->getObject(); |
9
|
|
|
if (!$object->hasMethod('log')) { |
10
|
|
|
// Not something we are able to log the transition for. Skip! |
11
|
|
|
return; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
$log = $object->log(); |
15
|
|
|
$log->write(sprintf( |
16
|
|
|
'State transitioned from "%s" to "%s" on %s (ID: %s, Title: %s)', |
17
|
|
|
$e->getInitialState()->getName(), |
18
|
|
|
$e->getTransition()->getState(), |
19
|
|
|
get_class($object), |
20
|
|
|
$object->ID, |
21
|
|
|
$object->Title |
22
|
|
|
)); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function onNew(TransitionEvent $e) { |
26
|
|
|
/** @var DNDeployment $deployment */ |
27
|
|
|
$deployment = $e->getStateMachine()->getObject(); |
28
|
|
|
$deployment->DeployRequested = null; |
29
|
|
|
$deployment->write(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function onSubmit(TransitionEvent $e) { |
33
|
|
|
/** @var DNDeployment $deployment */ |
34
|
|
|
$deployment = $e->getStateMachine()->getObject(); |
35
|
|
|
$deployment->DeployRequested = SS_Datetime::now()->Rfc2822(); |
36
|
|
|
$deployment->write(); |
37
|
|
|
$this->sendEmailToApprover($deployment); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function onApprove(TransitionEvent $e) { |
|
|
|
|
41
|
|
|
// @todo send email to requester that it's approved |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function onReject(TransitionEvent $e) { |
|
|
|
|
45
|
|
|
// @todo send email to requester that it's rejected |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function onQueue(TransitionEvent $e) { |
49
|
|
|
/** @var DNDeployment $deployment */ |
50
|
|
|
$deployment = $e->getStateMachine()->getObject(); |
51
|
|
|
|
52
|
|
|
$token = $deployment->enqueueDeployment(); |
53
|
|
|
$deployment->setResqueToken($token); |
54
|
|
|
$deployment->DeployStarted = SS_Datetime::now()->Rfc2822(); |
55
|
|
|
$deployment->write(); |
56
|
|
|
|
57
|
|
|
$log = $deployment->log(); |
58
|
|
|
$log->write(sprintf( |
59
|
|
|
'Deploy queued as job %s (sigFile is %s)', |
60
|
|
|
$token, |
61
|
|
|
$deployment->getSigFile() |
62
|
|
|
)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function onAbort(TransitionEvent $e) { |
66
|
|
|
$deployment = $e->getStateMachine()->getObject(); |
67
|
|
|
|
68
|
|
|
// 2 is SIGINT - we can't use SIGINT constant in the mod_apache context. |
69
|
|
|
$deployment->setSignal(2); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function sendEmailToApprover(DNDeployment $deployment) { |
73
|
|
|
$deployer = $deployment->Deployer(); |
74
|
|
|
$approver = $deployment->Approver(); |
|
|
|
|
75
|
|
|
if (!$approver || !$approver->exists()) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$email = Email::create(); |
80
|
|
|
$email->setTo(sprintf('%s <%s>', $approver->Name, $approver->Email)); |
81
|
|
|
$email->replyTo(sprintf('%s <%s>', $deployer->Name, $deployer->Email)); |
|
|
|
|
82
|
|
|
$email->setSubject('Deployment has been submitted'); |
83
|
|
|
$email->setTemplate('DeploymentNotificationSubmitted'); |
84
|
|
|
$email->populateTemplate($deployment); |
85
|
|
|
$email->send(); |
86
|
|
|
|
87
|
|
|
$log = $deployment->log(); |
88
|
|
|
$log->write(sprintf( |
89
|
|
|
'Deployment submitted email sent to approver %s <%s>', |
90
|
|
|
$approver->Name, |
91
|
|
|
$approver->Email |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.