Completed
Pull Request — master (#596)
by Stig
04:11 queued 01:05
created

DeployPlanDispatcher   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 187
Duplicated Lines 12.3 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 6
dl 23
loc 187
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 15 3
A Link() 0 3 1
A index() 0 6 1
B git_refs() 0 38 3
A getModel() 0 5 1
A getGitBranches() 0 10 2
A getGitTags() 0 10 2
B getGitPrevDeploys() 23 27 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
class DeployPlanDispatcher extends Dispatcher {
5
6
	const ACTION_PLAN = 'plan';
7
8
	/**
9
	 * @var array
10
	 */
11
	private static $action_types = [
12
		self::ACTION_PLAN
13
	];
14
15
	/**
16
	 * @var array
17
	 */
18
	public static $allowed_actions = [
19
		'git_refs',
20
	];
21
22
	/**
23
	 * @var \DNProject
24
	 */
25
	protected $project = null;
26
27
	/**
28
	 * @var \DNEnvironment
29
	 */
30
	protected $environment = null;
31
32
	public function init() {
33
		parent::init();
34
35
		$this->project = $this->getCurrentProject();
36
37
		if(!$this->project) {
38
			return $this->project404Response();
39
		}
40
41
		// Performs canView permission check by limiting visible projects
42
		$this->environment = $this->getCurrentEnvironment($this->project);
43
		if(!$this->environment) {
44
			return $this->environment404Response();
45
		}
46
	}
47
48
	/**
49
	 * @return string
50
	 */
51
	public function Link() {
52
		return \Controller::join_links($this->environment->Link(), self::ACTION_PLAN);
53
	}
54
55
	/**
56
	 * Render configuration form.
57
	 *
58
	 * @param \SS_HTTPRequest $request
59
	 *
60
	 * @return \HTMLText|\SS_HTTPResponse
61
	 */
62
	public function index(\SS_HTTPRequest $request) {
63
		$this->setCurrentActionType(self::ACTION_PLAN);
64
		return $this->customise([
65
			'Environment' => $this->environment
66
		])->renderWith(['Plan', 'DNRoot']);
67
	}
68
69
	/**
70
	 * @param SS_HTTPRequest $request
71
	 *
72
	 * @return string
73
	 */
74
	public function git_refs(\SS_HTTPRequest $request) {
75
76
		$refs = [];
77
		$order = 0;
78
		$refs[] = [
79
			'id' => ++$order,
80
			'label' => "Branch version",
81
			"description" => "Deploy the latest version of a branch",
82
			"list" => $this->getGitBranches($this->project)
83
		];
84
85
		$refs[] = [
86
			'id' => ++$order,
87
			'label' => "Tag version",
88
			"description" => "Deploy a tagged release",
89
			"list" => $this->getGitTags($this->project)
90
		];
91
92
		// @todo: the original was a tree that was keyed by environment, the
93
		// front-end dropdown needs to be changed to support that. brrrr.
94
		$prevDeploys = [];
95
		foreach($this->getGitPrevDeploys($this->project) as $env) {
96
			foreach($env as $deploy) {
97
				$prevDeploys[] = $deploy;
98
			}
99
		}
100
		$refs[] = [
101
			'id' => ++$order,
102
			'label' => "Redeploy a release that was previously deployed (to any environment",
103
			"description" => "Deploy a previous release",
104
			"list" => $prevDeploys
105
		];
106
107
		$body = json_encode($refs, JSON_PRETTY_PRINT);
108
		$this->getResponse()->addHeader('Content-Type', 'application/json');
109
		$this->getResponse()->setBody($body);
110
		return $body;
111
	}
112
113
	/**
114
	 * Generate the data structure used by the frontend component.
115
	 *
116
	 * @param string $name of the component
117
	 *
118
	 * @return array
119
	 */
120
	public function getModel($name) {
121
		return [
122
			'APIEndpoint' => Director::absoluteBaseURL().$this->Link()
123
		];
124
	}
125
126
	/**
127
	 * @param $project
128
	 *
129
	 * @return array
130
	 */
131
	protected function getGitBranches($project) {
132
		$branches = [];
133
		foreach($project->DNBranchList() as $branch) {
134
			$branches[] = [
135
				'key' => $branch->SHA(),
136
				'value' => $branch->Name(),
137
			];
138
		}
139
		return $branches;
140
	}
141
142
	/**
143
	 * @param $project
144
	 *
145
	 * @return array
146
	 */
147
	protected function getGitTags($project) {
148
		$tags = [];
149
		foreach($project->DNTagList()->setLimit(null) as $tag) {
150
			$tags[] = [
151
				'key' => $tag->SHA(),
152
				'value' => $tag->Name(),
153
			];
154
		}
155
		return $tags;
156
	}
157
158
	/**
159
	 * @param $project
160
	 *
161
	 * @return array
162
	 */
163
	protected function getGitPrevDeploys($project) {
164
		$redeploy = [];
165 View Code Duplication
		foreach($project->DNEnvironmentList() as $dnEnvironment) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
166
			$envName = $dnEnvironment->Name;
167
			$perEnvDeploys = [];
168
			foreach($dnEnvironment->DeployHistory() as $deploy) {
169
				$sha = $deploy->SHA;
170
171
				// Check if exists to make sure the newest deployment date is used.
172
				if(!isset($perEnvDeploys[$sha])) {
173
					$pastValue = sprintf(
174
						"%s (deployed %s)",
175
						substr($sha, 0, 8),
176
						$deploy->obj('LastEdited')->Ago()
177
					);
178
					$perEnvDeploys[$sha] = [
179
						'key' => $sha,
180
						'value' => $pastValue
181
					];
182
				}
183
			}
184
			if(!empty($perEnvDeploys)) {
185
				$redeploy[$envName] = array_values($perEnvDeploys);
186
			}
187
		}
188
		return $redeploy;
189
	}
190
}
191