Completed
Push — master ( 160a90...29891d )
by Joas
13:59
created

CloudIDFormatter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 12
c 5
b 0
f 1
lcom 1
cbo 0
dl 0
loc 72
ccs 34
cts 34
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A format() 0 19 4
C getDisplayNameFromContact() 0 25 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 10
	public function __construct(IManager $contactsManager) {
40 10
		$this->manager = $contactsManager;
41 10
		$this->federatedContacts = [];
42 10
	}
43
44
	/**
45
	 * @param IEvent $event
46
	 * @param string $parameter The parameter to be formatted
47
	 * @return string The formatted parameter
48
	 */
49 4
	public function format(IEvent $event, $parameter) {
50 4
		$displayName = $parameter;
51
		try {
52 4
			list($user, $server) = Helper::splitUserRemote($parameter);
53 4
		} 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...
54 1
			$user = $parameter;
55 1
			$server = '';
56
		}
57
58 4
		if ($server !== '') {
59 3
			$displayName = $user . '@…';
60 3
		}
61
62
		try {
63 4
			$displayName = $this->getDisplayNameFromContact($parameter);
64 4
		} catch (\OutOfBoundsException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
65
66 4
		return '<federated-cloud-id display-name="' . Util::sanitizeHTML($displayName) . '" user="' . Util::sanitizeHTML($user) . '" server="' . Util::sanitizeHTML($server) . '">' . Util::sanitizeHTML($parameter) . '</federated-cloud-id>';
67
	}
68
69
	/**
70
	 * Try to find the user in the contacts
71
	 *
72
	 * @param string $federatedCloudId
73
	 * @return string
74
	 * @throws \OutOfBoundsException when there is no contact for the id
75
	 */
76 5
	protected function getDisplayNameFromContact($federatedCloudId) {
77 5
		$federatedCloudId = strtolower($federatedCloudId);
78 5
		if (isset($this->federatedContacts[$federatedCloudId])) {
79 5
			if ($this->federatedContacts[$federatedCloudId] !== '') {
80 2
				return $this->federatedContacts[$federatedCloudId];
81
			} else {
82 3
				throw new \OutOfBoundsException('No contact found for federated cloud id');
83
			}
84
		}
85
86 5
		$addressBookEntries = $this->manager->search($federatedCloudId, ['CLOUD']);
87 5
		foreach ($addressBookEntries as $entry) {
88 4
			if (isset($entry['CLOUD'])) {
89 3
				foreach ($entry['CLOUD'] as $cloudID) {
90 2
					if ($cloudID === $federatedCloudId) {
91 2
						$this->federatedContacts[$federatedCloudId] = $entry['FN'];
92 2
						return $entry['FN'];
93
					}
94 2
				}
95 1
			}
96 3
		}
97
98 3
		$this->federatedContacts[$federatedCloudId] = '';
99 3
		throw new \OutOfBoundsException('No contact found for federated cloud id');
100
	}
101
}
102