UserScopeService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 39
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setUserScope() 0 11 3
A setFilesystemScope() 0 4 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Richdocuments\Service;
25
26
27
use OCP\IUserManager;
28
use OCP\IUserSession;
29
30
class UserScopeService {
31
32
	public function __construct(IUserSession $userSession, IUserManager $userManager) {
33
		$this->userSession = $userSession;
0 ignored issues
show
Bug introduced by
The property userSession 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...
34
		$this->userManager = $userManager;
0 ignored issues
show
Bug introduced by
The property userManager 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...
35
	}
36
37
	/**
38
	 * Set a valid user in IUserSession since lots of server logic is relying on obtaining
39
	 * the current acting user from that
40
	 *
41
	 * @param $uid
42
	 * @throws \InvalidArgumentException
43
	 */
44
	public function setUserScope(string $uid = null) {
45
		if ($uid === null) {
46
			return;
47
		}
48
49
		$user = $this->userManager->get($uid);
50
		if ($user === null) {
51
			throw new \InvalidArgumentException('No user found for the uid ' . $uid);
52
		}
53
		$this->userSession->setUser($user);
54
	}
55
56
	/**
57
	 * Setup the FS which is needed to emit hooks
58
	 *
59
	 * This is required for versioning/activity as the legacy filesystem hooks
60
	 * are not emitted if filesystem operations are executed though \OCP\Files\Node\File
61
	 *
62
	 * @param string $owner
63
	 */
64
	public function setFilesystemScope(string $owner) {
65
		\OC_Util::tearDownFS();
66
		\OC_Util::setupFS($owner);
67
	}
68
}
69