Completed
Pull Request — master (#6982)
by Blizzz
14:42
created

Manager::runSorters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 3
dl 0
loc 12
rs 9.4285
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
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Collaboration\AutoComplete;
25
26
use OCP\Collaboration\AutoComplete\IManager;
27
use OCP\Collaboration\AutoComplete\ISorter;
28
use OCP\IServerContainer;
29
30
class Manager implements IManager {
31
	/** @var string[] */
32
	protected $sorters =[];
33
34
	/** @var ISorter[]  */
35
	protected $sorterInstances = [];
36
	/** @var IServerContainer */
37
	private $c;
38
39
	public function __construct(IServerContainer $container) {
40
		$this->c = $container;
41
	}
42
43
	public function runSorters(array $sorters, array &$sortArray, array $context) {
44
		$sorterInstances = $this->getSorters();
45
		while($sorter = array_shift($sorters)) {
46
			if(isset($sorterInstances[$sorter])) {
47
				$sorterInstances[$sorter]->sort($sortArray, $context);
48
			} else {
49
				$this->c->getLogger()->warning('No sorter for ID "{id}", skipping', [
50
					'app' => 'core', 'id' => $sorter
51
				]);
52
			}
53
		}
54
	}
55
56
	public function registerSorter($className) {
57
		$this->sorters[] = $className;
58
	}
59
60
	protected function getSorters() {
61
		if(count($this->sorterInstances) === 0) {
62
			foreach ($this->sorters as $sorter) {
63
				/** @var ISorter $instance */
64
				$instance = $this->c->resolve($sorter);
65
				if(!$instance instanceof ISorter) {
66
					$this->c->getLogger()->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}',
67
						['app' => 'core', 'class' => $sorter]);
68
					continue;
69
				}
70
				$sorterId = trim($instance->getId());
71
				if(trim($sorterId) === '') {
72
					$this->c->getLogger()->notice('Skipping sorter with empty ID. Class name: {class}',
73
						['app' => 'core', 'class' => $sorter]);
74
					continue;
75
				}
76
				$this->sorterInstances[$sorterId] = $instance;
77
			}
78
		}
79
		return $this->sorterInstances;
80
	}
81
}
82