Completed
Push — master ( 626d03...c1030a )
by Morris
27:45 queued 15:55
created

AvatarHome::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
23
namespace OCA\DAV\Avatars;
24
25
26
use OCP\IAvatarManager;
27
use Sabre\DAV\Exception\Forbidden;
28
use Sabre\DAV\Exception\MethodNotAllowed;
29
use Sabre\DAV\Exception\NotFound;
30
use Sabre\DAV\ICollection;
31
use Sabre\Uri;
32
33
class AvatarHome implements ICollection {
34
35
	/** @var array */
36
	private $principalInfo;
37
	/** @var IAvatarManager */
38
	private $avatarManager;
39
40
	/**
41
	 * AvatarHome constructor.
42
	 *
43
	 * @param array $principalInfo
44
	 * @param IAvatarManager $avatarManager
45
	 */
46
	public function __construct($principalInfo, IAvatarManager $avatarManager) {
47
		$this->principalInfo = $principalInfo;
48
		$this->avatarManager = $avatarManager;
49
	}
50
51
	public function createFile($name, $data = null) {
52
		throw new Forbidden('Permission denied to create a file');
53
	}
54
55
	public function createDirectory($name) {
56
		throw new Forbidden('Permission denied to create a folder');
57
	}
58
59
	public function getChild($name) {
60
		$elements = pathinfo($name);
61
		$ext = isset($elements['extension']) ? $elements['extension'] : '';
62
		$size = (int)(isset($elements['filename']) ? $elements['filename'] : '64');
63
		if (!in_array($ext, ['jpeg', 'png'], true)) {
64
			throw new MethodNotAllowed('File format not allowed');
65
		}
66
		if ($size <= 0 || $size > 1024) {
67
			throw new MethodNotAllowed('Invalid image size');
68
		}
69
		$avatar = $this->avatarManager->getAvatar($this->getName());
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
70
		if ($avatar === null || !$avatar->exists()) {
71
			throw new NotFound();
72
		}
73
		return new AvatarNode($size, $ext, $avatar);
74
	}
75
76
	public function getChildren() {
77
		try {
78
			return [
79
				$this->getChild('96.jpeg')
80
			];
81
		} catch(NotFound $exception) {
0 ignored issues
show
Bug introduced by
The class Sabre\DAV\Exception\NotFound does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
82
			return [];
83
		}
84
	}
85
86
	public function childExists($name) {
87
		try {
88
			$ret = $this->getChild($name);
89
			return $ret !== null;
90
		} catch (NotFound $ex) {
0 ignored issues
show
Bug introduced by
The class Sabre\DAV\Exception\NotFound does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
91
			return false;
92
		} catch (MethodNotAllowed $ex) {
0 ignored issues
show
Bug introduced by
The class Sabre\DAV\Exception\MethodNotAllowed does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
93
			return false;
94
		}
95
	}
96
97
	public function delete() {
98
		throw new Forbidden('Permission denied to delete this folder');
99
	}
100
101
	public function getName() {
102
		list(,$name) = Uri\split($this->principalInfo['uri']);
103
		return $name;
104
	}
105
106
	public function setName($name) {
107
		throw new Forbidden('Permission denied to rename this folder');
108
	}
109
110
	/**
111
	 * Returns the last modification time, as a unix timestamp
112
	 *
113
	 * @return int|null
114
	 */
115
	public function getLastModified() {
116
		return null;
117
	}
118
119
120
}
121