Completed
Push — master ( edd944...d89c76 )
by Lukas
146:32 queued 124:11
created

Manager::findOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 8
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 IUser $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 IUser $user
74
	 * @param integer $shareType
75
	 * @param string $shareWith
76
	 * @return IEntry
77
	 */
78
	public function findOne(IUser $user, $shareType, $shareWith) {
79
		$entry = $this->store->findOne($user, $shareType, $shareWith);
80
		if ($entry) {
81
			$this->processEntries([$entry], $user);
82
		}
83
84
		return $entry;
85
	}
86
87
	/**
88
	 * @param IEntry[] $entries
89
	 * @return IEntry[]
90
	 */
91
	private function sortEntries(array $entries) {
92
		usort($entries, function(IEntry $entryA, IEntry $entryB) {
93
			return strcasecmp($entryA->getFullName(), $entryB->getFullName());
94
		});
95
		return $entries;
96
	}
97
98
	/**
99
	 * @param IEntry[] $entries
100
	 * @param IUser $user
101
	 */
102
	private function processEntries(array $entries, IUser $user) {
103
		$providers = $this->actionProviderStore->getProviders($user);
104
		foreach ($entries as $entry) {
105
			foreach ($providers as $provider) {
106
				$provider->process($entry);
107
			}
108
		}
109
	}
110
111
}
112