Failed Conditions
Push — master ( e8410d...a45585 )
by Marcos
09:36 queued 11s
created

Provider::search()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 2
nop 2
dl 0
loc 36
rs 8.0995
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
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\Passman\Search;
25
26
use OCA\Passman\AppInfo\Application;
27
use OCA\Passman\Db\CredentialMapper;
28
use OCA\Passman\Db\VaultMapper;
29
use OCA\Passman\Service\SettingsService;
30
use OCA\Passman\Service\VaultService;
31
use OCA\Passman\Utility\Utils;
32
use OCP\AppFramework\Db\DoesNotExistException;
33
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
34
use OCP\IDBConnection;
35
use OCP\IL10N;
36
use OCP\IURLGenerator;
37
use OCP\IUser;
38
use OCP\Search\IProvider;
39
use OCP\Search\ISearchQuery;
40
use OCP\Search\SearchResult;
41
use OCP\Search\SearchResultEntry;
42
43
class Provider implements IProvider {
44
45
	private IL10N $l10n;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
46
	private IURLGenerator $urlGenerator;
47
	private IDBConnection $db;
48
	private SettingsService $settings;
49
50
	public function __construct(IL10N $l10n, IURLGenerator $urlGenerator, IDBConnection $db, SettingsService $settings) {
51
		$this->l10n = $l10n;
52
		$this->urlGenerator = $urlGenerator;
53
		$this->db = $db;
54
		$this->settings = $settings;
55
	}
56
57
	public function getId(): string {
58
		return Application::APP_ID;
59
	}
60
61
	public function getName(): string {
62
		return $this->l10n->t('Passman');
63
	}
64
65
	public function getOrder(string $route, array $routeParameters): int {
66
		if (strpos($route, Application::APP_ID . '.') === 0) {
67
			// Active app, prefer my results
68
			return -1;
69
		}
70
71
		return 25;
72
	}
73
74
	public function search(IUser $user, ISearchQuery $query): SearchResult {
75
		$searchResultEntries = [];
76
77
		if ($this->settings->getAppSetting('enable_global_search', 0) === 1) {
78
			$VaultService = new VaultService(new VaultMapper($this->db, new Utils()));
79
			$Vaults = $VaultService->getByUser($user->getUID());
80
			$CredentialMapper = new CredentialMapper($this->db, new Utils());
81
82
			foreach ($Vaults as $Vault) {
83
				try {
84
					$Credentials = $CredentialMapper->getCredentialsByVaultId($Vault->getId(), $Vault->getUserId());
85
86
					foreach ($Credentials as $Credential) {
87
						if (strpos($Credential->getLabel(), $query->getTerm()) !== false) {
88
							try {
89
								$searchResultEntries[] = new SearchResultEntry(
90
									$this->urlGenerator->imagePath(Application::APP_ID, 'app.svg'),
91
									$Credential->getLabel(),
92
									\sprintf("Part of Passman vault %s", $Vault->getName()),
93
									$this->urlGenerator->linkToRoute('passman.page.index') . "#/vault/" . $Vault->getGuid() . "?show=" . $Credential->getGuid()
94
								);
95
							} catch (\Exception $e) {
96
							}
97
						}
98
					}
99
				} catch (DoesNotExistException $e) {
100
				} catch (MultipleObjectsReturnedException $e) {
101
				}
102
			}
103
		}
104
105
		return SearchResult::complete(
106
			$this->l10n->t(Application::APP_ID),
107
			$searchResultEntries
108
		);
109
	}
110
}
111