BackendController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 26.92 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%
Metric Value
wmc 9
lcom 1
cbo 2
dl 14
loc 52
ccs 0
cts 36
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C getConnectors() 0 22 7
A enableBackend() 7 7 1
A backendStatus() 7 7 1

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
 * @author Nicolas Mora
4
 * @copyright 2014 Nicolas Mora ([email protected])
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later.
7
 * See the COPYING-README file.
8
 */
9
10
namespace OCA\Contacts\Controller;
11
12
use OCA\Contacts\App,
13
	OCA\Contacts\JSONResponse,
14
	OCA\Contacts\Utils\JSONSerializer,
15
	OCA\Contacts\Controller,
16
	OCP\AppFramework\Http;
17
18
/**
19
 * Controller class For Address Books
20
 */
21
class BackendController extends Controller {
22
23
	/**
24
	 * @NoAdminRequired
25
	 * @NoCSRFRequired
26
	 */
27
	public function getConnectors() {
28
		$response = new JSONResponse();
29
		$prefix = "backend_ldap_";
30
		$suffix = "_connector.xml";
31
		$path = __DIR__ . "/../../formats/";
32
		$files = scandir($path);
33
		$formats = array();
34
		foreach ($files as $file) {
35
			if (!strncmp($file, $prefix, strlen($prefix)) && substr($file, - strlen($suffix)) === $suffix) {
36
				if (file_exists($path.$file)) {
37
					$format = simplexml_load_file ( $path.$file );
38
					if ($format) {
39
						if (isset($format['name'])) {
40
							$formatId = substr($file, strlen($prefix), - strlen($suffix));
41
							$formats[] = array('id' => $formatId, 'name' => (string)$format['name'], 'xml' => $format->asXML());
42
						}
43
					}
44
				}
45
			}
46
		}
47
		return $response->setData($formats);
48
	}
49
	
50
	/**
51
	 * @NoAdminRequired
52
	 * @NoCSRFRequired
53
	 */
54 View Code Duplication
	public function enableBackend() {
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...
55
		$response = new JSONResponse();
56
		$params = $this->request->urlParams;
57
		$backend = $params['backend'];
58
		$enable = $params['enable'];
59
		return $response->setData(\OCP\Config::setAppValue('contacts', 'backend_'.$backend, $enable));
60
	}
61
	/**
62
	 * @NoAdminRequired
63
	 * @NoCSRFRequired
64
	 */
65 View Code Duplication
	public function backendStatus() {
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...
66
		$response = new JSONResponse();
67
		$params = $this->request->urlParams;
68
		$backend = $params['backend'];
69
		$enabled = \OCP\Config::getAppValue('contacts', 'backend_'.$backend, "false");
70
		return $response->setData($enabled);
71
	}
72
}
73
74