Completed
Push — master ( 446472...7ef246 )
by Lukas
02:50
created

TokenManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 74
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B getToken() 0 39 5
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Richdocuments;
23
24
use OC\Share\Constants;
25
use OCA\Richdocuments\Db\Wopi;
26
use OCA\Richdocuments\WOPI\Parser;
27
use OCP\Files\File;
28
use OCP\Files\IRootFolder;
29
use OCP\IURLGenerator;
30
use OCP\Share\IManager;
31
32
class TokenManager {
33
	/** @var IRootFolder */
34
	private $rootFolder;
35
	/** @var IManager */
36
	private $shareManager;
37
	/** @var IURLGenerator */
38
	private $urlGenerator;
39
	/** @var Parser */
40
	private $wopiParser;
41
42
	/**
43
	 * @param IRootFolder $rootFolder
44
	 * @param IManager $shareManager
45
	 * @param IURLGenerator $urlGenerator
46
	 * @param string $UserId
47
	 */
48
	public function __construct(IRootFolder $rootFolder,
49
								IManager $shareManager,
50
								IURLGenerator $urlGenerator,
51
								Parser $wopiParser,
52
								$UserId) {
53
		$this->rootFolder = $rootFolder;
54
		$this->shareManager = $shareManager;
55
		$this->urlGenerator = $urlGenerator;
56
		$this->wopiParser = $wopiParser;
57
		$this->userId = $UserId;
0 ignored issues
show
Bug introduced by
The property userId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
	}
59
60
	/**
61
	 * @param string $fileId
62
	 * @param string $shareToken
63
	 * @return array
64
	 * @throws \Exception
65
	 */
66
	public function getToken($fileId, $shareToken = null) {
67
		$arr = explode('_', $fileId, 2);
68
		$version = '0';
69
		if (count($arr) === 2) {
70
			list($fileId, $version) = $arr;
71
		}
72
73
		// if the user is not logged-in do use the sharers storage
74
		if($shareToken !== null) {
75
			/** @var File $file */
76
			$rootFolder = $this->rootFolder;
77
			$share = $this->shareManager->getShareByToken($shareToken);
78
			$updatable = (bool)($share->getPermissions() & \OCP\Constants::PERMISSION_UPDATE);
79
		} else {
80
			try {
81
				/** @var File $file */
82
				$rootFolder = $this->rootFolder->getUserFolder($this->userId);
83
				$updatable = $rootFolder->isUpdateable();
84
			} catch (\Exception $e) {
85
				throw $e;
86
			}
87
		}
88
		/** @var File $file */
89
		$file = $rootFolder->getById($fileId)[0];
90
91
		$row = new Wopi();
92
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');//$this->request->getServerProtocol() . '://' . $this->request->getServerHost();
93
		$token = $row->generateFileToken($fileId, $file->getOwner()->getUID(), $this->userId, $version, $updatable, $serverHost);
94
95
		try {
96
97
			return [
98
				$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'],
99
				$token,
100
			];
101
		} catch (\Exception $e){
102
			throw $e;
103
		}
104
	}
105
}