Application::register()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\AnnouncementCenter\AppInfo;
26
27
use OCA\AnnouncementCenter\Controller\PageController;
28
use OCA\AnnouncementCenter\Manager;
29
use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException;
30
use OCA\AnnouncementCenter\Notification\Notifier;
31
use OCP\AppFramework\App;
32
use OCP\Comments\CommentsEntityEvent;
33
34
class Application extends App {
35
36 15
	public function __construct() {
37 15
		parent::__construct('announcementcenter');
38 15
		$container = $this->getContainer();
39
40 15
		$container->registerAlias('PageController', PageController::class);
41 15
	}
42
43 1
	public function register() {
44 1
		$this->registerNotificationNotifier();
45 1
		$this->registerCommentsEntity();
46 1
	}
47
48 1
	protected function registerCommentsEntity() {
49
		$this->getContainer()->getServer()->getEventDispatcher()->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) {
50
			$event->addEntityCollection('announcement', function($name) {
51
				/** @var Manager $manager */
52
				$manager = $this->getContainer()->query(Manager::class);
53
				try {
54
					$announcement = $manager->getAnnouncement((int) $name);
55
				} catch (AnnouncementDoesNotExistException $e) {
56
					return false;
57
				}
58
				return (bool) $announcement->getAllowComments();
59
			});
60 1
		});
61 1
	}
62
63 1
	protected function registerNotificationNotifier() {
64 1
		$this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class);
65 1
	}
66
}
67