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
|
|
|
'State' => $deployment->State |
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
|
|
|
protected 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
|
|
|
$machine->getDispatcher()->addListener( |
73
|
|
|
// Global hook which runs before any transition is actually done. |
74
|
|
|
// It gives a chance to perform a dry-run, and reject the transition, for example |
75
|
|
|
// in case the user is unauthorised. |
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 = self::ALLOW_DNDEPLOYMENT_APPROVE; |
83
|
|
|
break; |
84
|
|
|
case 'queue': |
85
|
|
|
$code = self::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
|
|
|
|
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.