CloudIDFormatter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A format() 0 19 4
B getDisplayNameFromContact() 0 25 7
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license AGPL-3.0
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\Activity\Formatter;
24
25
use OC\HintException;
26
use OC\Share\Helper;
27
use OCP\Activity\IEvent;
28
use OCP\Contacts\IManager;
29
use OCP\Util;
30
31
class CloudIDFormatter implements IFormatter {
32
	/** @var IManager */
33
	protected $manager;
34
	/** @var array */
35
	protected $federatedContacts;
36
37
	/**
38
	 * @param IManager $contactsManager
39
	 */
40 16
	public function __construct(IManager $contactsManager) {
41 16
		$this->manager = $contactsManager;
42 16
		$this->federatedContacts = [];
43 16
	}
44
45
	/**
46
	 * @param IEvent $event
47
	 * @param string $parameter The parameter to be formatted
48
	 * @return string The formatted parameter
49
	 */
50 4
	public function format(IEvent $event, $parameter) {
51 4
		$displayName = $parameter;
52
		try {
53 4
			list($user, $server) = Helper::splitUserRemote($parameter);
54 1
		} 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...
55 1
			$user = $parameter;
56 1
			$server = '';
57
		}
58
59 4
		if ($server !== '') {
60 3
			$displayName = $user . '@…';
61
		}
62
63
		try {
64 4
			$displayName = $this->getDisplayNameFromContact($parameter);
65 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...
66
67 4
		return '<federated-cloud-id display-name="' . Util::sanitizeHTML($displayName) . '" user="' . Util::sanitizeHTML($user) . '" server="' . Util::sanitizeHTML($server) . '">' . Util::sanitizeHTML($parameter) . '</federated-cloud-id>';
68
	}
69
70
	/**
71
	 * Try to find the user in the contacts
72
	 *
73
	 * @param string $federatedCloudId
74
	 * @return string
75
	 * @throws \OutOfBoundsException when there is no contact for the id
76
	 */
77 5
	protected function getDisplayNameFromContact($federatedCloudId) {
78 5
		$federatedCloudId = strtolower($federatedCloudId);
79 5
		if (isset($this->federatedContacts[$federatedCloudId])) {
80 5
			if ($this->federatedContacts[$federatedCloudId] !== '') {
81 2
				return $this->federatedContacts[$federatedCloudId];
82
			} else {
83 3
				throw new \OutOfBoundsException('No contact found for federated cloud id');
84
			}
85
		}
86
87 5
		$addressBookEntries = $this->manager->search($federatedCloudId, ['CLOUD']);
88 5
		foreach ($addressBookEntries as $entry) {
89 4
			if (isset($entry['CLOUD'])) {
90 3
				foreach ($entry['CLOUD'] as $cloudID) {
91 2
					if ($cloudID === $federatedCloudId) {
92 2
						$this->federatedContacts[$federatedCloudId] = $entry['FN'];
93 4
						return $entry['FN'];
94
					}
95
				}
96
			}
97
		}
98
99 3
		$this->federatedContacts[$federatedCloudId] = '';
100 3
		throw new \OutOfBoundsException('No contact found for federated cloud id');
101
	}
102
}
103