Completed
Push — master ( 0876bf...6c1183 )
by
unknown
01:40
created

Application::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\Controller\PageController;
27
use OCA\AnnouncementCenter\Manager;
28
use OCA\AnnouncementCenter\Notification\Notifier;
29
use OCP\AppFramework\App;
30
use OCP\Comments\CommentsEntityEvent;
31
32
class Application extends App {
33
34 13
	public function __construct() {
35 13
		parent::__construct('announcementcenter');
36 13
		$container = $this->getContainer();
37
38 13
		$container->registerAlias('PageController', PageController::class);
39 13
	}
40
41 2
	public function register() {
42 2
		$this->registerNavigationEntry();
43 2
		$this->registerNotificationNotifier();
44 2
		$this->registerCommentsEntity();
45 2
	}
46
47 2
	protected function registerNavigationEntry() {
48 2
		$server = $this->getContainer()->getServer();
49
50
		$server->getNavigationManager()->add(function() use ($server) {
51 1
			$urlGenerator = $server->getURLGenerator();
52 1
			$l = $server->getL10NFactory()->get('announcementcenter');
53
			return [
54 1
				'id' => 'announcementcenter',
55 1
				'order' => 10,
56 1
				'href' => $urlGenerator->linkToRoute('announcementcenter.page.index'),
57 1
				'icon' => $urlGenerator->imagePath('announcementcenter', 'announcementcenter.svg'),
58 1
				'name' => $l->t('Announcements'),
59 1
			];
60 2
		});
61 2
	}
62
63 2
	protected function registerCommentsEntity() {
64
		$this->getContainer()->getServer()->getEventDispatcher()->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) {
65
			$event->addEntityCollection('announcement', function($name) {
66
				/** @var Manager $manager */
67
				$manager = $this->getContainer()->query(Manager::class);
68
				try {
69
					$announcement = $manager->getAnnouncement((int) $name);
70
				} catch (\InvalidArgumentException $e) {
71
					return false;
72
				}
73
				return $announcement['comments'] !== false;
74
			});
75 2
		});
76 2
	}
77
78 2
	protected function registerNotificationNotifier() {
79
		$this->getContainer()->getServer()->getNotificationManager()->registerNotifier(function() {
80 1
			return $this->getContainer()->query(Notifier::class);
81 2
		}, function() {
82 1
			$l = $this->getContainer()->getServer()->getL10NFactory()->get('announcementcenter');
83
			return [
84 1
				'id' => 'announcementcenter',
85 1
				'name' => $l->t('Announcements'),
86 1
			];
87 2
		});
88 2
	}
89
}
90