Completed
Push — master ( 5412c2...2d62f9 )
by Blizzz
45:23 queued 29:26
created

MailPlugin   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 130
Duplicated Lines 28.46 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 37
loc 130
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
D search() 30 103 18

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Collaboration\Collaborators;
25
26
27
use OCP\Collaboration\Collaborators\ISearchPlugin;
28
use OCP\Collaboration\Collaborators\ISearchResult;
29
use OCP\Collaboration\Collaborators\SearchResultType;
30
use OCP\Contacts\IManager;
31
use OCP\Federation\ICloudIdManager;
32
use OCP\IConfig;
33
use OCP\Share;
34
35
class MailPlugin implements ISearchPlugin {
36
	protected $shareeEnumeration;
37
38
	/** @var IManager */
39
	private $contactsManager;
40
	/** @var ICloudIdManager */
41
	private $cloudIdManager;
42
	/** @var IConfig */
43
	private $config;
44
45 View Code Duplication
	public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config) {
46
		$this->contactsManager = $contactsManager;
47
		$this->cloudIdManager = $cloudIdManager;
48
		$this->config = $config;
49
50
		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
51
	}
52
53
	/**
54
	 * @param $search
55
	 * @param $limit
56
	 * @param $offset
57
	 * @param ISearchResult $searchResult
58
	 * @return bool
59
	 * @since 13.0.0
60
	 */
61
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
62
		$result = ['wide' => [], 'exact' => []];
63
		$userType = new SearchResultType('users');
64
		$emailType = new SearchResultType('emails');
65
66
		// Search in contacts
67
		//@todo Pagination missing
68
		$addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']);
69
		$lowerSearch = strtolower($search);
70
		foreach ($addressBookContacts as $contact) {
71
			if (isset($contact['EMAIL'])) {
72
				$emailAddresses = $contact['EMAIL'];
73
				if (!is_array($emailAddresses)) {
74
					$emailAddresses = [$emailAddresses];
75
				}
76
				foreach ($emailAddresses as $emailAddress) {
77
					$exactEmailMatch = strtolower($emailAddress) === $lowerSearch;
78
79
					if (isset($contact['isLocalSystemBook'])) {
80
						if ($exactEmailMatch) {
81
							try {
82
								$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
83
							} catch (\InvalidArgumentException $e) {
84
								continue;
85
							}
86
87 View Code Duplication
							if (!$searchResult->hasResult($userType, $cloud->getUser())) {
88
								$singleResult = [[
89
									'label' => $contact['FN'] . " ($emailAddress)",
90
									'value' => [
91
										'shareType' => Share::SHARE_TYPE_USER,
92
										'shareWith' => $cloud->getUser(),
93
									],
94
								]];
95
								$searchResult->addResultSet($userType, [], $singleResult);
96
								$searchResult->markExactIdMatch($emailType);
97
							}
98
							return false;
99
						}
100
101
						if ($this->shareeEnumeration) {
102
							try {
103
								$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
104
							} catch (\InvalidArgumentException $e) {
105
								continue;
106
							}
107
108 View Code Duplication
							if (!$searchResult->hasResult($userType, $cloud->getUser())) {
109
								$singleResult = [[
110
									'label' => $contact['FN'] . " ($emailAddress)",
111
									'value' => [
112
										'shareType' => Share::SHARE_TYPE_USER,
113
										'shareWith' => $cloud->getUser(),
114
									]],
115
								];
116
								$searchResult->addResultSet($userType, $singleResult, []);
117
							}
118
						}
119
						continue;
120
					}
121
122
					if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) {
123
						if ($exactEmailMatch) {
124
							$searchResult->markExactIdMatch($emailType);
125
						}
126
						$result['exact'][] = [
127
							'label' => $contact['FN'] . " ($emailAddress)",
128
							'value' => [
129
								'shareType' => Share::SHARE_TYPE_EMAIL,
130
								'shareWith' => $emailAddress,
131
							],
132
						];
133 View Code Duplication
					} else {
134
						$result['wide'][] = [
135
							'label' => $contact['FN'] . " ($emailAddress)",
136
							'value' => [
137
								'shareType' => Share::SHARE_TYPE_EMAIL,
138
								'shareWith' => $emailAddress,
139
							],
140
						];
141
					}
142
				}
143
			}
144
		}
145
146
		if (!$this->shareeEnumeration) {
147
			$result['wide'] = [];
148
		}
149
150
		if (!$searchResult->hasExactIdMatch($emailType) && filter_var($search, FILTER_VALIDATE_EMAIL)) {
151
			$result['exact'][] = [
152
				'label' => $search,
153
				'value' => [
154
					'shareType' => Share::SHARE_TYPE_EMAIL,
155
					'shareWith' => $search,
156
				],
157
			];
158
		}
159
160
		$searchResult->addResultSet($emailType, $result['wide'], $result['exact']);
161
162
		return true;
163
	}
164
}
165