Completed
Push — stable9 ( be630e...27be8a )
by Morris
12s
created

FileFormatter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.68%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 102
ccs 38
cts 41
cp 0.9268
rs 10
c 2
b 0
f 0

4 Methods

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