Completed
Pull Request — master (#5715)
by Morris
51:53 queued 29:36
created

CapabilitiesManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Capabilities\IPublicCapability;
28
use OCP\ILogger;
29
30
class CapabilitiesManager {
31
32
	/** @var \Closure[] */
33
	private $capabilities = array();
34
35
	/** @var ILogger */
36
	private $logger;
37
38
	public function __construct(ILogger $logger) {
39
		$this->logger = $logger;
40
	}
41
42
	/**
43
	 * Get an array of al the capabilities that are registered at this manager
44
     *
45
	 * @param bool $public get public capabilities only
46
	 * @throws \InvalidArgumentException
47
	 * @return array
48
	 */
49
	public function getCapabilities($public = false) {
50
		$capabilities = [];
51
		foreach($this->capabilities as $capability) {
52
			try {
53
				$c = $capability();
54
			} catch (QueryException $e) {
55
				$this->logger->error('CapabilitiesManager: {message}', ['app' => 'core', 'message' => $e->getMessage()]);
56
				continue;
57
			}
58
59
			if ($c instanceof ICapability) {
60
				if(!$public || $c instanceof IPublicCapability) {
61
					$capabilities = array_replace_recursive($capabilities, $c->getCapabilities());
62
				}
63
			} else {
64
				throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface');
65
			}
66
		}
67
68
		return $capabilities;
69
	}
70
71
	/**
72
	 * In order to improve lazy loading a closure can be registered which will be called in case
73
	 * capabilities are actually requested
74
	 *
75
	 * $callable has to return an instance of OCP\Capabilities\ICapability
76
	 *
77
	 * @param \Closure $callable
78
	 */
79
	public function registerCapability(\Closure $callable) {
80
		array_push($this->capabilities, $callable);
81
	}
82
}
83