Completed
Push — master ( 160a90...29891d )
by Joas
13:59
created

FileFormatter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 11
c 7
b 1
f 3
lcom 1
cbo 1
dl 0
loc 90
ccs 38
cts 38
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B format() 0 33 6
A fixLegacyFilename() 0 6 2
A splitPathFromFilename() 0 9 2
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, 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 22
	public function __construct(ViewInfoCache $infoCache, IURLGenerator $urlGenerator, IL10N $l, $user) {
47 22
		$this->infoCache = $infoCache;
48 22
		$this->urlGenerator = $urlGenerator;
49 22
		$this->l = $l;
50 22
		$this->user = $user;
51 22
	}
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 ($event->getObjectType() === 'files' && $event->getObjectName() === $param) {
65 2
			$fileId = $event->getObjectId();
66 2
			$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
67 2
		} else {
68 9
			$info = $this->infoCache->getInfoByPath($this->user, $param);
69
		}
70
71 11
		if ($info['is_dir']) {
72 3
			$linkData = ['dir' => $info['path']];
73 3
		} else {
74 8
			$parentDir = (substr_count($info['path'], '/') === 1) ? '/' : dirname($info['path']);
75 8
			$fileName = basename($info['path']);
76
			$linkData = [
77 8
				'dir' => $parentDir,
78 8
				'scrollto' => $fileName,
79 8
			];
80
		}
81
82 11
		if ($info['view'] !== '') {
83 2
			$linkData['view'] = $info['view'];
84 2
		}
85
86 11
		$param = trim($param, '/');
87 11
		$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.view.index', $linkData);
88
89 11
		return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>';
90
	}
91
92
	/**
93
	 * Prepend leading slash to filenames of legacy activities
94
	 * @param string $filename
95
	 * @return string
96
	 */
97 8
	protected function fixLegacyFilename($filename) {
98 8
		if (strpos($filename, '/') !== 0) {
99 2
			return '/' . $filename;
100
		}
101 6
		return $filename;
102
	}
103
104
	/**
105
	 * Split the path from the filename string
106
	 *
107
	 * @param string $filename
108
	 * @return array Array with path and filename
109
	 */
110 6
	protected function splitPathFromFilename($filename) {
111 6
		if (strrpos($filename, '/') !== false) {
112
			return array(
113 5
				trim(substr($filename, 0, strrpos($filename, '/')), '/'),
114 5
				substr($filename, strrpos($filename, '/') + 1),
115 5
			);
116
		}
117 1
		return array('', $filename);
118
	}
119
}
120