Passed
Push — master ( a842fd...195d63 )
by Julius
03:43 queued 51s
created

Application::__construct()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 8.9599
c 0
b 0
f 0
cc 4
nc 1
nop 1

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
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[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
24
namespace OCA\Deck\AppInfo;
25
26
use OCA\Deck\Activity\CommentEventHandler;
27
use OCA\Deck\Db\Acl;
28
use OCA\Deck\Db\AclMapper;
29
use OCA\Deck\Db\AssignedUsersMapper;
30
use OCA\Deck\Db\CardMapper;
31
use OCA\Deck\Middleware\ExceptionMiddleware;
32
use OCA\Deck\Notification\Notifier;
33
use OCP\AppFramework\App;
34
use OCA\Deck\Middleware\SharingMiddleware;
35
use OCP\Collaboration\Resources\IManager;
36
use OCP\Comments\CommentsEntityEvent;
37
use OCP\IGroup;
38
use OCP\IUser;
39
use OCP\IUserManager;
40
use OCP\IURLGenerator;
41
use OCP\INavigationManager;
42
43
class Application extends App {
44
45
	/**
46
	 * Application constructor.
47
	 *
48
	 * @param array $urlParams
49
	 * @throws \OCP\AppFramework\QueryException
50
	 */
51
	public function __construct(array $urlParams = array()) {
52
		parent::__construct('deck', $urlParams);
53
54
		$container = $this->getContainer();
55
		$server = $container->getServer();
56
57
		$container->registerService('ExceptionMiddleware', function() use ($server) {
58
			return new ExceptionMiddleware(
59
				$server->getLogger(),
60
				$server->getConfig()
61
			);
62
		});
63
		$container->registerMiddleWare('ExceptionMiddleware');
64
65
		$container->registerService('databaseType', function($container) {
66
			return $container->getServer()->getConfig()->getSystemValue('dbtype', 'sqlite');
67
		});
68
69
		$container->registerService('database4ByteSupport', function($container) {
70
			return $container->getServer()->getDatabaseConnection()->supports4ByteText();
71
		});
72
73
		// Delete user/group acl entries when they get deleted
74
		/** @var IUserManager $userManager */
75
		$userManager = $server->getUserManager();
76
		$userManager->listen('\OC\User', 'postDelete', function(IUser $user) use ($container) {
0 ignored issues
show
Bug introduced by
The method listen() does not seem to exist on object<OCP\IUserManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
			// delete existing acl entries for deleted user
78
			/** @var AclMapper $aclMapper */
79
			$aclMapper = $container->query(AclMapper::class);
80
			$acls = $aclMapper->findByParticipant(Acl::PERMISSION_TYPE_USER, $user->getUID());
81
			foreach ($acls as $acl) {
82
				$aclMapper->delete($acl);
83
			}
84
			// delete existing user assignments
85
			$assignmentMapper = $container->query(AssignedUsersMapper::class);
86
			$assignments = $assignmentMapper->findByUserId($user->getUID());
87
			foreach ($assignments as $assignment) {
88
				$assignmentMapper->delete($assignment);
89
			}
90
		});
91
92
		/** @var IUserManager $userManager */
93
		$groupManager = $server->getGroupManager();
94
		$groupManager->listen('\OC\Group', 'postDelete', function(IGroup $group) use ($container) {
0 ignored issues
show
Bug introduced by
The method listen() does not seem to exist on object<OCP\IGroupManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
			/** @var AclMapper $aclMapper */
96
			$aclMapper = $container->query(AclMapper::class);
97
			$aclMapper->findByParticipant(Acl::PERMISSION_TYPE_GROUP, $group->getGID());
98
			$acls = $aclMapper->findByParticipant(Acl::PERMISSION_TYPE_GROUP, $group->getGID());
99
			foreach ($acls as $acl) {
100
				$aclMapper->delete($acl);
101
			}
102
		});
103
104
		$this->registerCollaborationResources();
105
106
	}
107
108
	/**
109
	 * @throws \OCP\AppFramework\QueryException
110
	 */
111
	public function registerNavigationEntry() {
112
		$container = $this->getContainer();
113
		$container->query(INavigationManager::class)->add(function() use ($container) {
114
			$urlGenerator = $container->query(IURLGenerator::class);
115
			return [
116
				'id' => 'deck',
117
				'order' => 10,
118
				'href' => $urlGenerator->linkToRoute('deck.page.index'),
119
				'icon' => $urlGenerator->imagePath('deck', 'deck.svg'),
120
				'name' => 'Deck',
121
			];
122
		});
123
	}
124
125
	public function registerNotifications() {
126
		$notificationManager = \OC::$server->getNotificationManager();
127
		$self = &$this;
128
		$notificationManager->registerNotifier(function() use (&$self) {
129
			return $self->getContainer()->query(Notifier::class);
130
		}, function() {
131
			return ['id' => 'deck', 'name' => 'Deck'];
132
		});
133
	}
134
135
	/**
136
	 * @throws \OCP\AppFramework\QueryException
137
	 */
138
	public function registerCommentsEntity() {
139
		$this->getContainer()->getServer()->getEventDispatcher()->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) {
140
			$event->addEntityCollection('deckCard', function($name) {
141
				/** @var CardMapper */
142
				$service = $this->getContainer()->query(CardMapper::class);
143
				try {
144
					$service->find((int) $name);
145
				} catch (\InvalidArgumentException $e) {
146
					return false;
147
				}
148
				return true;
149
			});
150
		});
151
		$this->registerCommentsEventHandler();
152
	}
153
154
	/**
155
	 * @throws \OCP\AppFramework\QueryException
156
	 */
157
	protected function registerCommentsEventHandler() {
158
		$this->getContainer()->getServer()->getCommentsManager()->registerEventHandler(function () {
159
			return $this->getContainer()->query(CommentEventHandler::class);
160
		});
161
	}
162
163
	/**
164
	 * @throws \OCP\AppFramework\QueryException
165
	 */
166
	protected function registerCollaborationResources() {
167
		$version = \OC_Util::getVersion()[0];
168
		if ($version < 16) {
169
			return;
170
		}
171
172
		/**
173
		 * Register Collaboration ResourceProvider
174
		 */
175
		/** @var IManager $resourceManager */
176
		$resourceManager = $this->getContainer()->query(IManager::class);
177
		$resourceManager->registerResourceProvider(\OCA\Deck\Collaboration\Resources\ResourceProvider::class);
178
		\OC::$server->getEventDispatcher()->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () {
179
			\OCP\Util::addScript('deck', 'build/collections');
180
		});
181
	}
182
}
183