Completed
Pull Request — master (#521)
by
unknown
20:34
created

FileFormatter::format()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.25

Importance

Changes 7
Bugs 0 Features 2
Metric Value
c 7
b 0
f 2
dl 0
loc 23
ccs 12
cts 16
cp 0.75
rs 8.7972
cc 4
eloc 16
nc 3
nop 2
crap 4.25
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 = '';
0 ignored issues
show
Unused Code introduced by
$fileId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64 11
		if (is_array($param)) {
65
			$fileId = key($param);
66
			$param = $param[$fileId];
67
			$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
0 ignored issues
show
Unused Code introduced by
$info is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68 11
		} elseif ($event->getObjectType() === 'files' && $event->getObjectName() === $param) {
69 2
			$fileId = $event->getObjectId();
70 2
			$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
0 ignored issues
show
Unused Code introduced by
$info is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71 2
		} else {
72 9
			$info = $this->infoCache->getInfoByPath($this->user, $param);
73 9
			$fileId = $info['fileid'];
74
		}
75
76 2
		$param = trim($param, '/');
77 2
		$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileId' => $fileId]);
78
79
		return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>';
80
	}
81
82
	/**
83
	 * Prepend leading slash to filenames of legacy activities
84
	 * @param string|array $filename
85
	 * @return string|array
86
	 */
87 10
	protected function fixLegacyFilename($filename) {
88 10
		if (is_array($filename)) {
89
			// 9.0: [fileId => path]
90 2
			return $filename;
91
		}
92 8
		if (strpos($filename, '/') !== 0) {
93 2
			return '/' . $filename;
94
		}
95 6
		return $filename;
96
	}
97
98
	/**
99
	 * Split the path from the filename string
100
	 *
101
	 * @param string $filename
102
	 * @return array Array with path and filename
103
	 */
104 6
	protected function splitPathFromFilename($filename) {
105 6
		if (strrpos($filename, '/') !== false) {
106
			return array(
107 5
				trim(substr($filename, 0, strrpos($filename, '/')), '/'),
108 5
				substr($filename, strrpos($filename, '/') + 1),
109 5
			);
110
		}
111 1
		return array('', $filename);
112
	}
113
}
114