APIProject::getFetch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
class APIProject extends APINoun {
0 ignored issues
show
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...
4
5
	/**
6
	 * @var array
7
	 */
8
	private static $allowed_actions = array(
9
		'index',
10
		'fetch'
11
	);
12
13
	/**
14
	 * @param \SS_HTTPRequest $request
15
	 * @return \SS_HTTPResponse
16
	 */
17
	public function index(\SS_HTTPRequest $request) {
18
		if(!$this->record->canView($this->getMember())) {
19
			return $this->message('You are not authorized to this environment', 403);
20
		}
21
22
		switch($request->httpMethod()) {
23
			case 'GET':
24
				$response = array(
25
					"name" => $this->record->Name,
26
					"href" => Director::absoluteURL($this->record->APILink("")),
27
					"created" => $this->record->Created,
28
					"last-edited" => $this->record->LastEdited,
29
					"disk-quota-mb" => $this->record->DiskQuotaMB,
30
					"environments" => array(),
31
				);
32
				foreach($this->record->DNEnvironmentList() as $environment) {
33
					$response['environments'][] = array(
34
						'name' => $environment->Name,
35
						'href' => Director::absoluteURL($this->record->APILink($environment->Name)),
36
					);
37
				}
38
39
				return $this->getAPIResponse($response);
40
			default:
41
				return $this->message('API not found', 404);
42
		}
43
	}
44
45
	/**
46
	 * @param \SS_HTTPRequest $request
47
	 * @return \SS_HTTPResponse
48
	 */
49 View Code Duplication
	public function fetch(\SS_HTTPRequest $request) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
		if(!$this->record->canView($this->getMember())) {
51
			return $this->message('You are not authorized to do that on this environment', 403);
52
		}
53
		switch($request->httpMethod()) {
54
			case 'GET':
55
				return $this->getFetch($this->getRequest()->param('ID'));
56
			case 'POST':
57
				return $this->createFetch();
58
			default:
59
				return $this->message('API not found', 404);
60
		}
61
	}
62
63
	/**
64
	 * @param int $ID
65
	 * @return SS_HTTPResponse
66
	 */
67 View Code Duplication
	protected function getFetch($ID) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
68
		$ping = DNGitFetch::get()->byID($ID);
69
		if(!$ping) {
70
			return $this->message('Fetch not found', 404);
71
		}
72
		$output = array(
73
			'status' => $ping->ResqueStatus(),
74
			'message' => $ping->LogContent()
75
		);
76
77
		return $this->getAPIResponse($output);
78
	}
79
80
	/**
81
	 * @return SS_HTTPResponse
82
	 */
83 View Code Duplication
	protected function createFetch() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
84
		/** @var DNGitFetch $fetch */
85
		$fetch = DNGitFetch::create();
86
		$fetch->ProjectID = $this->record->ID;
87
		$fetch->write();
88
		$fetch->start();
89
90
		$location = Director::absoluteBaseURL() . $this->Link() . '/fetch/' . $fetch->ID;
91
		$output = array(
92
			'message' => 'Fetch queued as job ' . $fetch->ResqueToken,
93
			'href' => $location,
94
		);
95
96
		$response = $this->getAPIResponse($output);
97
		$response->setStatusCode(201);
98
		$response->addHeader('Location', $location);
99
		return $response;
100
	}
101
102
	/**
103
	 * @return string
104
	 */
105
	public function Link() {
106
		return Controller::join_links(
107
			$this->parent->Link(),
108
			$this->record->Name
109
		);
110
	}
111
}
112