1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Finite\StateMachine\StateMachine; |
4
|
|
|
|
5
|
|
|
class DNDeploymentDispatcher extends Dispatcher { |
6
|
|
|
|
7
|
|
|
const ALLOW_DNDEPLOYMENT_APPROVE = 'ALLOW_DNDEPLOYMENT_APPROVE'; |
8
|
|
|
const ALLOW_DNDEPLOYMENT_QUEUE = 'ALLOW_DNDEPLOYMENT_QUEUE'; |
9
|
|
|
|
10
|
|
|
private static $allowed_actions = [ |
11
|
|
|
'apply', |
12
|
|
|
'can', |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
private static $url_handlers = [ |
16
|
|
|
'$Id/can/$State' => 'can', |
17
|
|
|
'$Id/apply/$State' => 'apply', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
public function getModel($name) { |
21
|
|
|
$obj = $this->getObject(); |
|
|
|
|
22
|
|
|
return [ |
23
|
|
|
'Status' => $obj->Status |
24
|
|
|
]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function apply(SS_HTTPRequest $request) { |
|
|
|
|
28
|
|
|
$this->checkSecurityToken(); |
29
|
|
|
|
30
|
|
|
$project = $this->getCurrentProject(); |
31
|
|
|
if(!$project) { |
32
|
|
|
return $this->project404Response(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$env = $this->getCurrentEnvironment($project); |
36
|
|
|
if(!$env) { |
37
|
|
|
return $this->environment404Response(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$machine = $this->getMachine(); |
41
|
|
|
$machine->apply($request->param('State')); |
42
|
|
|
|
43
|
|
|
$model = $this->getModel('DNDeployment'); |
44
|
|
|
return $this->asJSON($model); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function can(SS_HTTPRequest $request) { |
|
|
|
|
48
|
|
|
$this->checkSecurityToken(); |
49
|
|
|
|
50
|
|
|
$project = $this->getCurrentProject(); |
51
|
|
|
if(!$project) { |
52
|
|
|
return $this->project404Response(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$env = $this->getCurrentEnvironment($project); |
56
|
|
|
if(!$env) { |
57
|
|
|
return $this->environment404Response(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$machine = $this->getMachine(); |
61
|
|
|
$machine->can($request->param('State')); |
62
|
|
|
|
63
|
|
|
$this->asJSON([ |
64
|
|
|
'Can' => $machine->can($request->param('State')) |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function getMachine() { |
69
|
|
|
$params = $this->request->param('Id'); |
|
|
|
|
70
|
|
|
$deployment = DNDeployment::get()->byId($id); |
|
|
|
|
71
|
|
|
$project = $deployment->Environment()->Project(); |
72
|
|
|
$machine = $deployment->getMachine(); |
73
|
|
|
|
74
|
|
|
// Map authorisation rules to selected transitions. |
75
|
|
|
$machine->getDispatcher()->addListener( |
76
|
|
|
'finite.test_transition', |
77
|
|
|
function (Finite\Event\TransitionEvent $e) use ($project) { |
78
|
|
|
$code = null; |
79
|
|
|
|
80
|
|
|
switch ($e->getTransition()->getName()) { |
81
|
|
|
case 'approve': |
82
|
|
|
$code = Dispatcher::ALLOW_DNDEPLOYMENT_APPROVE; |
83
|
|
|
break; |
84
|
|
|
case 'queue': |
85
|
|
|
$code = Dispatcher::ALLOW_DNDEPLOYMENT_QUEUE; |
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!$code || !$project->allowed($code)) { |
90
|
|
|
$e->reject(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $machine; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: