Completed
Push — master ( 0233b7...8a5b0a )
by Thomas
42s
created

AvatarManager::getAvatarFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
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
 * @author Vincent Petry <[email protected]>
10
 *
11
 * @copyright Copyright (c) 2018, ownCloud GmbH
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OC;
29
30
use OCP\Files\Folder;
31
use OCP\Files\NotFoundException;
32
use OCP\IAvatarManager;
33
use OCP\ILogger;
34
use OCP\IUserManager;
35
use OCP\Files\IRootFolder;
36
use OCP\IL10N;
37
use OCP\IUser;
38
39
/**
40
 * This class implements methods to access Avatar functionality
41
 */
42
class AvatarManager implements IAvatarManager {
43
44
	/** @var  IUserManager */
45
	private $userManager;
46
47
	/** @var  IRootFolder */
48
	private $rootFolder;
49
50
	/** @var IL10N */
51
	private $l;
52
53
	/** @var ILogger  */
54
	private $logger;
55
56
	/**
57
	 * AvatarManager constructor.
58
	 *
59
	 * @param IUserManager $userManager
60
	 * @param IRootFolder $rootFolder
61
	 * @param IL10N $l
62
	 * @param ILogger $logger
63
	 */
64
	public function __construct(
65
			IUserManager $userManager,
66
			IRootFolder $rootFolder,
67
			IL10N $l,
68
			ILogger $logger) {
69
		$this->userManager = $userManager;
70
		$this->rootFolder = $rootFolder;
71
		$this->l = $l;
72
		$this->logger = $logger;
73
	}
74
75
	/**
76
	 * return a user specific instance of \OCP\IAvatar
77
	 * @see \OCP\IAvatar
78
	 * @param string $userId the ownCloud user id
79
	 * @return \OCP\IAvatar
80
	 * @throws \Exception In case the username is potentially dangerous
81
	 * @throws NotFoundException In case there is no user folder yet
82
	 */
83
	public function getAvatar($userId) {
84
		$user = $this->userManager->get($userId);
85
		if ($user === null) {
86
			throw new \Exception('user does not exist');
87
		}
88
89
		$avatarsFolder = $this->getAvatarFolder($user);
90
		return new Avatar($avatarsFolder, $this->l, $user, $this->logger);
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...
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...
91
	}
92
93
	private function getFolder(Folder $folder, $path) {
94
		try {
95
			return $folder->get($path);
96
		} catch (NotFoundException $e) {
97
			return $folder->newFolder($path);
98
		}
99
	}
100
101
	private function buildAvatarPath($userId) {
102
		$avatar = \substr_replace(\substr_replace(\md5($userId), '/', 4, 0), '/', 2, 0);
103
		return \explode('/', $avatar);
104
	}
105
106
	/**
107
	 * Returns the avatar folder for the given user
108
	 *
109
	 * @param IUser $user user
110
	 * @return Folder|\OCP\Files\Node
111
	 *
112
	 * @internal
113
	 */
114
	public function getAvatarFolder(IUser $user) {
115
		$avatarsFolder = $this->getFolder($this->rootFolder, 'avatars');
116
		$parts = $this->buildAvatarPath($user->getUID());
117
		foreach ($parts as $part) {
118
			$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...
119
		}
120
		return $avatarsFolder;
121
	}
122
}
123