Completed
Pull Request — master (#44)
by Joas
02:42
created

Application::registerNotificationNotifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[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\AnnouncementCenter\AppInfo;
25
26
use OCA\AnnouncementCenter\Activity\Extension;
27
use OCA\AnnouncementCenter\Comments\EventHandler;
28
use OCA\AnnouncementCenter\Controller\PageController;
29
use OCA\AnnouncementCenter\Manager;
30
use OCA\AnnouncementCenter\Notification\Notifier;
31
use OCP\AppFramework\App;
32
use OCP\Comments\CommentsEntityEvent;
33
34
class Application extends App {
35 5
	public function __construct (array $urlParams = array()) {
36 5
		parent::__construct('announcementcenter', $urlParams);
37 5
		$container = $this->getContainer();
38
39 5
		$container->registerAlias('PageController', PageController::class);
40 5
	}
41
42 3
	public function register() {
43 3
		$this->registerNavigationEntry();
44 3
		$this->registerAdminPanel();
45 3
		$this->registerActivityExtension();
46 3
		$this->registerNotificationNotifier();
47 3
		$this->registerCommentsEntity();
48 3
	}
49
50 3
	protected function registerNavigationEntry() {
51 3
		$server = $this->getContainer()->getServer();
52
53
		$server->getNavigationManager()->add(function() use ($server) {
54 1
			$urlGenerator = $server->getURLGenerator();
55 1
			$l = $server->getL10NFactory()->get('announcementcenter');
56
			return [
57 1
				'id' => 'announcementcenter',
58 1
				'order' => 10,
59 1
				'href' => $urlGenerator->linkToRoute('announcementcenter.page.index'),
60 1
				'icon' => $urlGenerator->imagePath('announcementcenter', 'announcementcenter.svg'),
61 1
				'name' => $l->t('Announcements'),
62 1
			];
63 3
		});
64 3
	}
65
66 3
	protected function registerAdminPanel() {
67 3
		\OCP\App::registerAdmin('announcementcenter', 'settings/admin');
68 3
	}
69
70 3
	protected function registerActivityExtension() {
71
		$this->getContainer()->getServer()->getActivityManager()->registerExtension(function() {
72 1
			return $this->getContainer()->query(Extension::class);
73 3
		});
74 3
	}
75
76 3
	protected function registerCommentsEntity() {
77
		$this->getContainer()->getServer()->getEventDispatcher()->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) {
78
			$event->addEntityCollection('announcement', function($name) {
79
				/** @var \OCA\AnnouncementCenter\Manager $manager */
80
				$manager = $this->getContainer()->query(Manager::class);
81
				try {
82
					$announcement = $manager->getAnnouncement((int) $name);
83
				} catch (\InvalidArgumentException $e) {
84
					return false;
85
				}
86
				return $announcement['comments'] !== false;
87
			});
88 3
		});
89
90
		$this->getContainer()->getServer()->getCommentsManager()->registerEventHandler(function() {
91
			return $this->getContainer()->query(EventHandler::class);
92 3
		});
93 3
	}
94
95 3
	protected function registerNotificationNotifier() {
96
		$this->getContainer()->getServer()->getNotificationManager()->registerNotifier(function() {
97 1
			return $this->getContainer()->query(Notifier::class);
98 3
		}, function() {
99 1
			$l = $this->getContainer()->getServer()->getL10NFactory()->get('announcementcenter');
100
			return [
101 1
				'id' => 'announcementcenter',
102 1
				'name' => $l->t('Announcements'),
103 1
			];
104 3
		});
105 3
	}
106
}
107