Passed
Push — master ( 9d1e2c...943229 )
by Morris
16:38 queued 56s
created

SearchResult::removeCollaboratorResult()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 2
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OC\Collaboration\Collaborators;
27
28
use OCP\Collaboration\Collaborators\ISearchResult;
29
use OCP\Collaboration\Collaborators\SearchResultType;
30
31
class SearchResult implements ISearchResult {
32
	protected $result = [
33
		'exact' => [],
34
	];
35
36
	protected $exactIdMatches = [];
37
38
	public function addResultSet(SearchResultType $type, array $matches, array $exactMatches = null) {
39
		$type = $type->getLabel();
40
		if (!isset($this->result[$type])) {
41
			$this->result[$type] = [];
42
			$this->result['exact'][$type] = [];
43
		}
44
45
		$this->result[$type] = array_merge($this->result[$type], $matches);
46
		if (is_array($exactMatches)) {
47
			$this->result['exact'][$type] = array_merge($this->result['exact'][$type], $exactMatches);
48
		}
49
	}
50
51
	public function markExactIdMatch(SearchResultType $type) {
52
		$this->exactIdMatches[$type->getLabel()] = 1;
53
	}
54
55
	public function hasExactIdMatch(SearchResultType $type) {
56
		return isset($this->exactIdMatches[$type->getLabel()]);
57
	}
58
59
	public function hasResult(SearchResultType $type, $collaboratorId) {
60
		$type = $type->getLabel();
61
		if (!isset($this->result[$type])) {
62
			return false;
63
		}
64
65
		$resultArrays = [$this->result['exact'][$type], $this->result[$type]];
66
		foreach ($resultArrays as $resultArray) {
67
			foreach ($resultArray as $result) {
68
				if ($result['value']['shareWith'] === $collaboratorId) {
69
					return true;
70
				}
71
			}
72
		}
73
74
		return false;
75
	}
76
77
	public function asArray() {
78
		return $this->result;
79
	}
80
81
	public function unsetResult(SearchResultType $type) {
82
		$type = $type->getLabel();
83
		$this->result[$type] = [];
84
		if (isset($this->result['exact'][$type])) {
85
			$this->result['exact'][$type] = [];
86
		}
87
	}
88
89
	public function removeCollaboratorResult(SearchResultType $type, string $collaboratorId): bool {
90
		$type = $type->getLabel();
91
		if (!isset($this->result[$type])) {
92
			return false;
93
		}
94
95
		$actionDone = false;
96
		$resultArrays = [&$this->result['exact'][$type], &$this->result[$type]];
97
		foreach ($resultArrays as &$resultArray) {
98
			foreach ($resultArray as $k => $result) {
99
				if ($result['value']['shareWith'] === $collaboratorId) {
100
					unset($resultArray[$k]);
101
					$actionDone = true;
102
				}
103
			}
104
		}
105
106
		return $actionDone;
107
	}
108
}
109