DeploynautAPI::getProject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class DeploynautAPI
5
 *
6
 * Entry point for the deploynaut API
7
 *
8
 * /naut/api/projectname/
9
 * /naut/api/projectname/environmentname
10
 *
11
 * @todo It might be good to break this out to a controller chain so that
12
 * "/naut/api/projectname/environmentname" controller chain becomes
13
 * DeploynautAPI > APIProject > APIEnvironment
14
 *
15
 */
16
class DeploynautAPI extends APINoun {
0 ignored issues
show
Coding Style introduced by
The property $url_handlers is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $allowed_actions is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
17
18
	/**
19
	 * Default URL handlers - (Action)/(ID)//(OtherID)
20
	 *
21
	 * @var array
22
	 */
23
	private static $url_handlers = array(
24
		'' => 'listProjects',
25
		'$Project//fetch' => 'project',
26
		'$Project/$Environment!' => 'environment',
27
		'$Project/' => 'project',
28
	);
29
30
	/**
31
	 * @var array
32
	 */
33
	public static $allowed_actions = array(
34
		'project',
35
		'environment',
36
		'listProjects'
37
	);
38
39
	/**
40
	 * @var string
41
	 */
42
	protected $link = 'deploynaut/api';
43
44
	/**
45
	 * @param \SS_HTTPRequest $request
46
	 * @return \SS_HTTPResponse
47
	 */
48
	public function listProjects(\SS_HTTPRequest $request) {
49
		$response = array(
50
			'href' => Director::absoluteURL($this->Link()),
51
			'projects' => array(),
52
		);
53
54
		if($request->httpMethod() != 'GET') {
55
			return $this->message('API not found', 404);
56
		}
57
58
		foreach(DNProject::get() as $item) {
59
			if($item->canView($this->getMember())) {
60
				$response['projects'][] = array(
61
					"name" => $item->Name,
62
					"href" => Director::absoluteURL($item->APILink("")),
63
				);
64
			}
65
		}
66
67
		return $this->getAPIResponse($response);
68
	}
69
70
	/**
71
	 * Controller Action
72
	 *
73
	 * @param \SS_HTTPRequest $request
74
	 * @return APIProject
75
	 */
76
	public function project(\SS_HTTPRequest $request) {
77
		$project = $this->getProject();
78
		if(!$project) {
79
			return $this->project404Response();
80
		}
81
		return new APIProject($this, $project);
82
	}
83
84
	/**
85
	 * Controller Action
86
	 *
87
	 * @param \SS_HTTPRequest $request
88
	 * @return APIEnvironment
89
	 */
90
	public function environment(\SS_HTTPRequest $request) {
91
		$project = $this->getProject();
92
		if(!$project) {
93
			return $this->project404Response();
94
		}
95
96
		$environment = $this->getEnvironment();
97
		if(!$environment) {
98
			return $this->environment404Response();
99
		}
100
		return new APIEnvironment($this, $environment);
101
	}
102
103
	/**
104
	 * @return SS_HTTPResponse
105
	 */
106
	protected function project404Response() {
107
		$projectName = Convert::raw2xml($this->getRequest()->latestParam('Project'));
108
		return new SS_HTTPResponse('Project "' . $projectName . '" not found.', 404);
109
	}
110
111
	/**
112
	 * @return SS_HTTPResponse
113
	 */
114
	protected function environment404Response() {
115
		$envName = Convert::raw2xml($this->getRequest()->latestParam('Environment'));
116
		return new SS_HTTPResponse('Environment "' . $envName . '" not found.', 404);
117
	}
118
119
	/**
120
	 * Get project from URL
121
	 *
122
	 * @return DNProject
123
	 */
124
	protected function getProject() {
125
		$projectName = $this->getRequest()->param('Project');
126
		return DNProject::get()->filter('Name', $projectName)->first();
127
	}
128
129
	/**
130
	 * Get environment from URL
131
	 *
132
	 * @return DNEnvironment
133
	 */
134
	protected function getEnvironment() {
135
		$projectName = $this->getRequest()->param('Project');
136
		$project = DNProject::get()->filter('Name', $projectName)->first();
137
		$environmentName = $this->getRequest()->param('Environment');
138
		return $project->Environments()->filter('Name', $environmentName)->first();
139
	}
140
141
	/**
142
	 * @return string
143
	 */
144
	public function Link() {
145
		return 'naut/api';
146
	}
147
}
148