Completed
Pull Request — master (#580)
by Sean
04:02
created

DNDeploymentDispatcher::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
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
		$deployment = $this->getDeployment();
22
		if (!$deployment) {
23
			return [];
24
		}
25
26
		return [
27
			'State' => $deployment->State
28
		];
29
	}
30
31
	public function getDeployment() {
32
		$id = $this->request->param('Id');
33
		$deployment = DNDeployment::get()->byId($id);
34
		if (!$deployment || !$deployment->exists()) {
35
			return false;
36
		}
37
		return $deployment;
38
	}
39
40
	public function apply(SS_HTTPRequest $request) {
41
		$this->checkSecurityToken();
42
		$response = $this->checkRequest();
43
		if ($response) {
44
			return $response;
45
		}
46
47
		$machine = $this->getMachine();
48
		$machine->apply($request->param('State'));
49
50
		$model = $this->getModel('DNDeployment');
51
		return $this->asJSON($model);
52
	}
53
54
	public function canApply(SS_HTTPRequest $request) {
55
		$this->checkSecurityToken();
56
		$response = $this->checkRequest();
57
		if ($response) {
58
			return $response;
59
		}
60
61
		return $this->asJSON([
62
			'Can' => $this->getMachine()->can($request->param('State'))
63
		]);
64
	}
65
66
	protected function checkRequest() {
67
		$project = $this->getCurrentProject();
68
		if(!$project) {
69
			return $this->project404Response();
70
		}
71
72
		$env = $this->getCurrentEnvironment($project);
73
		if(!$env) {
74
			return $this->environment404Response();
75
		}
76
77
		$deployment = $this->getDeployment();
78
		if (!$deployment) {
79
			return new SS_HTTPResponse('Deployment not found', 404);
80
		}
81
82
		return true;
83
	}
84
85
	protected function getMachine() {
86
		$deployment = $this->getDeployment();
87
		if (!$deployment) {
88
			return null;
89
		}
90
91
		$project = $deployment->Environment()->Project();
92
		$machine = $deployment->getMachine();
93
94
		$machine->getDispatcher()->addListener(
95
			// Global hook which runs before any transition is actually done.
96
			// It gives a chance to perform a dry-run, and reject the transition, for example
97
			// in case the user is unauthorised.
98
			'finite.test_transition',
99
			function (Finite\Event\TransitionEvent $e) use ($project) {
100
				$code = null;
101
102
				switch ($e->getTransition()->getName()) {
103
				case 'approve':
104
					$code = self::ALLOW_DNDEPLOYMENT_APPROVE;
105
					break;
106
				case 'queue':
107
					$code = self::ALLOW_DNDEPLOYMENT_QUEUE;
108
					break;
109
				}
110
111
				if (!$code || !$project->allowed($code)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $code of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
112
					$e->reject();
113
				}
114
			}
115
		);
116
117
		return $machine;
118
	}
119
120
}
121