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

FileFormatter::format()   C

Complexity

Conditions 11
Paths 60

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 11

Importance

Changes 6
Bugs 1 Features 3
Metric Value
c 6
b 1
f 3
dl 0
loc 50
ccs 32
cts 32
cp 1
rs 5.4894
cc 11
eloc 31
nc 60
nop 4
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 OCA\Activity\ViewInfoCache;
25
use OCP\Activity\IEvent;
26
use OCP\IL10N;
27
use OCP\IURLGenerator;
28
use OCP\Util;
29
30
class FileFormatter implements IFormatter {
31
	/** @var ViewInfoCache */
32
	protected $infoCache;
33
	/** @var IURLGenerator */
34
	protected $urlGenerator;
35
	/** @var IL10N */
36
	protected $l;
37
	/** @var string */
38
	protected $user;
39
40
	/**
41
	 * @param ViewInfoCache $infoCache
42
	 * @param IURLGenerator $urlGenerator
43
	 * @param IL10N $l
44
	 * @param string $user
45
	 */
46 40
	public function __construct(ViewInfoCache $infoCache, IURLGenerator $urlGenerator, IL10N $l, $user) {
47 40
		$this->infoCache = $infoCache;
48 40
		$this->urlGenerator = $urlGenerator;
49 40
		$this->l = $l;
50 40
		$this->user = $user;
51 40
	}
52
53
	/**
54
	 * @param IEvent $event
55
	 * @param string $parameter The parameter to be formatted
56
	 * @param bool $allowHtml   Should HTML be used to format the parameter?
57
	 * @param bool $verbose     Should paths, names, etc be shortened or full length
58
	 * @return string The formatted parameter
59
	 */
60 29
	public function format(IEvent $event, $parameter, $allowHtml, $verbose = false) {
61 29
		$param = $this->fixLegacyFilename($parameter);
62
63
		// If the activity is about the very same file, we use the current path
64
		// for the link generation instead of the one that was saved.
65 29
		$fileId = '';
66 29
		if ($event->getObjectType() === 'files' && $event->getObjectName() === $param) {
67 8
			$fileId = $event->getObjectId();
68 8
			$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
69 8
		} else {
70 21
			$info = $this->infoCache->getInfoByPath($this->user, $param);
71
		}
72
73 29
		if ($info['is_dir']) {
74 12
			$linkData = ['dir' => $info['path']];
75 12
		} else {
76 17
			$parentDir = (substr_count($info['path'], '/') === 1) ? '/' : dirname($info['path']);
77 17
			$fileName = basename($info['path']);
78
			$linkData = [
79 17
				'dir' => $parentDir,
80 17
				'scrollto' => $fileName,
81 17
			];
82
		}
83
84 29
		if ($info['view'] !== '') {
85 8
			$linkData['view'] = $info['view'];
86 8
		}
87
88 29
		$param = trim($param, '/');
89 29
		list($path, $name) = $this->splitPathFromFilename($param);
90 29
		$fileLink = $this->urlGenerator->linkTo('files', 'index.php', $linkData);
91
92 29
		if ($allowHtml === null) {
93 4
			return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>';
94
		}
95
96 29
		if ($verbose || $path === '') {
97 21
			if (!$allowHtml) {
98 12
				return $param;
99
			}
100 13
			return '<a class="filename" href="' . $fileLink . '">' . Util::sanitizeHTML($param) . '</a>';
101
		}
102
103 12
		if (!$allowHtml) {
104 8
			return $name;
105
		}
106
107 8
		$title = ' title="' . $this->l->t('in %s', array(Util::sanitizeHTML($path))) . '"';
108 8
		return '<a class="filename has-tooltip" href="' . $fileLink . '"' . $title . '>' . Util::sanitizeHTML($name) . '</a>';
109
	}
110
111
	/**
112
	 * Prepend leading slash to filenames of legacy activities
113
	 * @param string $filename
114
	 * @return string
115
	 */
116 8
	protected function fixLegacyFilename($filename) {
117 8
		if (strpos($filename, '/') !== 0) {
118 2
			return '/' . $filename;
119
		}
120 6
		return $filename;
121
	}
122
123
	/**
124
	 * Split the path from the filename string
125
	 *
126
	 * @param string $filename
127
	 * @return array Array with path and filename
128
	 */
129 35
	protected function splitPathFromFilename($filename) {
130 35
		if (strrpos($filename, '/') !== false) {
131
			return array(
132 25
				trim(substr($filename, 0, strrpos($filename, '/')), '/'),
133 25
				substr($filename, strrpos($filename, '/') + 1),
134 25
			);
135
		}
136 10
		return array('', $filename);
137
	}
138
}
139