UserFormatter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 59
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A format() 0 21 4
A isRemoteUser() 0 3 1
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 OCP\Activity\IEvent;
26
use OCP\IL10N;
27
use OCP\IUser;
28
use OCP\IUserManager;
29
use OCP\Util;
30
31
class UserFormatter implements IFormatter {
32
33
	/** @var IUserManager */
34
	protected $manager;
35
36
	/** @var IL10N */
37
	protected $l;
38
39
	/** @var CloudIDFormatter */
40
	protected $cloudIDFormatter;
41
42
	/**
43
	 * @param IUserManager $userManager
44
	 * @param CloudIDFormatter $cloudIDFormatter
45
	 * @param IL10N $l
46
	 */
47 11
	public function __construct(IUserManager $userManager, CloudIDFormatter $cloudIDFormatter, IL10N $l) {
48 11
		$this->manager = $userManager;
49 11
		$this->l = $l;
50 11
		$this->cloudIDFormatter = $cloudIDFormatter;
51 11
	}
52
53
	/**
54
	 * @param IEvent $event
55
	 * @param string $parameter The parameter to be formatted
56
	 * @return string The formatted parameter
57
	 */
58 9
	public function format(IEvent $event, $parameter) {
59
		// If the username is empty, the action has been performed by a remote
60
		// user, or via a public share. We don't know the username in that case
61 9
		if ($parameter === '') {
62 1
			return '<user display-name="' . Util::sanitizeHTML($this->l->t('"remote user"')) . '">' . Util::sanitizeHTML('') . '</user>';
63
		}
64
65 8
		$user = $this->manager->get($parameter);
66 8
		if (!($user instanceof IUser)) {
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67 6
			if ($this->isRemoteUser($parameter)) {
68
				// Remote user detected
69 1
				return $this->cloudIDFormatter->format($event, $parameter);
70
			}
71 5
			$displayName = $parameter;
72
		} else {
73 2
			$displayName = $user->getDisplayName();
74
		}
75 7
		$parameter = Util::sanitizeHTML($parameter);
76
77 7
		return '<user display-name="' . Util::sanitizeHTML($displayName) . '">' . Util::sanitizeHTML($parameter) . '</user>';
78
	}
79
80
	/**
81
	 * Very simple "remote user" detection should be improved someday™
82
	 *
83
	 * @param string $parameter
84
	 * @return bool
85
	 */
86 6
	protected function isRemoteUser($parameter) {
87 6
		return strpos($parameter, '@') > 0;
88
	}
89
}
90