APINoun   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 25.64 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 5
dl 30
loc 117
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A init() 0 4 1
A getRecord() 0 3 1
A getMember() 0 3 1
A respondWithJSON() 9 9 3
A respondWithText() 9 9 3
A getRequestBody() 0 3 1
A message() 0 8 1
A getAPIResponse() 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
 * Base class for the APINouns
4
 */
5
class APINoun extends Controller {
6
7
	/**
8
	 * @var controller
9
	 */
10
	protected $parent = null;
11
12
	/**
13
	 * @var DataObject
14
	 */
15
	protected $record = null;
16
17
	/**
18
	 * @var Member
19
	 */
20
	protected $member = null;
21
22
	/**
23
	 * Holds the url segment for this admin
24
	 *
25
	 * @param Controller|null $parent
26
	 * @param DataObject|null $record
27
	 */
28
	public function __construct(\Controller $parent = null, DataObject $record = null) {
29
		$this->record = $record;
30
		$this->parent = $parent;
31
		parent::__construct();
32
	}
33
34
	/**
35
	 * Enable basic auth on the API
36
	 */
37
	public function init() {
38
		$this->member = BasicAuth::requireLogin('Deploynaut API');
39
		parent::init();
40
	}
41
42
	/**
43
	 * @return DataObject
44
	 */
45
	public function getRecord() {
46
		return $this->record;
47
	}
48
49
	/**
50
	 * @return Member
51
	 */
52
	public function getMember() {
53
		return $this->member;
54
	}
55
56
	/**
57
	 * @param array $output
58
	 * @return SS_HTTPResponse
59
	 */
60 View Code Duplication
	protected function getAPIResponse($output) {
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...
61
		$response = $this->getResponse();
62
		if($this->respondWithText()) {
63
			$body = print_r($output, true);
64
			$response->addHeader('Content-Type', 'text/plain');
65
		} else {
66
			$body = Convert::raw2json($output);
67
			$response->addHeader('Content-Type', 'application/json');
68
		}
69
		$response->setBody($body);
70
		return $response;
71
	}
72
73
	/**
74
	 * @return boolean
75
	 */
76 View Code Duplication
	protected function respondWithJSON() {
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...
77
		if($this->getRequest()->getExtension() == 'json') {
78
			return true;
79
		}
80
		if(strpos($this->getRequest()->getHeader('Accept'), 'application/json') !== false) {
81
			return true;
82
		}
83
		return false;
84
	}
85
86
	/**
87
	 * @return boolean
88
	 */
89 View Code Duplication
	protected function respondWithText() {
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...
90
		if($this->getRequest()->getExtension() == 'txt') {
91
			return true;
92
		}
93
		if(strpos($this->getRequest()->getHeader('Accept'), 'text/plain') !== false) {
94
			return true;
95
		}
96
		return false;
97
	}
98
99
	/**
100
	 * @return array|null
101
	 */
102
	protected function getRequestBody() {
103
		return Convert::json2array($this->getRequest()->getBody());
104
	}
105
106
	/**
107
	 * Return a simple response with a message
108
	 *
109
	 * @param string $message
110
	 * @param int $statusCode
111
	 * @return SS_HTTPResponse
112
	 */
113
	protected function message($message, $statusCode) {
114
		$response = $this->getAPIResponse(array(
115
			'message' => $message,
116
			'statusCode' => $statusCode
117
		));
118
		$response->setStatusCode($statusCode);
119
		return $response;
120
	}
121
}
122