Passed
Push — nc21sidebar ( a11006...67b2b6 )
by Matias
05:56
created

PersonSearchProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 94
ccs 0
cts 51
cp 0
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 2 1
A search() 0 23 1
A getOrder() 0 6 3
A getName() 0 2 1
A __construct() 0 15 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2020 Li Xiangbin  <[email protected]>
5
 *
6
 * @author Li Xiangbin <[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\FaceRecognition\Search;
26
27
use OCP\Search\IProvider;
28
use OCP\IL10N;
29
use OCP\IURLGenerator;
30
use OCP\IUser;
31
use OCP\Search\ISearchQuery;
32
use OCP\Search\SearchResult;
33
use OCP\Search\SearchResultEntry;
34
use OCP\Files\IRootFolder;
35
36
use OCA\FaceRecognition\Db\Person;
37
use OCA\FaceRecognition\Db\PersonMapper;
38
use OCA\FaceRecognition\Db\ImageMapper;
39
use OCA\FaceRecognition\Service\SettingsService;
40
use OCA\FaceRecognition\Model\IModel;
41
use OCA\FaceRecognition\Service\UrlService;
42
43
/**
44
 * Provide search results from the 'facerecognition' app
45
 */
46
class PersonSearchProvider implements IProvider {
47
48
	/** @var PersonMapper personMapper */
49
	private $personMapper;
50
51
	/** @var ImageMapper imageMapper */
52
	private $imageMapper;
53
54
	/** @var SettingsService Settings service */
55
	private $settingsService;
56
57
	/** @var IL10N */
58
	private $l10n;
59
60
	/** @var IURLGenerator */
61
	private $urlGenerator;
62
63
	/** @var UrlService */
64
	private $urlService;
65
66
	/** @var IRootFolder */
67
	private $rootFolder;
68
69
	/** @var int*/
70
	private $modelId;
71
72
	public function __construct(PersonMapper      $personMapper,
73
	                            ImageMapper       $imageMapper,
74
	                            SettingsService   $settingsService,
75
	                            IL10N             $l10n,
76
	                            UrlService        $urlService,
77
	                            IURLGenerator     $urlGenerator,
78
	                            IRootFolder       $rootFolder) {
79
		$this->personMapper     = $personMapper;
80
		$this->imageMapper      = $imageMapper;
81
		$this->settingsService  = $settingsService;
82
		$this->l10n             = $l10n;
83
		$this->urlService       = $urlService;
84
		$this->urlGenerator     = $urlGenerator;
85
		$this->rootFolder       = $rootFolder;
86
		$this->modelId          = $this->settingsService->getCurrentFaceModel();
87
	}
88
89
	/**
90
	 * @inheritDoc
91
	 */
92
	public function getId(): string {
93
		return 'facerecognition';
94
	}
95
96
	/**
97
	 * @inheritDoc
98
	 */
99
	public function getName(): string {
100
		return $this->l10n->t('Face Recognition');
101
	}
102
103
	/**
104
	 * @inheritDoc
105
	 */
106
	public function getOrder(string $route, array $routeParameters): int {
107
		if ($route === 'settings.PersonalSettings.index' &&
108
		    $routeParameters["section"] === 'facerecognition') {
109
			return 0;
110
		}
111
		return 10;
112
	}
113
114
	/**
115
	 * @inheritDoc
116
	 */
117
	public function search(IUser $user, ISearchQuery $query) : SearchResult {
118
		$page = $query->getCursor() ?? 0;
119
		$limit = $query->getLimit();
120
		return SearchResult::paginated(
121
			$this->l10n->t('Face Recognition'),
122
			array_map(function (Person $result) {
123
				$personName = $result->getName();
124
				return new SearchResultEntry(
125
					'',
126
					$personName,
127
					'',
128
					$this->urlService->getRedirectToPersonUrl($personName),
129
					'icon-contacts-dark',
130
					true,
131
				);
132
			},
133
			$this->personMapper->findPersonsLike($user->getUID(),
134
				$this->modelId,
135
				$query->getTerm(),
136
				$page * $limit,
137
				$limit)
138
			),
139
			$page);
140
	}
141
142
}
143