Completed
Pull Request — master (#1352)
by Christoph
07:51
created

ContactsIntegration   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.3%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 11
c 2
b 1
f 0
lcom 1
cbo 0
dl 0
loc 75
ccs 36
cts 37
cp 0.973
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getMatchingRecipient() 0 31 6
A getPhoto() 0 17 4
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 *
7
 * ownCloud - Mail
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Mail\Service;
24
25
use OCP\Contacts\IManager;
26
27
class ContactsIntegration {
28
29
	/**
30
	 * @var IManager
31
	 */
32
	private $contactsManager;
33
34
	/**
35
	 * @param IManager $contactsManager
36
	 */
37 5
	public function __construct(IManager $contactsManager) {
38 5
		$this->contactsManager = $contactsManager;
39 5
	}
40
41
	/**
42
	 * Extracts all matching contacts with email address and name
43
	 *
44
	 * @param string $term
45
	 * @return array
46
	 */
47 2
	public function getMatchingRecipient($term) {
48 2
		if (!$this->contactsManager->isEnabled()) {
49 1
			return [];
50
		}
51
52 1
		$result = $this->contactsManager->search($term, ['FN', 'EMAIL']);
53 1
		$receivers = [];
54 1
		foreach ($result as $r) {
55 1
			$id = $r['id'];
56 1
			$fn = $r['FN'];
57 1
			if (!isset($r['EMAIL'])) {
58 1
				continue;
59
			}
60 1
			$email = $r['EMAIL'];
61 1
			if (!is_array($email)) {
62 1
				$email = [$email];
63 1
			}
64
65
			// loop through all email addresses of this contact
66 1
			foreach ($email as $e) {
67 1
				$displayName = "\"$fn\" <$e>";
68 1
				$receivers[] = [
69 1
					'id' => $id,
70 1
					'label' => $displayName,
71
					'value' => $displayName
72 1
				];
73 1
			}
74 1
		}
75
76 1
		return $receivers;
77
	}
78
79
	/**
80
	 * @param string $email
81
	 * @return null|string
82
	 */
83 3
	public function getPhoto($email) {
84 3
		$result = $this->contactsManager->search($email, ['EMAIL']);
85 3
		$uriPrefix = 'VALUE=uri:';
86 3
		if (count($result) > 0) {
87 3
			if (isset($result[0]['PHOTO'])) {
88 1
				$s = $result[0]['PHOTO'];
89 1
				if (substr($s, 0, strlen($uriPrefix)) === $uriPrefix) {
90
					return substr($s, strpos($s, 'http'));
91
				} else {
92
					// ignore contacts >= 1.0 binary images
93
					// TODO: fix
94 1
					return null;
95
				}
96
			}
97 2
		}
98 2
		return null;
99
	}
100
101
}
102