Completed
Pull Request — master (#126)
by Maxence
03:23
created

CollaboratorSearchPlugin::search()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 11
nc 3
nop 4
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2017
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Circles\Collaboration\v1;
29
30
use OCA\Circles\Api\v1\Circles;
31
use OCA\Circles\Model\Circle;
32
use OCA\Circles\Model\Member;
33
use OCA\Circles\Service\MiscService;
34
use OCP\Collaboration\Collaborators\ISearchPlugin;
35
use OCP\Collaboration\Collaborators\ISearchResult;
36
use OCP\Collaboration\Collaborators\SearchResultType;
37
use OCP\Share;
38
39
class CollaboratorSearchPlugin implements ISearchPlugin {
40
41
	/**
42
	 * {@inheritdoc}
43
	 */
44
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
45
		$wide = $exact = [];
46
47
		$circles = Circles::listCircles(Circle::CIRCLES_ALL, $search, Member::LEVEL_MEMBER);
48
		foreach ($circles as $circle) {
49
			$entry = self::addResultEntry($circle);
50
			if (strtolower($circle->getName()) === strtolower($search)) {
51
				$exact[] = $entry;
52
			} else {
53
				$wide[] = $entry;
54
			}
55
		}
56
57
		$type = new SearchResultType('circles');
58
		$searchResult->addResultSet($type, $wide, $exact);
59
	}
60
61
62
	/**
63
	 * @param Circle $circle
64
	 *
65
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|array<string,string>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
66
	 */
67
	private static function addResultEntry(Circle $circle) {
68
69
		return [
70
			'label' => $circle->getName(),
71
			'value' => [
72
				'shareType'   => Share::SHARE_TYPE_CIRCLE,
73
				'shareWith'   => $circle->getUniqueId(),
74
				'circleInfo'  => $circle->getInfo(),
75
				'circleOwner' => MiscService::getDisplay(
76
					$circle->getOwner()
77
						   ->getUserId(), Member::TYPE_USER
78
				)
79
			],
80
		];
81
	}
82
}