Completed
Pull Request — master (#625)
by Sean
03:21
created

DeployDispatcher::history()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 40
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 40
rs 8.439
cc 6
eloc 29
nc 24
nop 1
1
<?php
2
3
/**
4
 * This dispatcher takes care of updating and returning information about this
5
 * projects git repository
6
 */
7
class DeployDispatcher extends Dispatcher {
8
9
	const ACTION_DEPLOY = 'deploy';
10
11
	/**
12
	 * @var array
13
	 */
14
	private static $action_types = [
15
		self::ACTION_DEPLOY
16
	];
17
18
	/**
19
	 * @var array
20
	 */
21
	public static $allowed_actions = [
22
		'history'
23
	];
24
25
	/**
26
	 * @var \DNProject
27
	 */
28
	protected $project = null;
29
30
	/**
31
	 * @var \DNEnvironment
32
	 */
33
	protected $environment = null;
34
35
	public function init() {
36
		parent::init();
37
38
		$this->project = $this->getCurrentProject();
39
40
		if(!$this->project) {
41
			return $this->project404Response();
42
		}
43
	}
44
45
	/**
46
	 *
47
	 * @param \SS_HTTPRequest $request
48
	 *
49
	 * @return \HTMLText|\SS_HTTPResponse
50
	 */
51
	public function index(\SS_HTTPRequest $request) {
52
		return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302);
53
	}
54
55
	/**
56
	 * @return SS_HTTPResponse
57
	 */
58
	public function history(SS_HTTPRequest $request) {
59
		$data = [];
60
		$list = $this->DeployHistory();
61
		$page = $request->getVar('page') ?: 1;
62
		if ($page > $list->TotalPages()) {
63
			$page = 1;
64
		}
65
		if ($page < 1) {
66
			$page = 1;
67
		}
68
		$start = ($page - 1) * $list->getPageLength();
69
		$list->setPageStart((int) $start);
70
		if (empty($list)) {
71
			return $this->getAPIResponse(['message' => 'No deploy history'], 404);
72
		}
73
74
		foreach ($list as $deployment) {
75
			$data[] = [
76
				'CreatedDate' => $deployment->Created,
77
				'Branch' => $deployment->Branch,
78
				'Tags' => $deployment->getTags()->toArray(),
79
				'Changes' => $deployment->getDeploymentStrategy()->getChanges(),
80
				'CommitMessage' => $deployment->getCommitMessage(),
81
				'Deployer' => $deployment->Deployer()->getName(),
82
				'Approver' => $deployment->Approver()->getName(),
83
				'State' => $deployment->State,
84
			];
85
		}
86
87
		return $this->getAPIResponse(
88
			[
89
				'list' => $data,
90
				'pagelength' => $list->getPageLength(),
91
				'pagestart' => $list->getPageStart(),
92
				'totalpages' => $list->TotalPages(),
93
				'currentpage' => $list->CurrentPage()
94
			],
95
			200
96
		);
97
	}
98
99
	/**
100
	 * @return string
101
	 */
102
	public function Link() {
103
		return \Controller::join_links($this->project->Link(), self::ACTION_DEPLOY);
104
	}
105
106
	/**
107
	 * @param string $name
108
	 *
109
	 * @return array
110
	 */
111
	public function getModel($name = '') {
112
		return [];
113
	}
114
115
}
116