Completed
Push — master ( 2d6803...417193 )
by Mateusz
03:07
created

DeployPlanDispatcher::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
		'gitrefs',
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
	 *
57
	 * @param \SS_HTTPRequest $request
58
	 *
59
	 * @return \HTMLText|\SS_HTTPResponse
60
	 */
61
	public function index(\SS_HTTPRequest $request) {
62
		$this->setCurrentActionType(self::ACTION_PLAN);
63
		return $this->customise([
64
			'Environment' => $this->environment
65
		])->renderWith(['Plan', 'DNRoot']);
66
	}
67
68
	/**
69
	 * @param SS_HTTPRequest $request
70
	 *
71
	 * @return string
72
	 */
73
	public function gitrefs(\SS_HTTPRequest $request) {
74
75
		$refs = [];
76
		$order = 0;
77
		$refs[] = [
78
			'id' => ++$order,
79
			'label' => "Branch version",
80
			"description" => "Deploy the latest version of a branch",
81
			"list" => $this->getGitBranches($this->project)
82
		];
83
84
		$refs[] = [
85
			'id' => ++$order,
86
			'label' => "Tag version",
87
			"description" => "Deploy a tagged release",
88
			"list" => $this->getGitTags($this->project)
89
		];
90
91
		// @todo: the original was a tree that was keyed by environment, the
92
		// front-end dropdown needs to be changed to support that. brrrr.
93
		$prevDeploys = [];
94
		foreach($this->getGitPrevDeploys($this->project) as $env) {
95
			foreach($env as $deploy) {
96
				$prevDeploys[] = $deploy;
97
			}
98
		}
99
		$refs[] = [
100
			'id' => ++$order,
101
			'label' => "Redeploy a release that was previously deployed (to any environment",
102
			"description" => "Deploy a previous release",
103
			"list" => $prevDeploys
104
		];
105
106
		$body = json_encode($refs, JSON_PRETTY_PRINT);
107
		$this->getResponse()->addHeader('Content-Type', 'application/json');
108
		$this->getResponse()->setBody($body);
109
		return $body;
110
	}
111
112
	/**
113
	 * Generate the data structure used by the frontend component.
114
	 *
115
	 * @param string $name of the component
116
	 *
117
	 * @return array
118
	 */
119
	public function getModel($name) {
120
		return [
121
			'APIEndpoint' => Director::absoluteBaseURL().$this->Link()
122
		];
123
	}
124
125
	/**
126
	 * @param $project
127
	 *
128
	 * @return array
129
	 */
130
	protected function getGitBranches($project) {
131
		$branches = [];
132
		foreach($project->DNBranchList() as $branch) {
133
			$branches[] = [
134
				'key' => $branch->SHA(),
135
				'value' => $branch->Name(),
136
			];
137
		}
138
		return $branches;
139
	}
140
141
	/**
142
	 * @param $project
143
	 *
144
	 * @return array
145
	 */
146
	protected function getGitTags($project) {
147
		$tags = [];
148
		foreach($project->DNTagList()->setLimit(null) as $tag) {
149
			$tags[] = [
150
				'key' => $tag->SHA(),
151
				'value' => $tag->Name(),
152
			];
153
		}
154
		return $tags;
155
	}
156
157
	/**
158
	 * @param $project
159
	 *
160
	 * @return array
161
	 */
162
	protected function getGitPrevDeploys($project) {
163
		$redeploy = [];
164 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...
165
			$envName = $dnEnvironment->Name;
166
			$perEnvDeploys = [];
167
			foreach($dnEnvironment->DeployHistory() as $deploy) {
168
				$sha = $deploy->SHA;
169
170
				// Check if exists to make sure the newest deployment date is used.
171
				if(!isset($perEnvDeploys[$sha])) {
172
					$pastValue = sprintf(
173
						"%s (deployed %s)",
174
						substr($sha, 0, 8),
175
						$deploy->obj('LastEdited')->Ago()
176
					);
177
					$perEnvDeploys[$sha] = [
178
						'key' => $sha,
179
						'value' => $pastValue
180
					];
181
				}
182
			}
183
			if(!empty($perEnvDeploys)) {
184
				$redeploy[$envName] = array_values($perEnvDeploys);
185
			}
186
		}
187
		return $redeploy;
188
	}
189
}
190