Completed
Push — stable9 ( 3c64b7...7d6fa4 )
by Joas
9s
created

FileFormatter::splitPathFromFilename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
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
	 * @param string $user
45
	 */
46 24
	public function __construct(ViewInfoCache $infoCache, IURLGenerator $urlGenerator, IL10N $l, $user) {
47 24
		$this->infoCache = $infoCache;
48 24
		$this->urlGenerator = $urlGenerator;
49 24
		$this->l = $l;
50 24
		$this->user = $user;
51 24
	}
52
53
	/**
54
	 * @param IEvent $event
55
	 * @param string $parameter The parameter to be formatted
56
	 * @return string The formatted parameter
57
	 */
58 11
	public function format(IEvent $event, $parameter) {
59 11
		$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 11
		$fileId = '';
64 11
		if (is_array($param)) {
65
			$fileId = key($param);
66
			$param = $param[$fileId];
67
			$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
68 11
		} 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 9
			$info = $this->infoCache->getInfoByPath($this->user, $param);
73
		}
74
75 11
		if ($info['is_dir']) {
76 3
			$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 11
		if ($info['view'] !== '') {
87 2
			$linkData['view'] = $info['view'];
88
		}
89
90 11
		$param = trim($param, '/');
91 11
		$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.view.index', $linkData);
92
93 11
		return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>';
94
	}
95
96
	/**
97
	 * Prepend leading slash to filenames of legacy activities
98
	 * @param string|array $filename
99
	 * @return string|array
100
	 */
101 10
	protected function fixLegacyFilename($filename) {
102 10
		if (is_array($filename)) {
103
			// 9.0: [fileId => path]
104 2
			return $filename;
105
		}
106 8
		if (strpos($filename, '/') !== 0) {
107 2
			return '/' . $filename;
108
		}
109 6
		return $filename;
110
	}
111
112
	/**
113
	 * Split the path from the filename string
114
	 *
115
	 * @param string $filename
116
	 * @return array Array with path and filename
117
	 */
118 6
	protected function splitPathFromFilename($filename) {
119 6
		if (strrpos($filename, '/') !== false) {
120
			return array(
121 5
				trim(substr($filename, 0, strrpos($filename, '/')), '/'),
122 5
				substr($filename, strrpos($filename, '/') + 1),
123
			);
124
		}
125 1
		return array('', $filename);
126
	}
127
}
128