Completed
Push — master ( 7f194b...f212c6 )
by Morris
18:03 queued 13s
created

DirectFile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright 2018, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\DAV\Direct;
26
27
use OCA\DAV\Db\Direct;
28
use OCP\Files\File;
29
use OCP\Files\IRootFolder;
30
use Sabre\DAV\Exception\Forbidden;
31
use Sabre\DAV\Exception\NotFound;
32
use Sabre\DAV\IFile;
33
34
class DirectFile implements IFile {
35
	/** @var Direct */
36
	private $direct;
37
38
	/** @var IRootFolder */
39
	private $rootFolder;
40
41
	/** @var File */
42
	private $file;
43
44
	public function __construct(Direct $direct, IRootFolder $rootFolder) {
45
		$this->direct = $direct;
46
		$this->rootFolder = $rootFolder;
47
	}
48
49
	public function put($data) {
50
		throw new Forbidden();
51
	}
52
53
	public function get() {
54
		$this->getFile();
55
56
		return $this->file->fopen('rb');
57
	}
58
59
	public function getContentType() {
60
		$this->getFile();
61
62
		return $this->file->getMimeType();
63
	}
64
65
	public function getETag() {
66
		$this->getFile();
67
68
		return $this->file->getEtag();
69
	}
70
71
	public function getSize() {
72
		$this->getFile();
73
74
		return $this->file->getSize();
75
	}
76
77
	public function delete() {
78
		throw new Forbidden();
79
	}
80
81
	public function getName() {
82
		return $this->direct->getToken();
83
	}
84
85
	public function setName($name) {
86
		throw new Forbidden();
87
	}
88
89
	public function getLastModified() {
90
		$this->getFile();
91
92
		return $this->file->getMTime();
93
	}
94
95
	private function getFile() {
96
		if ($this->file === null) {
97
			$userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId());
98
			$files = $userFolder->getById($this->direct->getFileId());
99
100
			if ($files === []) {
101
				throw new NotFound();
102
			}
103
104
			$this->file = array_shift($files);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_shift($files) can also be of type object<OCP\Files\Node>. However, the property $file is declared as type object<OCP\Files\File>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
105
		}
106
107
		return $this->file;
108
	}
109
110
}
111