Completed
Pull Request — master (#4374)
by Georg
25:00
created
core/Controller/ContactsMenuController.php 1 patch
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -33,47 +33,47 @@
 block discarded – undo
33 33
 
34 34
 class ContactsMenuController extends Controller {
35 35
 
36
-	/** @var Manager */
37
-	private $manager;
36
+    /** @var Manager */
37
+    private $manager;
38 38
 
39
-	/** @var IUserSession */
40
-	private $userSession;
39
+    /** @var IUserSession */
40
+    private $userSession;
41 41
 
42
-	/**
43
-	 * @param IRequest $request
44
-	 * @param IUserSession $userSession
45
-	 * @param Manager $manager
46
-	 */
47
-	public function __construct(IRequest $request, IUserSession $userSession, Manager $manager) {
48
-		parent::__construct('core', $request);
49
-		$this->userSession = $userSession;
50
-		$this->manager = $manager;
51
-	}
42
+    /**
43
+     * @param IRequest $request
44
+     * @param IUserSession $userSession
45
+     * @param Manager $manager
46
+     */
47
+    public function __construct(IRequest $request, IUserSession $userSession, Manager $manager) {
48
+        parent::__construct('core', $request);
49
+        $this->userSession = $userSession;
50
+        $this->manager = $manager;
51
+    }
52 52
 
53
-	/**
54
-	 * @NoAdminRequired
55
-	 *
56
-	 * @param string|null filter
57
-	 * @return JSONResponse
58
-	 */
59
-	public function index($filter = null) {
60
-		return $this->manager->getEntries($this->userSession->getUser(), $filter);
61
-	}
53
+    /**
54
+     * @NoAdminRequired
55
+     *
56
+     * @param string|null filter
57
+     * @return JSONResponse
58
+     */
59
+    public function index($filter = null) {
60
+        return $this->manager->getEntries($this->userSession->getUser(), $filter);
61
+    }
62 62
 
63
-	/**
64
-	 * @NoAdminRequired
65
-	 *
66
-	 * @param integer $shareType
67
-	 * @param string $shareWith
68
-	 * @return JSONResponse
69
-	 */
70
-	public function findOne($shareType, $shareWith) {
71
-		$contact = $this->manager->findOne($this->userSession->getUser(), $shareType, $shareWith);
63
+    /**
64
+     * @NoAdminRequired
65
+     *
66
+     * @param integer $shareType
67
+     * @param string $shareWith
68
+     * @return JSONResponse
69
+     */
70
+    public function findOne($shareType, $shareWith) {
71
+        $contact = $this->manager->findOne($this->userSession->getUser(), $shareType, $shareWith);
72 72
 
73
-		if ($contact) {
74
-			return $contact;
75
-		} else {
76
-			return new JSONResponse([], Http::STATUS_NOT_FOUND);
77
-		}
78
-	}
73
+        if ($contact) {
74
+            return $contact;
75
+        } else {
76
+            return new JSONResponse([], Http::STATUS_NOT_FOUND);
77
+        }
78
+    }
79 79
 }
Please login to merge, or discard this patch.
core/routes.php 1 patch
Indentation   +69 added lines, -69 removed lines patch added patch discarded remove patch
@@ -35,40 +35,40 @@  discard block
 block discarded – undo
35 35
 
36 36
 $application = new Application();
37 37
 $application->registerRoutes($this, [
38
-	'routes' => [
39
-		['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'],
40
-		['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'],
41
-		['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'],
42
-		['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'],
43
-		['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'],
44
-		['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'],
45
-		['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'],
46
-		['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'],
47
-		['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'],
48
-		['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'],
49
-		['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'],
50
-		['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'],
51
-		['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'],
52
-		['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'],
53
-		['name' => 'ClientFlowLogin#redirectPage', 'url' => '/login/flow/redirect', 'verb' => 'GET'],
54
-		['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'],
55
-		['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'],
56
-		['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'],
57
-		['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'],
58
-		['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'],
59
-		['name' => 'Preview#getPreview', 'url' => '/core/preview', 'verb' => 'GET'],
60
-		['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'],
61
-		['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'],
62
-		['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'],
63
-		['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'],
64
-		['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'],
65
-	],
66
-	'ocs' => [
67
-		['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
68
-		['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'],
69
-		['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'],
70
-		['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'],
71
-	],
38
+    'routes' => [
39
+        ['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'],
40
+        ['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'],
41
+        ['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'],
42
+        ['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'],
43
+        ['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'],
44
+        ['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'],
45
+        ['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'],
46
+        ['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'],
47
+        ['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'],
48
+        ['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'],
49
+        ['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'],
50
+        ['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'],
51
+        ['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'],
52
+        ['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'],
53
+        ['name' => 'ClientFlowLogin#redirectPage', 'url' => '/login/flow/redirect', 'verb' => 'GET'],
54
+        ['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'],
55
+        ['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'],
56
+        ['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'],
57
+        ['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'],
58
+        ['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'],
59
+        ['name' => 'Preview#getPreview', 'url' => '/core/preview', 'verb' => 'GET'],
60
+        ['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'],
61
+        ['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'],
62
+        ['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'],
63
+        ['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'],
64
+        ['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'],
65
+    ],
66
+    'ocs' => [
67
+        ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
68
+        ['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'],
69
+        ['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'],
70
+        ['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'],
71
+    ],
72 72
 ]);
73 73
 
74 74
 // Post installation check
@@ -77,62 +77,62 @@  discard block
 block discarded – undo
77 77
 // Core ajax actions
78 78
 // Search
79 79
 $this->create('search_ajax_search', '/core/search')
80
-	->actionInclude('core/search/ajax/search.php');
80
+    ->actionInclude('core/search/ajax/search.php');
81 81
 // Routing
82 82
 $this->create('core_ajax_update', '/core/ajax/update.php')
83
-	->actionInclude('core/ajax/update.php');
83
+    ->actionInclude('core/ajax/update.php');
84 84
 
85 85
 // File routes
86 86
 $this->create('files.viewcontroller.showFile', '/f/{fileid}')->action(function($urlParams) {
87
-	$app = new \OCA\Files\AppInfo\Application($urlParams);
88
-	$app->dispatch('ViewController', 'index');
87
+    $app = new \OCA\Files\AppInfo\Application($urlParams);
88
+    $app->dispatch('ViewController', 'index');
89 89
 });
90 90
 
91 91
 // Call routes
92 92
 $this->create('spreed.pagecontroller.showCall', '/call/{token}')->action(function($urlParams) {
93
-	if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) {
94
-		$app = new \OCA\Spreed\AppInfo\Application($urlParams);
95
-		$app->dispatch('PageController', 'index');
96
-	} else {
97
-		throw new \OC\HintException('App spreed is not enabled');
98
-	}
93
+    if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) {
94
+        $app = new \OCA\Spreed\AppInfo\Application($urlParams);
95
+        $app->dispatch('PageController', 'index');
96
+    } else {
97
+        throw new \OC\HintException('App spreed is not enabled');
98
+    }
99 99
 });
100 100
 
101 101
 // Sharing routes
102 102
 $this->create('files_sharing.sharecontroller.showShare', '/s/{token}')->action(function($urlParams) {
103
-	if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
104
-		$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
105
-		$app->dispatch('ShareController', 'showShare');
106
-	} else {
107
-		throw new \OC\HintException('App file sharing is not enabled');
108
-	}
103
+    if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
104
+        $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
105
+        $app->dispatch('ShareController', 'showShare');
106
+    } else {
107
+        throw new \OC\HintException('App file sharing is not enabled');
108
+    }
109 109
 });
110 110
 $this->create('files_sharing.sharecontroller.authenticate', '/s/{token}/authenticate')->post()->action(function($urlParams) {
111
-	if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
112
-		$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
113
-		$app->dispatch('ShareController', 'authenticate');
114
-	} else {
115
-		throw new \OC\HintException('App file sharing is not enabled');
116
-	}
111
+    if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
112
+        $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
113
+        $app->dispatch('ShareController', 'authenticate');
114
+    } else {
115
+        throw new \OC\HintException('App file sharing is not enabled');
116
+    }
117 117
 });
118 118
 $this->create('files_sharing.sharecontroller.showAuthenticate', '/s/{token}/authenticate')->get()->action(function($urlParams) {
119
-	if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
120
-		$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
121
-		$app->dispatch('ShareController', 'showAuthenticate');
122
-	} else {
123
-		throw new \OC\HintException('App file sharing is not enabled');
124
-	}
119
+    if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
120
+        $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
121
+        $app->dispatch('ShareController', 'showAuthenticate');
122
+    } else {
123
+        throw new \OC\HintException('App file sharing is not enabled');
124
+    }
125 125
 });
126 126
 $this->create('files_sharing.sharecontroller.downloadShare', '/s/{token}/download')->get()->action(function($urlParams) {
127
-	if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
128
-		$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
129
-		$app->dispatch('ShareController', 'downloadShare');
130
-	} else {
131
-		throw new \OC\HintException('App file sharing is not enabled');
132
-	}
127
+    if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
128
+        $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
129
+        $app->dispatch('ShareController', 'downloadShare');
130
+    } else {
131
+        throw new \OC\HintException('App file sharing is not enabled');
132
+    }
133 133
 });
134 134
 
135 135
 // used for heartbeat
136 136
 $this->create('heartbeat', '/heartbeat')->action(function(){
137
-	// do nothing
137
+    // do nothing
138 138
 });
Please login to merge, or discard this patch.
lib/private/Contacts/ContactsMenu/Manager.php 1 patch
Indentation   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -30,82 +30,82 @@
 block discarded – undo
30 30
 
31 31
 class Manager {
32 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
-	}
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 110
 
111 111
 }
Please login to merge, or discard this patch.
lib/private/Contacts/ContactsMenu/ContactsStore.php 2 patches
Indentation   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -30,110 +30,110 @@
 block discarded – undo
30 30
 
31 31
 class ContactsStore {
32 32
 
33
-	/** @var IManager */
34
-	private $contactsManager;
35
-
36
-	/**
37
-	 * @param IManager $contactsManager
38
-	 */
39
-	public function __construct(IManager $contactsManager) {
40
-		$this->contactsManager = $contactsManager;
41
-	}
42
-
43
-	/**
44
-	 * @param IUser $user
45
-	 * @param string|null $filter
46
-	 * @return IEntry[]
47
-	 */
48
-	public function getContacts(IUser $user, $filter) {
49
-		$allContacts = $this->contactsManager->search($filter ?: '', [
50
-			'FN',
51
-		]);
52
-
53
-		$self = $user->getUID();
54
-		$entries = array_map(function(array $contact) {
55
-			return $this->contactArrayToEntry($contact);
56
-		}, $allContacts);
57
-		return array_filter($entries, function(IEntry $entry) use ($self) {
58
-			return $entry->getProperty('UID') !== $self;
59
-		});
60
-	}
61
-
62
-	/**
63
-	 * @param IUser $user
64
-	 * @param integer $shareType
65
-	 * @param string $shareWith
66
-	 * @return IEntry|null
67
-	 */
68
-	public function findOne(IUser $user, $shareType, $shareWith) {
69
-		switch($shareType) {
70
-			case 0:
71
-			case 6:
72
-				$filter = ['UID'];
73
-				break;
74
-			case 4:
75
-				$filter = ['EMAIL'];
76
-				break;
77
-			default:
78
-				return null;
79
-		}
80
-
81
-		$userId = $user->getUID();
82
-		$allContacts = $this->contactsManager->search($shareWith, $filter);
83
-		$contacts = array_filter($allContacts, function($contact) use ($userId) {
84
-			return $contact['UID'] !== $userId;
85
-		});
86
-		$match = null;
87
-
88
-		foreach ($contacts as $contact) {
89
-			if ($shareType === 4 && isset($contact['EMAIL'])) {
90
-				if (in_array($shareWith, $contact['EMAIL'])) {
91
-					$match = $contact;
92
-					break;
93
-				}
94
-			}
95
-			if ($shareType === 0 || $shareType === 6) {
96
-				if ($contact['UID'] === $shareWith && $contact['isLocalSystemBook'] === true) {
97
-					$match = $contact;
98
-					break;
99
-				}
100
-			}
101
-		}
102
-
103
-		return $match ? $this->contactArrayToEntry($match) : null;
104
-	}
105
-
106
-	/**
107
-	 * @param array $contact
108
-	 * @return Entry
109
-	 */
110
-	private function contactArrayToEntry(array $contact) {
111
-		$entry = new Entry();
112
-
113
-		if (isset($contact['id'])) {
114
-			$entry->setId($contact['id']);
115
-		}
116
-
117
-		if (isset($contact['FN'])) {
118
-			$entry->setFullName($contact['FN']);
119
-		}
120
-
121
-		$avatarPrefix = "VALUE=uri:";
122
-		if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
123
-			$entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
124
-		}
125
-
126
-		if (isset($contact['EMAIL'])) {
127
-			foreach ($contact['EMAIL'] as $email) {
128
-				$entry->addEMailAddress($email);
129
-			}
130
-		}
131
-
132
-		// Attach all other properties to the entry too because some
133
-		// providers might make use of it.
134
-		$entry->setProperties($contact);
135
-
136
-		return $entry;
137
-	}
33
+    /** @var IManager */
34
+    private $contactsManager;
35
+
36
+    /**
37
+     * @param IManager $contactsManager
38
+     */
39
+    public function __construct(IManager $contactsManager) {
40
+        $this->contactsManager = $contactsManager;
41
+    }
42
+
43
+    /**
44
+     * @param IUser $user
45
+     * @param string|null $filter
46
+     * @return IEntry[]
47
+     */
48
+    public function getContacts(IUser $user, $filter) {
49
+        $allContacts = $this->contactsManager->search($filter ?: '', [
50
+            'FN',
51
+        ]);
52
+
53
+        $self = $user->getUID();
54
+        $entries = array_map(function(array $contact) {
55
+            return $this->contactArrayToEntry($contact);
56
+        }, $allContacts);
57
+        return array_filter($entries, function(IEntry $entry) use ($self) {
58
+            return $entry->getProperty('UID') !== $self;
59
+        });
60
+    }
61
+
62
+    /**
63
+     * @param IUser $user
64
+     * @param integer $shareType
65
+     * @param string $shareWith
66
+     * @return IEntry|null
67
+     */
68
+    public function findOne(IUser $user, $shareType, $shareWith) {
69
+        switch($shareType) {
70
+            case 0:
71
+            case 6:
72
+                $filter = ['UID'];
73
+                break;
74
+            case 4:
75
+                $filter = ['EMAIL'];
76
+                break;
77
+            default:
78
+                return null;
79
+        }
80
+
81
+        $userId = $user->getUID();
82
+        $allContacts = $this->contactsManager->search($shareWith, $filter);
83
+        $contacts = array_filter($allContacts, function($contact) use ($userId) {
84
+            return $contact['UID'] !== $userId;
85
+        });
86
+        $match = null;
87
+
88
+        foreach ($contacts as $contact) {
89
+            if ($shareType === 4 && isset($contact['EMAIL'])) {
90
+                if (in_array($shareWith, $contact['EMAIL'])) {
91
+                    $match = $contact;
92
+                    break;
93
+                }
94
+            }
95
+            if ($shareType === 0 || $shareType === 6) {
96
+                if ($contact['UID'] === $shareWith && $contact['isLocalSystemBook'] === true) {
97
+                    $match = $contact;
98
+                    break;
99
+                }
100
+            }
101
+        }
102
+
103
+        return $match ? $this->contactArrayToEntry($match) : null;
104
+    }
105
+
106
+    /**
107
+     * @param array $contact
108
+     * @return Entry
109
+     */
110
+    private function contactArrayToEntry(array $contact) {
111
+        $entry = new Entry();
112
+
113
+        if (isset($contact['id'])) {
114
+            $entry->setId($contact['id']);
115
+        }
116
+
117
+        if (isset($contact['FN'])) {
118
+            $entry->setFullName($contact['FN']);
119
+        }
120
+
121
+        $avatarPrefix = "VALUE=uri:";
122
+        if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
123
+            $entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
124
+        }
125
+
126
+        if (isset($contact['EMAIL'])) {
127
+            foreach ($contact['EMAIL'] as $email) {
128
+                $entry->addEMailAddress($email);
129
+            }
130
+        }
131
+
132
+        // Attach all other properties to the entry too because some
133
+        // providers might make use of it.
134
+        $entry->setProperties($contact);
135
+
136
+        return $entry;
137
+    }
138 138
 
139 139
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -66,7 +66,7 @@
 block discarded – undo
66 66
 	 * @return IEntry|null
67 67
 	 */
68 68
 	public function findOne(IUser $user, $shareType, $shareWith) {
69
-		switch($shareType) {
69
+		switch ($shareType) {
70 70
 			case 0:
71 71
 			case 6:
72 72
 				$filter = ['UID'];
Please login to merge, or discard this patch.