Completed
Pull Request — master (#536)
by Thomas
15:31
created

UserFormatter::isRemoteUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, 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 OCP\Activity\IEvent;
25
use OCP\IL10N;
26
use OCP\IUser;
27
use OCP\IUserManager;
28
use OCP\Util;
29
30
class UserFormatter implements IFormatter {
31
32
	/** @var IUserManager */
33
	protected $manager;
34
35
	/** @var IL10N */
36
	protected $l;
37
38
	/** @var CloudIDFormatter */
39
	protected $cloudIDFormatter;
40 8
41 8
	/**
42 8
	 * @param IUserManager $userManager
43 8
	 * @param CloudIDFormatter $cloudIDFormatter
44
	 * @param IL10N $l
45
	 */
46
	public function __construct(IUserManager $userManager, CloudIDFormatter $cloudIDFormatter, IL10N $l) {
47
		$this->manager = $userManager;
48
		$this->l = $l;
49
		$this->cloudIDFormatter = $cloudIDFormatter;
50 7
	}
51
52
	/**
53 7
	 * @param IEvent $event
54 1
	 * @param string $parameter The parameter to be formatted
55
	 * @return string The formatted parameter
56
	 */
57 6
	public function format(IEvent $event, $parameter) {
58 6
		// If the username is empty, the action has been performed by a remote
59 6
		// user, or via a public share. We don't know the username in that case
60
		if ($parameter === '') {
61 6
			return '<user display-name="' . Util::sanitizeHTML($this->l->t('"remote user"')) . '">' . Util::sanitizeHTML('') . '</user>';
62
		}
63
64
		$user = $this->manager->get($parameter);
65
		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...
66
			if ($this->isRemoteUser($parameter)) {
67
				// Remote user detected
68
				return $this->cloudIDFormatter->format($event, $parameter);
69
			}
70
			$displayName = $parameter;
71
		} else {
72
			$displayName = $user->getDisplayName();
73
		}
74
		$parameter = Util::sanitizeHTML($parameter);
75
76
		return '<user display-name="' . Util::sanitizeHTML($displayName) . '">' . Util::sanitizeHTML($parameter) . '</user>';
77
	}
78
79
	/**
80
	 * Very simple "remote user" detection should be improved someday™
81
	 *
82
	 * @param string $parameter
83
	 * @return bool
84
	 */
85
	protected function isRemoteUser($parameter) {
86
		return strpos($parameter, '@') > 0;
87
	}
88
}
89