Completed
Push — master ( c4929a...3bd4fb )
by Julius
19:34 queued 18:15
created

Helper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 75
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parseFileId() 0 27 5
A toISO8601() 0 9 2
A getNewFileName() 0 10 2
A getGuestName() 0 6 4
1
<?php
2
/**
3
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2013 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments;
13
14
use \DateTime;
15
use \DateTimeZone;
16
use OCP\Files\Folder;
17
18
class Helper {
19
20
	/** @var string|null */
21
	private $userId;
22
23
	public function __construct($userId) {
24
		$this->userId = $userId;
25
	}
26
27
	/**
28
	 * @param string $fileId
29
	 * @return array
30
	 * @throws \Exception
31
	 */
32
	public static function parseFileId($fileId) {
33
		$arr = explode('_', $fileId);
34
		$templateId = null;
35
		if (count($arr) === 1) {
36
			$fileId = $arr[0];
37
			$instanceId = '';
38
			$version = '0';
39
		} else if (count($arr) === 2) {
40
			list($fileId, $instanceId) = $arr;
41
			$version = '0';
42
		} else if (count($arr) === 3) {
43
			list($fileId, $instanceId, $version) = $arr;
44
		} else {
45
			throw new \Exception('$fileId has not the expected format');
46
		}
47
48
		if (strpos($fileId, '-') !== false) {
49
			list($fileId, $templateId) = explode('/', $fileId);
50
		}
51
52
		return [
53
			$fileId,
54
			$instanceId,
55
			$version,
56
			$templateId
57
		];
58
	}
59
60
	/**
61
	 * WOPI helper function to convert to ISO 8601 round-trip format.
62
	 * @param integer $time Must be seconds since unix epoch
63
	 */
64
	public static function toISO8601($time)
65
	{
66
		// TODO: Be more precise and don't ignore milli, micro seconds ?
67
		$datetime = DateTime::createFromFormat('U', $time, new DateTimeZone('UTC'));
68
		if ($datetime)
69
			return $datetime->format('Y-m-d\TH:i:s.u\Z');
70
71
		return false;
72
	}
73
74
	public static function getNewFileName(Folder $folder, $filename) {
75
		$fileNum = 1;
76
77
		while ($folder->nodeExists($filename)) {
78
			$fileNum++;
79
			$filename = preg_replace('/(\.| \(\d+\)\.)([^.]*)$/', ' (' . $fileNum . ').$2', $filename);
80
		}
81
82
		return $filename;
83
	}
84
85
	public function getGuestName() {
86
		if ($this->userId !== null || !isset($_COOKIE['guestUser']) || $_COOKIE['guestUser'] === '') {
87
			return null;
88
		}
89
		return $_COOKIE['guestUser'];
90
	}
91
92
}
93