Completed
Push — master ( 255c7d...6db691 )
by Jan-Christoph
11:48 queued 11:24
created

Manager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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 OCP\App\IAppManager;
28
use OCP\Contacts\ContactsMenu\IEntry;
29
use OCP\IUser;
30
31
class Manager {
32
33
	/** @var ContactsStore */
34
	private $store;
35
36
	/** @var ActionProviderStore */
37
	private $actionProviderStore;
38
39
	/** @var IAppManager */
40
	private $appManager;
41
42
	/**
43
	 * @param ContactsStore $store
44
	 * @param ActionProviderStore $actionProviderStore
45
	 * @param IAppManager $appManager
46
	 */
47
	public function __construct(ContactsStore $store, ActionProviderStore $actionProviderStore, IAppManager $appManager) {
48
		$this->store = $store;
49
		$this->actionProviderStore = $actionProviderStore;
50
		$this->appManager = $appManager;
51
	}
52
53
	/**
54
	 * @param string $user
55
	 * @param string $filter
56
	 * @return array
57
	 */
58
	public function getEntries(IUser $user, $filter) {
59
		$entries = $this->store->getContacts($user, $filter);
60
61
		$sortedEntries = $this->sortEntries($entries);
62
		$topEntries = array_slice($sortedEntries, 0, 25);
63
		$this->processEntries($topEntries, $user);
64
65
		$contactsEnabled = $this->appManager->isEnabledForUser('contacts', $user);
66
		return [
67
			'contacts' => $topEntries,
68
			'contactsAppEnabled' => $contactsEnabled,
69
		];
70
	}
71
72
	/**
73
	 * @param IEntry[] $entries
74
	 * @return IEntry[]
75
	 */
76
	private function sortEntries(array $entries) {
77
		usort($entries, function(IEntry $entryA, IEntry $entryB) {
78
			return strcasecmp($entryA->getFullName(), $entryB->getFullName());
79
		});
80
		return $entries;
81
	}
82
83
	/**
84
	 * @param IEntry[] $entries
85
	 * @param IUser $user
86
	 */
87
	private function processEntries(array $entries, IUser $user) {
88
		$providers = $this->actionProviderStore->getProviders($user);
89
		foreach ($entries as $entry) {
90
			foreach ($providers as $provider) {
91
				$provider->process($entry);
92
			}
93
		}
94
	}
95
96
}
97