APIEnvironment   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 198
Duplicated Lines 25.25 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 10
dl 50
loc 198
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 0 31 3
A ping() 13 13 4
A deploy() 13 13 4
A Link() 0 7 1
A showRecord() 0 3 1
A createPing() 0 20 2
A getPing() 12 12 2
B createDeploy() 0 30 4
A getDeploy() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class APIEnvironment 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
		'ping',
10
		'deploy'
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 view this environment', 403);
20
		}
21
		switch($request->httpMethod()) {
22
			case 'GET':
23
				$href = Director::absoluteURL($this->record->Project()->APILink($this->record->Name));
24
				return $this->getAPIResponse(array(
25
					"name" => $this->record->Name,
26
					"project" => $this->record->Project()->Name,
27
					"href" => $href,
28
					"created" => $this->record->Created,
29
					"last-edited" => $this->record->LastEdited,
30
31
					// Stolen from https://github.com/kevinswiber/siren spec
32
					"actions" => array(
33
						array(
34
							"name" => "deploy",
35
							"method" =>  "POST",
36
							"href" => "$href/deploy",
37
							"type" => "application/json",
38
							"fields" => array(
39
								array("name" => "release", "type" => "text"),
40
							),
41
						)
42
					)
43
				));
44
			default:
45
				return $this->message('API not found', 404);
46
		}
47
	}
48
49
	/**
50
	 * @param \SS_HTTPRequest $request
51
	 * @return \SS_HTTPResponse
52
	 */
53 View Code Duplication
	public function ping(\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...
54
		if(!$this->record->canView($this->getMember())) {
55
			return $this->message('You are not authorized to do that on this environment', 403);
56
		}
57
		switch($request->httpMethod()) {
58
			case 'GET':
59
				return $this->getPing($this->getRequest()->param('ID'));
60
			case 'POST':
61
				return $this->createPing();
62
			default:
63
				return $this->message('API not found', 404);
64
		}
65
	}
66
67
	/**
68
	 * @deprecated 2.0.0 - moved to DeployDispatcher
69
	 *
70
	 * @param \SS_HTTPRequest $request
71
	 * @return \SS_HTTPResponse
72
	 */
73 View Code Duplication
	public function deploy(\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...
74
		if(!$this->record->canView($this->getMember())) {
75
			return $this->message('You are not authorized to do that on this environment', 403);
76
		}
77
		switch($request->httpMethod()) {
78
			case 'GET':
79
				return $this->getDeploy($this->getRequest()->param('ID'));
0 ignored issues
show
Deprecated Code introduced by
The method APIEnvironment::getDeploy() has been deprecated with message: 2.0.0 - moved to DeployDispatcher

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
80
			case 'POST':
81
				return $this->createDeploy();
0 ignored issues
show
Deprecated Code introduced by
The method APIEnvironment::createDeploy() has been deprecated with message: 2.0.0 - moved to DeployDispatcher

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
			default:
83
				return $this->message('API not found', 404);
84
		}
85
	}
86
87
	/**
88
	 * @return string
89
	 */
90
	public function Link() {
91
		return Controller::join_links(
92
			$this->parent->Link(),
93
			$this->record->Project()->Name,
94
			$this->record->Name
95
		);
96
	}
97
98
	/**
99
	 * @return SS_HTTPResponse
100
	 */
101
	protected function showRecord() {
102
		return $this->getAPIResponse($this->record->toMap());
103
	}
104
105
	/**
106
	 * @return SS_HTTPResponse
107
	 */
108
	protected function createPing() {
109
		if(!$this->record->canDeploy($this->getMember())) {
110
			return $this->message('You are not authorized to do that on this environment', 403);
111
		}
112
		$ping = DNPing::create();
113
		$ping->EnvironmentID = $this->record->ID;
114
		$ping->write();
115
		$ping->start();
116
117
		$location = Director::absoluteBaseURL() . $this->Link() . '/ping/' . $ping->ID;
118
		$output = array(
119
			'message' => 'Ping queued as job ' . $ping->ResqueToken,
120
			'href' => $location,
121
		);
122
123
		$response = $this->getAPIResponse($output);
124
		$response->setStatusCode(201);
125
		$response->addHeader('Location', $location);
126
		return $response;
127
	}
128
129
	/**
130
	 * @param int $ID
131
	 * @return SS_HTTPResponse
132
	 */
133 View Code Duplication
	protected function getPing($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...
134
		$ping = DNPing::get()->byID($ID);
135
		if(!$ping) {
136
			return $this->message('Ping not found', 404);
137
		}
138
		$output = array(
139
			'status' => $ping->ResqueStatus(),
140
			'message' => $ping->LogContent()
141
		);
142
143
		return $this->getAPIResponse($output);
144
	}
145
146
	/**
147
	 * @deprecated 2.0.0 - moved to DeployDispatcher
148
	 *
149
	 * @return SS_HTTPResponse
150
	 */
151
	protected function createDeploy() {
152
		if(!$this->record->canDeploy($this->getMember())) {
153
			return $this->message('You are not authorized to do that on this environment', 403);
154
		}
155
156
		$reqBody = $this->getRequestBody();
157
158
		if($reqBody === null) {
159
			return $this->message('the request body did not contain a valid JSON object.', 400);
160
		}
161
162
		if(empty($reqBody['release'])) {
163
			return $this->message('deploy requires a {"release": "sha1"} in the body of the request.', 400);
164
		}
165
166
		$strategy = new DeploymentStrategy($this->record, array(
0 ignored issues
show
Compatibility introduced by
$this->record of type object<DataObject> is not a sub-type of object<DNEnvironment>. It seems like you assume a child class of the class DataObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
167
			'sha' => $reqBody['release']
168
		));
169
		$deploy = $strategy->createDeployment();
170
		$deploy->start();
0 ignored issues
show
Documentation Bug introduced by
The method start does not exist on object<DNDeployment>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
171
		$location = Director::absoluteBaseURL() . $this->Link() . '/deploy/' . $deploy->ID;
172
		$output = array(
173
			'message' => 'Deploy queued as job ' . $deploy->ResqueToken,
174
			'href' => $location,
175
		);
176
		$response = $this->getAPIResponse($output);
177
		$response->setStatusCode(201);
178
		$response->addHeader('Location', $location);
179
		return $response;
180
	}
181
182
	/**
183
	 * @deprecated 2.0.0 - moved to DeployDispatcher
184
	 *
185
	 * @param int $id
186
	 * @return SS_HTTPResponse
187
	 */
188 View Code Duplication
	protected function getDeploy($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...
189
		$deploy = DNDeployment::get()->byID($id);
190
		if(!$deploy) {
191
			return $this->message('Deploy not found', 404);
192
		}
193
		$output = array(
194
			'status' => $deploy->ResqueStatus(),
195
			'message' => $deploy->LogContent()
196
		);
197
198
		return $this->getAPIResponse($output);
199
	}
200
}
201