Completed
Push — master ( 81153d...2a6722 )
by Thomas
52s
created

File::fopen()   C

Complexity

Conditions 16
Paths 30

Size

Total Lines 34
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 30
nc 30
nop 1
dl 0
loc 34
rs 5.0151
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Robin Appelman <[email protected]>
6
 * @author Roeland Jago Douma <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 * @author Vincent Petry <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2018, ownCloud GmbH
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC\Files\Node;
28
29
use OCP\Files\IPreviewNode;
30
use OCP\Files\NotPermittedException;
31
use OCP\IImage;
32
33
class File extends Node implements \OCP\Files\File, IPreviewNode {
34
	/**
35
	 * Creates a Folder that represents a non-existing path
36
	 *
37
	 * @param string $path path
38
	 * @return string non-existing node class
39
	 */
40
	protected function createNonExistingNode($path) {
41
		return new NonExistingFile($this->root, $this->view, $path);
42
	}
43
44
	/**
45
	 * @return string
46
	 * @throws \OCP\Files\NotPermittedException
47
	 */
48
	public function getContent() {
49
		if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
50
			/**
51
			 * @var \OC\Files\Storage\Storage $storage;
52
			 */
53
			return $this->view->file_get_contents($this->path);
54
		} else {
55
			throw new NotPermittedException();
56
		}
57
	}
58
59
	/**
60
	 * @param string $data
61
	 * @throws \OCP\Files\NotPermittedException
62
	 */
63
	public function putContent($data) {
64
		if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
65
			$this->sendHooks(['preWrite']);
66
			$this->view->file_put_contents($this->path, $data);
67
			$this->fileInfo = null;
68
			$this->sendHooks(['postWrite']);
69
		} else {
70
			throw new NotPermittedException();
71
		}
72
	}
73
74
	/**
75
	 * @param string $mode
76
	 * @return resource
77
	 * @throws \OCP\Files\NotPermittedException
78
	 */
79
	public function fopen($mode) {
80
		$preHooks = [];
81
		$postHooks = [];
82
		$requiredPermissions = \OCP\Constants::PERMISSION_READ;
83
		switch ($mode) {
84
			case 'r+':
85
			case 'rb+':
86
			case 'w+':
87
			case 'wb+':
88
			case 'x+':
89
			case 'xb+':
90
			case 'a+':
91
			case 'ab+':
92
			case 'w':
93
			case 'wb':
94
			case 'x':
95
			case 'xb':
96
			case 'a':
97
			case 'ab':
98
				$preHooks[] = 'preWrite';
99
				$postHooks[] = 'postWrite';
100
				$requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE;
101
				break;
102
		}
103
104
		if ($this->checkPermissions($requiredPermissions)) {
105
			$this->sendHooks($preHooks);
106
			$result = $this->view->fopen($this->path, $mode);
107
			$this->sendHooks($postHooks);
108
			return $result;
109
		} else {
110
			throw new NotPermittedException();
111
		}
112
	}
113
114 View Code Duplication
	public function delete() {
115
		if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
116
			$this->sendHooks(['preDelete']);
117
			$fileInfo = $this->getFileInfo();
118
			$this->view->unlink($this->path);
119
			$nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
120
			$this->root->emit('\OC\Files', 'postDelete', [$nonExisting]);
121
			$this->exists = false;
0 ignored issues
show
Bug introduced by
The property exists 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...
122
			$this->fileInfo = null;
123
		} else {
124
			throw new NotPermittedException();
125
		}
126
	}
127
128
	/**
129
	 * @param string $type
130
	 * @param bool $raw
131
	 * @return string
132
	 */
133
	public function hash($type, $raw = false) {
134
		return $this->view->hash($type, $this->path, $raw);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->view->hash($type, $this->path, $raw); of type boolean|null|string adds the type boolean to the return on line 134 which is incompatible with the return type declared by the interface OCP\Files\File::hash of type string.
Loading history...
135
	}
136
137
	/**
138
	 * @inheritdoc
139
	 */
140
	public function getChecksum() {
141
		return $this->getFileInfo()->getChecksum();
142
	}
143
144
	/**
145
	 * @param array $options
146
	 * @return IImage
147
	 * @since 10.1.0
148
	 */
149 View Code Duplication
	public function getThumbnail($options) {
150
		$maxX = array_key_exists('x', $options) ? (int)$options['x'] : 32;
151
		$maxY = array_key_exists('y', $options) ? (int)$options['y'] : 32;
152
		$scalingUp = array_key_exists('scalingup', $options) ? (bool)$options['scalingup'] : true;
153
		$keepAspect = array_key_exists('a', $options) ? true : false;
154
		$mode = array_key_exists('mode', $options) ? $options['mode'] : 'fill';
155
156
		$preview = new \OC\Preview();
157
		$preview->setFile($this);
158
		$preview->setMaxX($maxX);
159
		$preview->setMaxY($maxY);
160
		$preview->setScalingUp($scalingUp);
161
		$preview->setMode($mode);
162
		$preview->setKeepAspect($keepAspect);
163
		return $preview->getPreview();
164
	}
165
}
166