Completed
Push — master ( b4df57...e6895c )
by Lukas
26s
created

Application::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 81
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 46
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 81
rs 8.8076

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2016, ownCloud, Inc.
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
namespace OCA\DAV\AppInfo;
24
25
use OCA\DAV\CalDAV\BirthdayService;
26
use OCA\DAV\CalDAV\CalDavBackend;
27
use OCA\DAV\CardDAV\CardDavBackend;
28
use OCA\DAV\CardDAV\ContactsManager;
29
use OCA\DAV\CardDAV\SyncJob;
30
use OCA\DAV\CardDAV\SyncService;
31
use OCA\DAV\Connector\Sabre\Principal;
32
use OCA\DAV\DAV\GroupPrincipalBackend;
33
use OCA\DAV\HookManager;
34
use OCA\DAV\Migration\Classification;
35
use OCA\DAV\Migration\GenerateBirthdays;
36
use \OCP\AppFramework\App;
37
use OCP\AppFramework\IAppContainer;
38
use OCP\Contacts\IManager;
39
use OCP\IUser;
40
use Sabre\VObject\Reader;
41
use Symfony\Component\EventDispatcher\GenericEvent;
42
43
class Application extends App {
44
45
	/**
46
	 * Application constructor.
47
	 *
48
	 * @param array $urlParams
49
	 */
50
	public function __construct (array $urlParams=array()) {
51
		parent::__construct('dav', $urlParams);
52
53
		$container = $this->getContainer();
54
		$container->registerService('ContactsManager', function($c) {
55
			/** @var IAppContainer $c */
56
			return new ContactsManager(
57
				$c->query('CardDavBackend')
58
			);
59
		});
60
61
		$container->registerService('HookManager', function($c) {
62
			/** @var IAppContainer $c */
63
			return new HookManager(
64
				$c->getServer()->getUserManager(),
65
				$c->query('SyncService'),
66
				$c->query('CalDavBackend'),
67
				$c->query('CardDavBackend')
68
			);
69
		});
70
71
		$container->registerService('SyncService', function($c) {
72
			/** @var IAppContainer $c */
73
			return new SyncService(
74
				$c->query('CardDavBackend'),
75
				$c->getServer()->getUserManager(),
76
				$c->getServer()->getLogger()
77
			);
78
		});
79
80
		$container->registerService('CardDavBackend', function($c) {
81
			/** @var IAppContainer $c */
82
			$db = $c->getServer()->getDatabaseConnection();
83
			$dispatcher = $c->getServer()->getEventDispatcher();
84
			$principal = new Principal(
85
				$c->getServer()->getUserManager(),
86
				$c->getServer()->getGroupManager()
87
			);
88
			return new CardDavBackend($db, $principal, $dispatcher);
89
		});
90
91
		$container->registerService('CalDavBackend', function($c) {
92
			/** @var IAppContainer $c */
93
			$db = $c->getServer()->getDatabaseConnection();
94
			$principal = new Principal(
95
				$c->getServer()->getUserManager(),
96
				$c->getServer()->getGroupManager()
97
			);
98
			return new CalDavBackend($db, $principal);
99
		});
100
101
		$container->registerService('BirthdayService', function($c) {
102
			/** @var IAppContainer $c */
103
			$g = new GroupPrincipalBackend(
104
				$c->getServer()->getGroupManager()
105
			);
106
			return new BirthdayService(
107
				$c->query('CalDavBackend'),
108
				$c->query('CardDavBackend'),
109
				$g
110
			);
111
		});
112
113
		$container->registerService('OCA\DAV\Migration\Classification', function ($c) {
114
			/** @var IAppContainer $c */
115
			return new Classification(
116
				$c->query('CalDavBackend'),
117
				$c->getServer()->getUserManager()
118
			);
119
		});
120
121
		$container->registerService('OCA\DAV\Migration\GenerateBirthdays', function ($c) {
122
			/** @var IAppContainer $c */
123
			/** @var BirthdayService $b */
124
			$b = $c->query('BirthdayService');
125
			return new GenerateBirthdays(
126
				$b,
127
				$c->getServer()->getUserManager()
128
			);
129
		});
130
	}
131
132
	/**
133
	 * @param IManager $contactsManager
134
	 * @param string $userID
135
	 */
136
	public function setupContactsProvider(IManager $contactsManager, $userID) {
137
		/** @var ContactsManager $cm */
138
		$cm = $this->getContainer()->query('ContactsManager');
139
		$cm->setupContactsProvider($contactsManager, $userID);
140
	}
141
142
	public function registerHooks() {
143
		/** @var HookManager $hm */
144
		$hm = $this->getContainer()->query('HookManager');
145
		$hm->setup();
146
147 View Code Duplication
		$listener = function($event) {
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...
148
			if ($event instanceof GenericEvent) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\EventDispatcher\GenericEvent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
149
				/** @var BirthdayService $b */
150
				$b = $this->getContainer()->query('BirthdayService');
151
				$b->onCardChanged(
152
					$event->getArgument('addressBookId'),
153
					$event->getArgument('cardUri'),
154
					$event->getArgument('cardData')
155
				);
156
			}
157
		};
158
159
		$dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
160
		$dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', $listener);
161
		$dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $listener);
162 View Code Duplication
		$dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', function($event) {
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...
163
			if ($event instanceof GenericEvent) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\EventDispatcher\GenericEvent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
164
				/** @var BirthdayService $b */
165
				$b = $this->getContainer()->query('BirthdayService');
166
				$b->onCardDeleted(
167
					$event->getArgument('addressBookId'),
168
					$event->getArgument('cardUri')
169
				);
170
			}
171
		});
172
	}
173
174
	public function getSyncService() {
175
		return $this->getContainer()->query('SyncService');
176
	}
177
178
}
179