Completed
Push — master ( 515646...bf1f12 )
by Thomas
03:48
created

App   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 64.44%
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 135
ccs 29
cts 45
cp 0.6444
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A getBackend() 0 8 3
A getAddressBooksForUser() 0 17 4
B getAddressBook() 0 21 5
A getContact() 0 4 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 30 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @author Thomas Tanghus
4
 * @copyright 2013-2014 Thomas Tanghus ([email protected])
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later.
8
 * See the COPYING-README file.
9
 */
10
11
namespace OCA\Contacts;
12
13
use Sabre\VObject,
14
	OCP\AppFramework,
15
	OCA\Contacts\Controller\AddressBookController,
16
	OCA\Contacts\Controller\BackendController,
17
	OCA\Contacts\Controller\GroupController,
18
	OCA\Contacts\Controller\ContactController,
19
	OCA\Contacts\Controller\ContactPhotoController,
20
	OCA\Contacts\Controller\SettingsController,
21
	OCA\Contacts\Controller\ImportController;
22
23
/**
24
 * This class manages our app actions
25
 *
26
 * TODO: Merge in Dispatcher
27
 */
28 1
App::$l10n = \OC::$server->getL10N('contacts');
29
30
class App {
31
32
	/**
33
	* @brief Categories of the user
34
	* @var OC_VCategories
35
	*/
36
	public static $categories = null;
37
38
	/**
39
	 * @brief language object for calendar app
40
	 *
41
	 * @var \OCP\IL10N
42
	 */
43
	public static $l10n;
44
45
	/**
46
	 * An array holding the current users address books.
47
	 * @var array
48
	 */
49
	protected static $addressBooks = array();
50
	/**
51
	* If backends are added to this map, they will be automatically mapped
52
	* to their respective classes, if constructed with the 'getBackend' method.
53
	*
54
	* @var array
55
	*/
56
	public static $backendClasses = array(
57
		'local' => 'OCA\Contacts\Backend\Database',
58
		'shared' => 'OCA\Contacts\Backend\Shared',
59
//		'localusers' => 'OC\Contacts\Backend\LocalUsers',
60
	);
61
62 2
	public function __construct(
63
		$user = null,
64
		$addressBooksTableName = '*PREFIX*addressbook',
65
		$backendsTableName = '*PREFIX*addressbooks_backend',
66
		$dbBackend = null
67
	) {
68 2
		$this->user = $user ? $user : \OC::$server->getUserSession()->getUser()->getUId();
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69 2
		$this->addressBooksTableName = $addressBooksTableName;
0 ignored issues
show
Bug introduced by
The property addressBooksTableName does not seem to exist. Did you mean addressBooks?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70 2
		$this->backendsTableName = $backendsTableName;
0 ignored issues
show
Bug introduced by
The property backendsTableName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71 2
		$this->dbBackend = $dbBackend
0 ignored issues
show
Bug introduced by
The property dbBackend does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72 2
			? $dbBackend
73 2
			: new Backend\Database($user);
74 2
		if (\OCP\Config::getAppValue('contacts', 'backend_ldap', "false") === "true") {
75
			self::$backendClasses['ldap'] = 'OCA\Contacts\Backend\Ldap';
76
		}
77 2
	}
78
79
	/**
80
	* Gets backend by name.
81
	*
82
	* @param string $name
83
	* @return \Backend\AbstractBackend
84
	*/
85 2
	public function getBackend($name) {
86 2
		$name = $name ? $name : 'local';
87 2
		if (isset(self::$backendClasses[$name])) {
88 2
			return new self::$backendClasses[$name]($this->user);
89
		} else {
90
			throw new \Exception('No backend for: ' . $name, '404');
91
		}
92
	}
93
94
	/**
95
	 * Return all registered address books for current user.
96
	 * For now this is hard-coded to using the Database and
97
	 * Shared backends, but eventually admins will be able to
98
	 * register additional backends, and users will be able to
99
	 * subscribe to address books using those backends.
100
	 *
101
	 * @return AddressBook[]
102
	 */
103
	public function getAddressBooksForUser() {
104
		if (!self::$addressBooks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::$addressBooks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
			foreach (array_keys(self::$backendClasses) as $backendName) {
106
				$backend = self::getBackend($backendName, $this->user);
0 ignored issues
show
Unused Code introduced by
The call to App::getBackend() has too many arguments starting with $this->user.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
107
				$addressBooks = $backend->getAddressBooksForUser();
108
109
				foreach ($addressBooks as $addressBook) {
110
					$addressBook['backend'] = $backendName;
111
					self::$addressBooks[] = new AddressBook($backend, $addressBook);
112
				}
113
114
			}
115
116
		}
117
118
		return self::$addressBooks;
119
	}
120
121
	/**
122
	 * Get an address book from a specific backend.
123
	 *
124
	 * @param string $backendName
125
	 * @param string $addressbookid
126
	 * @return AddressBook|null
127
	 */
128 2
	public function getAddressBook($backendName, $addressbookid) {
129
		//\OCP\Util::writeLog('contacts', __METHOD__ . ': '. $backendName . ', ' . $addressbookid, \OCP\Util::DEBUG);
130 2
		foreach (self::$addressBooks as $addressBook) {
131 2
			if ($addressBook->getBackend()->name === $backendName
132 2
				&& $addressBook->getId() === $addressbookid
133 2
			) {
134 1
				return $addressBook;
135
			}
136 2
		}
137
138 2
		$backend = self::getBackend($backendName, $this->user);
0 ignored issues
show
Unused Code introduced by
The call to App::getBackend() has too many arguments starting with $this->user.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
139 2
		$info = $backend->getAddressBook($addressbookid);
140
141 2
		if (!$info) {
142
			throw new \Exception(self::$l10n->t('Address book not found'), 404);
143
		}
144
		
145 2
		$addressBook = new AddressBook($backend, $info);
146 2
		self::$addressBooks[] = $addressBook;
147 2
		return $addressBook;
148
	}
149
150
	/**
151
	 * Get a Contact from an address book from a specific backend.
152
	 *
153
	 * @param string $backendName
154
	 * @param string $addressbookid
155
	 * @param string $id - Contact id
156
	 * @return Contact|null
157
	 *
158
	 */
159 2
	public function getContact($backendName, $addressbookid, $id) {
160 2
		$addressBook = $this->getAddressBook($backendName, $addressbookid);
161 2
		return $addressBook->getChild($id);
162
	}
163
164
}
165