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

DeployDispatcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 85
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
A index() 0 3 1
A history() 0 16 2
A Link() 0 3 1
A getModel() 0 3 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
		foreach($this->DeployHistory() as $deployment) {
0 ignored issues
show
Bug introduced by
The expression $this->DeployHistory() of type object<PaginatedList>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
61
			$data[] = [
62
				'CreatedDate' => $deployment->Created,
63
				'Branch' => $deployment->Branch,
64
				'Tags' => $deployment->getTags()->toArray(),
65
				'Changes' => $deployment->getDeploymentStrategy()->getChanges(),
66
				'CommitMessage' => $deployment->getCommitMessage(),
67
				'Deployer' => $deployment->Deployer()->getName(),
68
				'Approver' => $deployment->Approver()->getName(),
69
				'State' => $deployment->State,
70
			];
71
		}
72
		return $this->getAPIResponse(['history' => $data], 200);
73
	}
74
75
	/**
76
	 * @return string
77
	 */
78
	public function Link() {
79
		return \Controller::join_links($this->project->Link(), self::ACTION_DEPLOY);
80
	}
81
82
	/**
83
	 * @param string $name
84
	 *
85
	 * @return array
86
	 */
87
	public function getModel($name = '') {
88
		return [];
89
	}
90
91
}
92