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

UserFormatter::format()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 32
ccs 19
cts 19
cp 1
rs 5.3846
cc 8
eloc 20
nc 11
nop 4
crap 8
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 OCP\Activity\IEvent;
25
use OCP\IConfig;
26
use OCP\IL10N;
27
use OCP\IUserManager;
28
use OCP\Util;
29
30
class UserFormatter implements IFormatter {
31
	/** @var IUserManager */
32
	protected $manager;
33
	/** @var IConfig */
34
	protected $config;
35
	/** @var IL10N */
36
	protected $l;
37
38
	/**
39
	 * @param IUserManager $userManager
40
	 * @param IConfig $config
41
	 * @param IL10N $l
42
	 */
43 27
	public function __construct(IUserManager $userManager, IConfig $config, IL10N $l) {
44 27
		$this->manager = $userManager;
45 27
		$this->config = $config;
46 27
		$this->l = $l;
47 27
	}
48
49
	/**
50
	 * @param IEvent $event
51
	 * @param string $parameter The parameter to be formatted
52
	 * @param bool $allowHtml   Should HTML be used to format the parameter?
53
	 * @param bool $verbose     Should paths, names, etc be shortened or full length
54
	 * @return string The formatted parameter
55
	 */
56 26
	public function format(IEvent $event, $parameter, $allowHtml, $verbose = false) {
57
		// If the username is empty, the action has been performed by a remote
58
		// user, or via a public share. We don't know the username in that case
59 26
		if ($parameter === '') {
60 5
			if ($allowHtml === null) {
61 1
				return '<user display-name="' . Util::sanitizeHTML($this->l->t('"remote user"')) . '">' . Util::sanitizeHTML('') . '</user>';
62
			}
63 4
			if ($allowHtml) {
64 2
				return '<strong>' . $this->l->t('"remote user"') . '</strong>';
65
			} else {
66 2
				return $this->l->t('"remote user"');
67
			}
68
		}
69
70 21
		$user = $this->manager->get($parameter);
71 21
		$displayName = ($user) ? $user->getDisplayName() : $parameter;
72 21
		$parameter = Util::sanitizeHTML($parameter);
73
74 21
		if ($allowHtml === null) {
75 5
			return '<user display-name="' . Util::sanitizeHTML($displayName) . '">' . Util::sanitizeHTML($parameter) . '</user>';
76
		}
77
78 19
		if ($allowHtml) {
79 11
			$avatarPlaceholder = '';
80 11
			if ($this->config->getSystemValue('enable_avatars', true)) {
81 7
				$avatarPlaceholder = '<div class="avatar" data-user="' . $parameter . '"></div>';
82 7
			}
83 11
			return $avatarPlaceholder . '<strong>' . Util::sanitizeHTML($displayName) . '</strong>';
84
		} else {
85 11
			return $displayName;
86
		}
87
	}
88
}
89