Completed
Push — master ( 912623...0260a2 )
by Maxence
13s queued 11s
created

CollaboratorSearchPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 OC\User\NoUserException;
31
use OCA\Circles\Api\v1\Circles;
32
use OCA\Circles\AppInfo\Application;
33
use OCA\Circles\Model\Circle;
34
use OCA\Circles\Model\Member;
35
use OCA\Circles\Service\MiscService;
36
use OCP\AppFramework\QueryException;
37
use OCP\Collaboration\Collaborators\ISearchPlugin;
38
use OCP\Collaboration\Collaborators\ISearchResult;
39
use OCP\Collaboration\Collaborators\SearchResultType;
40
use OCP\IL10N;
41
use OCP\Share;
42
43
class CollaboratorSearchPlugin implements ISearchPlugin {
44
45
46
	/** @var IL10N */
47
	private $l10n;
48
49
	/** @var MiscService */
50
	private $miscService;
51
52
53
	/**
54
	 * CollaboratorSearchPlugin constructor.
55
	 *
56
	 * @throws QueryException
57
	 */
58
	public function __construct() {
59
		$this->l10n = \OC::$server->getL10N(Application::APP_NAME);
60
		$this->miscService = \OC::$server->query(MiscService::class);
61
	}
62
63
64
	/**
65
	 * {@inheritdoc}
66
	 */
67
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
68
		$wide = $exact = [];
69
70
		$circles = Circles::listCircles(Circle::CIRCLES_ALL, $search, Member::LEVEL_MEMBER);
71
		foreach ($circles as $circle) {
72
			try {
73
				$entry = $this->addResultEntry($circle);
74
			} catch (NoUserException $e) {
0 ignored issues
show
Bug introduced by
The class OC\User\NoUserException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
75
				continue;
76
			}
77
78
			if (strtolower($circle->getName()) === strtolower($search)) {
79
				$exact[] = $entry;
80
			} else {
81
				$wide[] = $entry;
82
			}
83
		}
84
85
		$type = new SearchResultType('circles');
86
		$searchResult->addResultSet($type, $wide, $exact);
87
	}
88
89
90
	/**
91
	 * @param Circle $circle
92
	 *
93
	 * @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...
94
	 * @throws NoUserException
95
	 */
96
	private function addResultEntry(Circle $circle) {
97
98
		return [
99
			'label' => sprintf(
100
				'%s (%s, %s)',
101
				$circle->getName(),
102
				$this->l10n->t($circle->getTypeLongString()),
103
				$this->miscService->getDisplayName(
104
					$circle->getOwner()
105
						   ->getUserId(), true
106
				)
107
			),
108
			'value' => [
109
				'shareType'   => Share::SHARE_TYPE_CIRCLE,
110
				'shareWith'   => $circle->getUniqueId(),
111
				'circleInfo'  => $circle->getInfo(),
112
				'circleOwner' => MiscService::getDisplay(
113
					$circle->getOwner()
114
						   ->getUserId(), Member::TYPE_USER
115
				)
116
			],
117
		];
118
	}
119
}
120