Completed
Push — master ( af3950...9dafc5 )
by Lukas
50:35 queued 40:31
created

OCSMiddleware   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 7.27 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 4
loc 55
rs 10
wmc 8
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B afterException() 0 18 5
A getFormat() 4 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
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\AppFramework\Middleware;
24
25
use OC\AppFramework\Http;
26
use OCP\AppFramework\Http\OCSResponse;
27
use OCP\AppFramework\OCS\OCSException;
28
use OCP\AppFramework\OCSController;
29
use OCP\IRequest;
30
use OCP\AppFramework\Middleware;
31
32
class OCSMiddleware extends Middleware {
33
34
	/** @var IRequest */
35
	private $request;
36
37
	/**
38
	 * @param IRequest $request
39
	 */
40
	public function __construct(IRequest $request) {
41
		$this->request = $request;
42
	}
43
44
	/**
45
	 * @param \OCP\AppFramework\Controller $controller
46
	 * @param string $methodName
47
	 * @param \Exception $exception
48
	 * @throws \Exception
49
	 * @return OCSResponse
50
	 */
51
	public function afterException($controller, $methodName, \Exception $exception) {
52
		if ($controller instanceof OCSController && $exception instanceof OCSException) {
53
			$format = $this->getFormat($controller);
54
55
			$code = $exception->getCode();
56
			if ($code === 0) {
57
				$code = Http::STATUS_INTERNAL_SERVER_ERROR;
58
			}
59
			$response = new OCSResponse($format, $code, $exception->getMessage());
60
61
			if ($this->request->getScriptName() === '/ocs/v2.php') {
62
				$response->setStatus($code);
63
			}
64
			return $response;
65
		}
66
67
		throw $exception;
68
	}
69
70
	/**
71
	 * @param \OCP\AppFramework\Controller $controller
72
	 * @return string
73
	 */
74
	private function getFormat($controller) {
75
		// get format from the url format or request format parameter
76
		$format = $this->request->getParam('format');
77
78
		// if none is given try the first Accept header
79 View Code Duplication
		if($format === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80
			$headers = $this->request->getHeader('Accept');
81
			$format = $controller->getResponderByHTTPHeader($headers, 'xml');
82
		}
83
84
		return $format;
85
	}
86
}
87