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