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
|
|
|
'canApply', |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
private static $url_handlers = [ |
16
|
|
|
'$Id/apply/$State' => 'apply', |
17
|
|
|
'$Id/can/$State' => 'canApply', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
public function getModel($name) { |
21
|
|
|
$id = $this->request->param('Id'); |
22
|
|
|
$deployment = DNDeployment::get()->byId($id); |
23
|
|
|
return [ |
24
|
|
|
'Status' => $deployment->Status |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function apply(SS_HTTPRequest $request) { |
29
|
|
|
$this->checkSecurityToken(); |
30
|
|
|
|
31
|
|
|
$project = $this->getCurrentProject(); |
32
|
|
|
if(!$project) { |
33
|
|
|
return $this->project404Response(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$env = $this->getCurrentEnvironment($project); |
37
|
|
|
if(!$env) { |
38
|
|
|
return $this->environment404Response(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$machine = $this->getMachine(); |
42
|
|
|
$machine->apply($request->param('State')); |
43
|
|
|
|
44
|
|
|
$model = $this->getModel('DNDeployment'); |
45
|
|
|
return $this->asJSON($model); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function canApply(SS_HTTPRequest $request) { |
|
|
|
|
49
|
|
|
$this->checkSecurityToken(); |
50
|
|
|
|
51
|
|
|
$project = $this->getCurrentProject(); |
52
|
|
|
if(!$project) { |
53
|
|
|
return $this->project404Response(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$env = $this->getCurrentEnvironment($project); |
57
|
|
|
if(!$env) { |
58
|
|
|
return $this->environment404Response(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->asJSON([ |
62
|
|
|
'Can' => $this->getMachine()->can($request->param('State')) |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function getMachine() { |
67
|
|
|
$id = $this->request->param('Id'); |
68
|
|
|
$deployment = DNDeployment::get()->byId($id); |
69
|
|
|
$project = $deployment->Environment()->Project(); |
70
|
|
|
$machine = $deployment->getMachine(); |
71
|
|
|
|
72
|
|
|
// Map authorisation rules to selected transitions. |
73
|
|
|
$machine->getDispatcher()->addListener( |
74
|
|
|
'finite.test_transition', |
75
|
|
|
function (Finite\Event\TransitionEvent $e) use ($project) { |
76
|
|
|
$code = null; |
77
|
|
|
|
78
|
|
|
switch ($e->getTransition()->getName()) { |
79
|
|
|
case 'approve': |
80
|
|
|
$code = self::ALLOW_DNDEPLOYMENT_APPROVE; |
81
|
|
|
break; |
82
|
|
|
case 'queue': |
83
|
|
|
$code = self::ALLOW_DNDEPLOYMENT_QUEUE; |
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!$code || !$project->allowed($code)) { |
|
|
|
|
88
|
|
|
$e->reject(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return $machine; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.