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

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