FileFormatter::format()   B
last analyzed

Complexity

Conditions 8
Paths 36

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8.125

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 21
cts 24
cp 0.875
rs 8.0195
c 0
b 0
f 0
cc 8
nc 36
nop 2
crap 8.125
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 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 25
	public function __construct(ViewInfoCache $infoCache, IURLGenerator $urlGenerator, IL10N $l, $user) {
47 25
		$this->infoCache = $infoCache;
48 25
		$this->urlGenerator = $urlGenerator;
49 25
		$this->l = $l;
50 25
		$this->user = $user;
51 25
	}
52
53
	/**
54
	 * @param IEvent $event
55
	 * @param string $parameter The parameter to be formatted
56
	 * @return string The formatted parameter
57
	 */
58 12
	public function format(IEvent $event, $parameter) {
59 12
		$param = $this->fixLegacyFilename($parameter);
60
61
		// If the activity is about the very same file, we use the current path
62
		// for the link generation instead of the one that was saved.
63 12
		$fileId = '';
64 12
		if (\is_array($param)) {
65
			$fileId = \key($param);
66
			$param = $param[$fileId];
67
			$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
68 12
		} elseif ($event->getObjectType() === 'files' && $event->getObjectName() === $param) {
69 2
			$fileId = $event->getObjectId();
70 2
			$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
71
		} else {
72 10
			$info = $this->infoCache->getInfoByPath($this->user, $param);
73
		}
74
75 12
		if ($info['is_dir']) {
76 4
			$linkData = ['dir' => $info['path']];
77
		} else {
78 8
			$parentDir = (\substr_count($info['path'], '/') === 1) ? '/' : \dirname($info['path']);
79 8
			$fileName = \basename($info['path']);
80
			$linkData = [
81 8
				'dir' => $parentDir,
82 8
				'scrollto' => $fileName,
83
			];
84
		}
85
86 12
		if ($info['view'] !== '') {
87 2
			$linkData['view'] = $info['view'];
88
		}
89
90 12
		$param = \trim($param, '/');
91 12
		if ($param === '') {
92 1
			$param = '/';
93
		}
94
95 12
		$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.view.index', $linkData);
96
97 12
		return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>';
98
	}
99
100
	/**
101
	 * Prepend leading slash to filenames of legacy activities
102
	 * @param string|array $filename
103
	 * @return string|array
104
	 */
105 10
	protected function fixLegacyFilename($filename) {
106 10
		if (\is_array($filename)) {
107
			// 9.0: [fileId => path]
108 2
			return $filename;
109
		}
110 8
		if (\strpos($filename, '/') !== 0) {
111 2
			return '/' . $filename;
112
		}
113 6
		return $filename;
114
	}
115
116
	/**
117
	 * Split the path from the filename string
118
	 *
119
	 * @param string $filename
120
	 * @return array Array with path and filename
121
	 */
122 6
	protected function splitPathFromFilename($filename) {
123 6
		if (\strrpos($filename, '/') !== false) {
124
			return [
125 5
				\trim(\substr($filename, 0, \strrpos($filename, '/')), '/'),
126 5
				\substr($filename, \strrpos($filename, '/') + 1),
127
			];
128
		}
129 1
		return ['', $filename];
130
	}
131
}
132