Completed
Push — master ( dec4ee...6dc956 )
by Björn
37:31 queued 27:15
created

CapabilitiesManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
namespace OC;
23
24
25
use OCP\AppFramework\QueryException;
26
use OCP\Capabilities\ICapability;
27
use OCP\ILogger;
28
29
class CapabilitiesManager {
30
31
	/** @var \Closure[] */
32
	private $capabilities = array();
33
34
	/** @var ILogger */
35
	private $logger;
36
37
	public function __construct(ILogger $logger) {
38
		$this->logger = $logger;
39
	}
40
41
	/**
42
	 * Get an array of al the capabilities that are registered at this manager
43
     *
44
	 * @throws \InvalidArgumentException
45
	 * @return array
46
	 */
47
	public function getCapabilities() {
48
		$capabilities = [];
49
		foreach($this->capabilities as $capability) {
50
			try {
51
				$c = $capability();
52
			} catch (QueryException $e) {
53
				$this->logger->error('CapabilitiesManager: {message}', ['app' => 'core', 'message' => $e->getMessage()]);
54
				continue;
55
			}
56
57
			if ($c instanceof ICapability) {
58
				$capabilities = array_replace_recursive($capabilities, $c->getCapabilities());
59
			} else {
60
				throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface');
61
			}
62
		}
63
64
		return $capabilities;
65
	}
66
67
	/**
68
	 * In order to improve lazy loading a closure can be registered which will be called in case
69
	 * capabilities are actually requested
70
	 *
71
	 * $callable has to return an instance of OCP\Capabilities\ICapability
72
	 *
73
	 * @param \Closure $callable
74
	 */
75
	public function registerCapability(\Closure $callable) {
76
		array_push($this->capabilities, $callable);
77
	}
78
}
79