Completed
Pull Request — master (#579)
by Mateusz
03:31
created

DNDeploymentDispatcher::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
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
		$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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)) {
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...
90
					$e->reject();
91
				}
92
			}
93
		);
94
95
		return $machine;
96
	}
97
98
}
99