PersonSearchProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 98
ccs 0
cts 43
cp 0
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 2 1
A getName() 0 2 1
A __construct() 0 15 1
A search() 0 23 1
A getOrder() 0 10 4
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
		if ($route === 'memories.Page.main') {
112
			return 0;
113
		}
114
115
		return 10;
116
	}
117
118
	/**
119
	 * @inheritDoc
120
	 */
121
	public function search(IUser $user, ISearchQuery $query) : SearchResult {
122
		$page = $query->getCursor() ?? 0;
123
		$limit = $query->getLimit();
124
		return SearchResult::paginated(
125
			$this->l10n->t('Face Recognition'),
126
			array_map(function (Person $result) {
127
				$personName = $result->getName();
128
				return new SearchResultEntry(
129
					$this->urlService->getPersonThumbUrl($personName, 32),
130
					$personName,
131
					$this->l10n->t('Images of %s', $personName),
132
					$this->urlService->getRedirectToPersonUrl($personName),
133
					'icon-contacts-dark',
134
					true
135
				);
136
			},
137
			$this->personMapper->findPersonsLike($user->getUID(),
138
				$this->modelId,
139
				$query->getTerm(),
140
				$page * $limit,
141
				$limit)
142
			),
143
			$page);
144
	}
145
146
}
147