Completed
Push — master ( c06f9d...14931f )
by Thomas
36:10
created

AvatarManager::getAvatarFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2016, 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;
28
29
use OCP\Files\Folder;
30
use OCP\Files\NotFoundException;
31
use OCP\IAvatarManager;
32
use OCP\ILogger;
33
use OCP\IUserManager;
34
use OCP\Files\IRootFolder;
35
use OCP\IL10N;
36
37
/**
38
 * This class implements methods to access Avatar functionality
39
 */
40
class AvatarManager implements IAvatarManager {
41
42
	/** @var  IUserManager */
43
	private $userManager;
44
45
	/** @var  IRootFolder */
46
	private $rootFolder;
47
48
	/** @var IL10N */
49
	private $l;
50
51
	/** @var ILogger  */
52
	private $logger;
53
54
	/**
55
	 * AvatarManager constructor.
56
	 *
57
	 * @param IUserManager $userManager
58
	 * @param IRootFolder $rootFolder
59
	 * @param IL10N $l
60
	 * @param ILogger $logger
61
	 */
62
	public function __construct(
63
			IUserManager $userManager,
64
			IRootFolder $rootFolder,
65
			IL10N $l,
66
			ILogger $logger) {
67
		$this->userManager = $userManager;
68
		$this->rootFolder = $rootFolder;
69
		$this->l = $l;
70
		$this->logger = $logger;
71
	}
72
73
	/**
74
	 * return a user specific instance of \OCP\IAvatar
75
	 * @see \OCP\IAvatar
76
	 * @param string $userId the ownCloud user id
77
	 * @return \OCP\IAvatar
78
	 * @throws \Exception In case the username is potentially dangerous
79
	 * @throws NotFoundException In case there is no user folder yet
80
	 */
81
	public function getAvatar($userId) {
82
		$user = $this->userManager->get($userId);
83
		if (is_null($user)) {
84
			throw new \Exception('user does not exist');
85
		}
86
87
		$avatarsFolder = $this->getAvatarFolder($userId);
88
		return new Avatar($avatarsFolder, $this->l, $user, $this->logger);
0 ignored issues
show
Compatibility introduced by
$user of type object<OCP\IUser> is not a sub-type of object<OC\User\User>. It seems like you assume a concrete implementation of the interface OCP\IUser to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$avatarsFolder of type object<OCP\Files\Node> is not a sub-type of object<OCP\Files\Folder>. It seems like you assume a child interface of the interface OCP\Files\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
89
	}
90
91
	private function getFolder(Folder $folder, $path) {
92
		try {
93
			return $folder->get($path);
94
		} catch (NotFoundException $e) {
95
			return $folder->newFolder($path);
96
		}
97
	}
98
99
	private function buildAvatarPath($userId) {
100
		$avatar = substr_replace(substr_replace(md5($userId), '/', 4, 0), '/', 2, 0);
101
		return explode('/', $avatar);
102
	}
103
104
	/**
105
	 * Returns the avatar folder for the given user
106
	 *
107
	 * @param $userId user id
108
	 * @return Folder|\OCP\Files\Node
109
	 *
110
	 * @internal
111
	 */
112
	public function getAvatarFolder($userId) {
113
		$avatarsFolder = $this->getFolder($this->rootFolder, 'avatars');
114
		$parts = $this->buildAvatarPath($userId);
115
		foreach ($parts as $part) {
116
			$avatarsFolder = $this->getFolder($avatarsFolder, $part);
0 ignored issues
show
Compatibility introduced by
$avatarsFolder of type object<OCP\Files\Node> is not a sub-type of object<OCP\Files\Folder>. It seems like you assume a child interface of the interface OCP\Files\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
117
		}
118
		return $avatarsFolder;
119
	}
120
}
121