Completed
Pull Request — master (#3233)
by Jan-Christoph
12:25
created

ActionProviderStore   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProviders() 0 20 3
A getServerProviderClasses() 0 5 1
A getAppProviderClasses() 0 16 3
1
<?php
2
3
/**
4
 * @copyright 2017 Christoph Wurst <[email protected]>
5
 *
6
 * @author 2017 Christoph Wurst <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\Contacts\ContactsMenu;
26
27
use Exception;
28
use OC\App\AppManager;
29
use OC\Contacts\ContactsMenu\Providers\EMailProvider;
30
use OCP\AppFramework\QueryException;
31
use OCP\Contacts\ContactsMenu\IProvider;
32
use OCP\ILogger;
33
use OCP\IServerContainer;
34
use OCP\IUser;
35
36
class ActionProviderStore {
37
38
	/** @var IServerContainer */
39
	private $serverContainer;
40
41
	/** @var AppManager */
42
	private $appManager;
43
44
	/** @var ILogger */
45
	private $logger;
46
47
	/**
48
	 * @param IServerContainer $serverContainer
49
	 * @param AppManager $appManager
50
	 * @param ILogger $logger
51
	 */
52
	public function __construct(IServerContainer $serverContainer, AppManager $appManager, ILogger $logger) {
53
		$this->serverContainer = $serverContainer;
54
		$this->appManager = $appManager;
55
		$this->logger = $logger;
56
	}
57
58
	/**
59
	 * @param IUser $user
60
	 * @return IProvider[]
61
	 * @throws Exception
62
	 */
63
	public function getProviders(IUser $user) {
64
		$appClasses = $this->getAppProviderClasses($user);
65
		$providerClasses = $this->getServerProviderClasses();
66
		$allClasses = array_merge($providerClasses, $appClasses);
67
		$providers = [];
68
69
		foreach ($allClasses as $class) {
70
			try {
71
				$providers[] = $this->serverContainer->query($class);
72
			} catch (QueryException $ex) {
73
				$this->logger->logException($ex, [
74
					'message' => "Could not load contacts menu action provider $class",
75
					'app' => 'core',
76
				]);
77
				throw new Exception("Could not load contacts menu action provider");
78
			}
79
		}
80
81
		return $providers;
82
	}
83
84
	/**
85
	 * @return string[]
86
	 */
87
	private function getServerProviderClasses() {
88
		return [
89
			EMailProvider::class,
90
		];
91
	}
92
93
	/**
94
	 * @param IUser $user
95
	 * @return string[]
96
	 */
97
	private function getAppProviderClasses(IUser $user) {
98
		return array_reduce($this->appManager->getEnabledAppsForUser($user), function($all, $appId) {
99
			$info = $this->appManager->getAppInfo($appId);
100
101
			if (!isset($info['contactsmenu']) || !isset($info['contactsmenu'])) {
102
				// Nothing to add
103
				return $all;
104
			}
105
106
			$providers = array_reduce($info['contactsmenu'], function($all, $provider) {
107
				return array_merge($all, [$provider]);
108
			}, []);
109
110
			return array_merge($all, $providers);
111
		}, []);
112
	}
113
114
}
115