Completed
Pull Request — master (#215)
by Morris
23:16
created

FileFormatter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.88%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 107
ccs 46
cts 49
cp 0.9388
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setUser() 0 3 1
C format() 0 41 8
A fixLegacyFilename() 0 10 3
A splitPathFromFilename() 0 9 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Activity\Formatter;
24
25
use OCA\Activity\ViewInfoCache;
26
use OCP\Activity\IEvent;
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
use OCP\Util;
30
31
class FileFormatter implements IFormatter {
32
	/** @var ViewInfoCache */
33
	protected $infoCache;
34
	/** @var IURLGenerator */
35
	protected $urlGenerator;
36
	/** @var IL10N */
37
	protected $l;
38
	/** @var string */
39
	protected $user;
40
41
	/**
42
	 * @param ViewInfoCache $infoCache
43
	 * @param IURLGenerator $urlGenerator
44
	 * @param IL10N $l
45
	 */
46 24
	public function __construct(ViewInfoCache $infoCache, IURLGenerator $urlGenerator, IL10N $l) {
47 24
		$this->infoCache = $infoCache;
48 24
		$this->urlGenerator = $urlGenerator;
49 24
		$this->l = $l;
50 24
	}
51
52
	/**
53
	 * @param string $user
54
	 */
55 17
	public function setUser($user) {
56 17
		$this->user = (string) $user;
57 17
	}
58
59
	/**
60
	 * @param IEvent $event
61
	 * @param string $parameter The parameter to be formatted
62
	 * @return string The formatted parameter
63
	 */
64 12
	public function format(IEvent $event, $parameter) {
65 12
		$param = $this->fixLegacyFilename($parameter);
66
67
		// If the activity is about the very same file, we use the current path
68
		// for the link generation instead of the one that was saved.
69 12
		$fileId = '';
70 12
		if (is_array($param)) {
71
			$fileId = key($param);
72
			$param = $param[$fileId];
73
			$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
74 12
		} elseif ($event->getObjectType() === 'files' && $event->getObjectName() === $param) {
75 2
			$fileId = $event->getObjectId();
76 2
			$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
77 2
		} else {
78 10
			$info = $this->infoCache->getInfoByPath($this->user, $param);
79
		}
80
81 12
		if ($info['is_dir']) {
82 4
			$linkData = ['dir' => $info['path']];
83 4
		} else {
84 8
			$parentDir = (substr_count($info['path'], '/') === 1) ? '/' : dirname($info['path']);
85 8
			$fileName = basename($info['path']);
86
			$linkData = [
87 8
				'dir' => $parentDir,
88 8
				'scrollto' => $fileName,
89 8
			];
90
		}
91
92 12
		if ($info['view'] !== '') {
93 2
			$linkData['view'] = $info['view'];
94 2
		}
95
96 12
		$param = trim($param, '/');
97 12
		if ($param === '') {
98 1
			$param = '/';
99 1
		}
100
101 12
		$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.view.index', $linkData);
102
103 12
		return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>';
104
	}
105
106
	/**
107
	 * Prepend leading slash to filenames of legacy activities
108
	 * @param string|array $filename
109
	 * @return string|array
110
	 */
111 10
	protected function fixLegacyFilename($filename) {
112 10
		if (is_array($filename)) {
113
			// 9.0: [fileId => path]
114 2
			return $filename;
115
		}
116 8
		if (strpos($filename, '/') !== 0) {
117 2
			return '/' . $filename;
118
		}
119 6
		return $filename;
120
	}
121
122
	/**
123
	 * Split the path from the filename string
124
	 *
125
	 * @param string $filename
126
	 * @return array Array with path and filename
127
	 */
128 6
	protected function splitPathFromFilename($filename) {
129 6
		if (strrpos($filename, '/') !== false) {
130
			return array(
131 5
				trim(substr($filename, 0, strrpos($filename, '/')), '/'),
132 5
				substr($filename, strrpos($filename, '/') + 1),
133 5
			);
134
		}
135 1
		return array('', $filename);
136
	}
137
}
138