Completed
Pull Request — master (#434)
by Joas
04:25
created

CloudIDFormatter::getDisplayNameFromContact()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 18
cts 18
cp 1
rs 6.7273
cc 7
eloc 16
nc 7
nop 1
crap 7
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Activity\Formatter;
23
24
use OC\HintException;
25
use OC\Share\Helper;
26
use OCP\Activity\IEvent;
27
use OCP\Contacts\IManager;
28
use OCP\Util;
29
30
class CloudIDFormatter implements IFormatter {
31
	/** @var IManager */
32
	protected $manager;
33
	/** @var array */
34
	protected $federatedContacts;
35
36
	/**
37
	 * @param IManager $contactsManager
38
	 */
39 19
	public function __construct(IManager $contactsManager) {
40 19
		$this->manager = $contactsManager;
41 19
		$this->federatedContacts = [];
42 19
	}
43
44
	/**
45
	 * @param IEvent $event
46
	 * @param string $parameter The parameter to be formatted
47
	 * @param bool $allowHtml   Should HTML be used to format the parameter?
48
	 * @param bool $verbose     Should paths, names, etc be shortened or full length
49
	 * @return string The formatted parameter
50
	 */
51 13
	public function format(IEvent $event, $parameter, $allowHtml, $verbose = false) {
52 13
		$displayName = $parameter;
53
		try {
54 13
			list($user, $server) = Helper::splitUserRemote($parameter);
55 13
		} catch (HintException $e) {
0 ignored issues
show
Bug introduced by
The class OC\HintException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
56 4
			$user = $parameter;
57 4
			$server = '';
58
		}
59
60 13
		if (!$verbose && $server !== '') {
61 5
			$displayName = $user . '@…';
62 5
		}
63
64
		try {
65 13
			$displayName = $this->getDisplayNameFromContact($parameter);
66 13
		} catch (\OutOfBoundsException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
67
68
69 13
		if ($allowHtml === null) {
70 1
			return '<federated-cloud-id display-name="' . Util::sanitizeHTML($displayName) . '" user="' . $user . '" server="' . $server . '">' . Util::sanitizeHTML($parameter) . '</federated-cloud-id>';
71
		}
72
73 12
		if ($allowHtml) {
74 6
			$title = ' title="' . Util::sanitizeHTML($parameter) . '"';
75 6
			return '<strong class="has-tooltip"' . $title . '>' . Util::sanitizeHTML($displayName) . '</strong>';
76
		} else {
77 6
			return $displayName;
78
		}
79
	}
80
81
	/**
82
	 * Try to find the user in the contacts
83
	 *
84
	 * @param string $federatedCloudId
85
	 * @return string
86
	 * @throws \OutOfBoundsException when there is no contact for the id
87
	 */
88 5
	protected function getDisplayNameFromContact($federatedCloudId) {
89 5
		$federatedCloudId = strtolower($federatedCloudId);
90 5
		if (isset($this->federatedContacts[$federatedCloudId])) {
91 5
			if ($this->federatedContacts[$federatedCloudId] !== '') {
92 2
				return $this->federatedContacts[$federatedCloudId];
93
			} else {
94 3
				throw new \OutOfBoundsException('No contact found for federated cloud id');
95
			}
96
		}
97
98 5
		$addressBookEntries = $this->manager->search($federatedCloudId, ['CLOUD']);
99 5
		foreach ($addressBookEntries as $entry) {
100 4
			if (isset($entry['CLOUD'])) {
101 3
				foreach ($entry['CLOUD'] as $cloudID) {
102 2
					if ($cloudID === $federatedCloudId) {
103 2
						$this->federatedContacts[$federatedCloudId] = $entry['FN'];
104 2
						return $entry['FN'];
105
					}
106 2
				}
107 1
			}
108 3
		}
109
110 3
		$this->federatedContacts[$federatedCloudId] = '';
111 3
		throw new \OutOfBoundsException('No contact found for federated cloud id');
112
	}
113
}
114