Application::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * Bookmarks_FullTextSearch - Indexing bookmarks
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\Bookmarks_FullTextSearch\AppInfo;
32
33
34
use Exception;
35
use OCA\Bookmarks_FullTextSearch\Provider\BookmarksProvider;
36
use OCP\App\IAppManager;
37
use OCP\AppFramework\App;
38
use OCP\AppFramework\QueryException;
39
use OCP\FullTextSearch\IFullTextSearchManager;
40
use OCP\FullTextSearch\Model\IIndex;
41
use OCP\IUserSession;
42
use OCP\Util;
43
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
44
use Symfony\Component\EventDispatcher\GenericEvent;
45
46
47
class Application extends App {
48
49
50
	const APP_NAME = 'bookmarks_fulltextsearch';
51
52
53
	/** @var IFullTextSearchManager */
54
	private $fullTextSearchManager;
55
56
	/** @var EventDispatcherInterface */
57
	private $eventDispatcher;
58
59
60
	/**
61
	 * @param array $params
62
	 */
63
	public function __construct(array $params = []) {
64
		parent::__construct(self::APP_NAME, $params);
65
66
		$c = $this->getContainer();
67
68
		try {
69
			$this->fullTextSearchManager = $c->query(IFullTextSearchManager::class);
70
		} catch (QueryException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
71
		}
72
	}
73
74
75
	/**
76
	 * Register Hooks
77
	 */
78
	public function registerHooks() {
79
		$this->eventDispatcher = \OC::$server->getEventDispatcher();
80
81
		$this->registerHookCreate();
82
		$this->registerHookUpdate();
83
		$this->registerHookDelete();
84
	}
85
86
87
	/**
88
	 *
89
	 */
90
	private function registerHookCreate() {
91
		$this->eventDispatcher->addListener(
92
			'\OCA\Bookmarks::onBookmarkCreate', function(GenericEvent $e) {
93
94
			$this->fullTextSearchManager->createIndex(
95
				BookmarksProvider::BOOKMARKS_PROVIDER_ID,
96
				(string)$e->getArgument('id'),
97
				$e->getArgument('userId')
98
			);
99
			$this->fullTextSearchManager->updateIndexStatus(
100
				BookmarksProvider::BOOKMARKS_PROVIDER_ID,
101
				(string)$e->getArgument('id'),
102
				IIndex::INDEX_FULL
103
			);
104
		}
105
		);
106
	}
107
108
109
	/**
110
	 *
111
	 */
112 View Code Duplication
	private function registerHookUpdate() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
		$this->eventDispatcher->addListener(
114
			'\OCA\Bookmarks::onBookmarkUpdate', function(GenericEvent $e) {
115
116
			$this->fullTextSearchManager->updateIndexStatus(
117
				BookmarksProvider::BOOKMARKS_PROVIDER_ID, (string)$e->getArgument('id'),
118
				IIndex::INDEX_FULL
119
			);
120
		}
121
		);
122
	}
123
124
125
	/**
126
	 *
127
	 */
128 View Code Duplication
	private function registerHookDelete() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
		$this->eventDispatcher->addListener(
130
			'\OCA\Bookmarks::onBookmarkDelete', function(GenericEvent $e) {
131
132
			$this->fullTextSearchManager->updateIndexStatus(
133
				BookmarksProvider::BOOKMARKS_PROVIDER_ID, (string)$e->getArgument('id'),
134
				IIndex::INDEX_REMOVE
135
			);
136
		}
137
		);
138
	}
139
140
141
	/**
142
	 *
143
	 * @throws QueryException
144
	 */
145
	public function registerBookmarksSearch() {
146
		$container = $this->getContainer();
147
148
		/** @var IUserSession $userSession */
149
		$userSession = $container->query(IUserSession::class);
150
151
		if (!$userSession->isLoggedIn()) {
152
			return;
153
		}
154
155
		$user = $userSession->getUser();
156
157
		try {
158
			$appManager = $container->query(IAppManager::class);
159
			if ($appManager->isEnabledForUser('fulltextsearch', $user)) {
160
				Util::addStyle(self::APP_NAME, 'fulltextsearch');
161
				$this->includeFullTextSearch();
162
			}
163
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
164
		}
165
	}
166
167
168
	/**
169
	 *
170
	 */
171
	private function includeFullTextSearch() {
172
		$this->eventDispatcher->addListener(
173
			'\OCA\Bookmarks::loadAdditionalScripts', function() {
174
			if ($this->fullTextSearchManager->isProviderIndexed(
175
				BookmarksProvider::BOOKMARKS_PROVIDER_ID
176
			)) {
177
				$this->fullTextSearchManager->addJavascriptAPI();
178
				Util::addScript(Application::APP_NAME, 'bookmarks');
179
			}
180
		}
181
		);
182
	}
183
184
}
185
186