Completed
Push — master ( beafec...bac082 )
by Sean
03:35
created

PlanDispatcher::Link()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This dispatchers takes care of the viewing and changing the deploy plan. This includes
5
 * the guessed changes for a deployment, the deployment notes written by the deployer etc.
6
 */
7
class PlanDispatcher extends Dispatcher {
8
9
	const ACTION_PLAN = 'plan';
10
11
	/**
12
	 * @var array
13
	 */
14
	private static $action_types = [
15
		self::ACTION_PLAN
16
	];
17
18
	/**
19
	 * @var array
20
	 */
21
	public static $allowed_actions = [
22
		'deploysummary'
23
	];
24
25
	/**
26
	 * @var \DNProject
27
	 */
28
	protected $project = null;
29
30
	/**
31
	 * @var \DNEnvironment
32
	 */
33
	protected $environment = null;
34
35 View Code Duplication
	public function init() {
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...
36
		parent::init();
37
38
		$this->project = $this->getCurrentProject();
39
40
		if(!$this->project) {
41
			return $this->project404Response();
42
		}
43
44
		// Performs canView permission check by limiting visible projects
45
		$this->environment = $this->getCurrentEnvironment($this->project);
46
		if(!$this->environment) {
47
			return $this->environment404Response();
48
		}
49
	}
50
51
	/**
52
	 *
53
	 * @param \SS_HTTPRequest $request
54
	 *
55
	 * @return \HTMLText|\SS_HTTPResponse
56
	 */
57
	public function index(\SS_HTTPRequest $request) {
58
		$this->setCurrentActionType(self::ACTION_PLAN);
59
		$this->httpError(404);
60
	}
61
62
	/**
63
	 * @param SS_HTTPRequest $request
64
	 *
65
	 * @return SS_HTTPResponse
66
	 */
67
	public function deploysummary(SS_HTTPRequest $request) {
68
69
		if(strtolower($request->httpMethod()) !== 'post') {
70
			return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);
71
		}
72
		$this->checkSecurityToken();
73
74
		// @todo further permission checks?
75
		// @todo add more options, like force full deploy etc
76
		$options = [
77
			'sha' => $request->requestVar('sha')
78
		];
79
80
		$strategy = $this->environment->Backend()->planDeploy($this->environment, $options);
81
		$data = $strategy->toArray();
82
83
		$interface = $this->project->getRepositoryInterface();
84
		if($this->canCompareCodeVersions($interface, $data['changes'])) {
0 ignored issues
show
Bug introduced by
It seems like $interface defined by $this->project->getRepositoryInterface() on line 83 can be null; however, PlanDispatcher::canCompareCodeVersions() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
85
			$compareurl = sprintf(
86
				'%s/compare/%s...%s',
87
				$interface->URL,
88
				$data['changes']['Code version']['from'],
89
				$data['changes']['Code version']['to']
90
			);
91
			$data['changes']['Code version']['compareUrl'] = $compareurl;
92
		}
93
		$this->extend('updateDeploySummary', $data);
94
		return $this->getAPIResponse($data, 201);
95
	}
96
97
	/**
98
	 * @return string
99
	 */
100
	public function Link() {
101
		return \Controller::join_links($this->environment->Link(), self::ACTION_PLAN);
102
	}
103
104
	/**
105
	 * @param string $name
106
	 *
107
	 * @return array
108
	 */
109
	public function getModel($name = '') {
110
		return [];
111
	}
112
113
	/**
114
	 * @param ArrayData $interface
115
	 * @param $changes
116
	 *
117
	 * @return bool
118
	 *
119
	 */
120
	protected function canCompareCodeVersions(\ArrayData $interface, $changes) {
121
		if(empty($changes['Code version'])) {
122
			return false;
123
		}
124
		$codeVersion = ['Code version'];
125
		if(empty($interface)) {
126
			return false;
127
		}
128
		if(empty($interface->URL)) {
129
			return false;
130
		}
131
		if(empty($codeVersion['from']) || empty($codeVersion['to'])) {
132
			return false;
133
		}
134
		if(strlen($codeVersion['from']) !== 40 || strlen($codeVersion['to']) !== 40) {
135
			return false;
136
		}
137
		return true;
138
	}
139
140
}
141