Completed
Push — master ( ad1e0c...2b0954 )
by Julius
200:16 queued 167:10
created

Application::registerCommentsEntity()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 1
nop 0
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\Notification\Notifier;
32
use OCP\AppFramework\App;
33
use OCA\Deck\Middleware\SharingMiddleware;
34
use OCP\Comments\CommentsEntityEvent;
35
use OCP\IGroup;
36
use OCP\IUser;
37
use OCP\IUserManager;
38
use OCP\IURLGenerator;
39
use OCP\INavigationManager;
40
41
class Application extends App {
42
43
	/**
44
	 * Application constructor.
45
	 *
46
	 * @param array $urlParams
47
	 * @throws \OCP\AppFramework\QueryException
48
	 */
49
	public function __construct(array $urlParams = array()) {
50
		parent::__construct('deck', $urlParams);
51
52
		$container = $this->getContainer();
53
		$server = $container->getServer();
54
55
		$container->registerService('SharingMiddleware', function() use ($server) {
56
			return new SharingMiddleware(
57
				$server->getLogger(),
58
				$server->getConfig()
59
			);
60
		});
61
		$container->registerMiddleWare('SharingMiddleware');
62
63
		$container->registerService('databaseType', function($container) {
64
			return $container->getServer()->getConfig()->getSystemValue('dbtype', 'sqlite');
65
		});
66
67
		$container->registerService('database4ByteSupport', function($container) {
68
			return $container->getServer()->getDatabaseConnection()->supports4ByteText();
69
		});
70
71
		// Delete user/group acl entries when they get deleted
72
		/** @var IUserManager $userManager */
73
		$userManager = $server->getUserManager();
74
		$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...
75
			// delete existing acl entries for deleted user
76
			/** @var AclMapper $aclMapper */
77
			$aclMapper = $container->query(AclMapper::class);
78
			$acls = $aclMapper->findByParticipant(Acl::PERMISSION_TYPE_USER, $user->getUID());
79
			foreach ($acls as $acl) {
80
				$aclMapper->delete($acl);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81
			}
82
			// delete existing user assignments
83
			$assignmentMapper = $container->query(AssignedUsersMapper::class);
84
			$assignments = $assignmentMapper->findByUserId($user->getUID());
85
			foreach ($assignments as $assignment) {
86
				$assignmentMapper->delete($assignment);
87
			}
88
		});
89
90
		/** @var IUserManager $userManager */
91
		$groupManager = $server->getGroupManager();
92
		$groupManager->listen('\OC\Group', 'postDelete', function(IGroup $group) use ($container) {
93
			/** @var AclMapper $aclMapper */
94
			$aclMapper = $container->query(AclMapper::class);
95
			$aclMapper->findByParticipant(Acl::PERMISSION_TYPE_GROUP, $group->getGID());
96
			$acls = $aclMapper->findByParticipant(Acl::PERMISSION_TYPE_GROUP, $group->getGID());
97
			foreach ($acls as $acl) {
98
				$aclMapper->delete($acl);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
99
			}
100
		});
101
102
	}
103
104
	public function registerNavigationEntry() {
105
		$container = $this->getContainer();
106
		$container->query(INavigationManager::class)->add(function() use ($container) {
107
			$urlGenerator = $container->query(IURLGenerator::class);
108
			return [
109
				'id' => 'deck',
110
				'order' => 10,
111
				'href' => $urlGenerator->linkToRoute('deck.page.index'),
112
				'icon' => $urlGenerator->imagePath('deck', 'deck.svg'),
113
				'name' => 'Deck',
114
			];
115
		});
116
	}
117
118
	public function registerNotifications() {
119
		$notificationManager = \OC::$server->getNotificationManager();
120
		$self = &$this;
121
		$notificationManager->registerNotifier(function() use (&$self) {
122
			return $self->getContainer()->query(Notifier::class);
123
		}, function() {
124
			return ['id' => 'deck', 'name' => 'Deck'];
125
		});
126
	}
127
128
	public function registerCommentsEntity() {
129
		$this->getContainer()->getServer()->getEventDispatcher()->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) {
130
			$event->addEntityCollection('deckCard', function($name) {
131
				/** @var CardMapper */
132
				$service = $this->getContainer()->query(CardMapper::class);
133
				try {
134
					$service->find((int) $name);
135
				} catch (\InvalidArgumentException $e) {
136
					return false;
137
				}
138
				return true;
139
			});
140
		});
141
		$this->registerCommentsEventHandler();
142
	}
143
144
	protected function registerCommentsEventHandler() {
145
		$this->getContainer()->getServer()->getCommentsManager()->registerEventHandler(function () {
146
			return $this->getContainer()->query(CommentEventHandler::class);
147
		});
148
	}
149
}
150