Completed
Pull Request — master (#579)
by Mateusz
07:37
created

DNDeploymentDispatcher::can()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 20
loc 20
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 12
nc 3
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
		'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();
0 ignored issues
show
Documentation Bug introduced by
The method getObject does not exist on object<DNDeploymentDispatcher>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
22
		return [
23
			'Status' => $obj->Status
24
		];
25
	}
26
27 View Code Duplication
	public function apply(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...
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) {
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...
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');
0 ignored issues
show
Unused Code introduced by
$params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
		$deployment = DNDeployment::get()->byId($id);
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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