Passed
Push — master ( 456412...602de2 )
by Joas
13:17 queued 10s
created

AvatarManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 18
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 * @author Christoph Wurst <[email protected]>
10
 * @author John Molakvoæ (skjnldsv) <[email protected]>
11
 * @author Julius Härtl <[email protected]>
12
 * @author Lukas Reschke <[email protected]>
13
 * @author Michael Weimann <[email protected]>
14
 * @author Morris Jobke <[email protected]>
15
 * @author Robin Appelman <[email protected]>
16
 * @author Roeland Jago Douma <[email protected]>
17
 * @author Thomas Müller <[email protected]>
18
 *
19
 * @license AGPL-3.0
20
 *
21
 * This code is free software: you can redistribute it and/or modify
22
 * it under the terms of the GNU Affero General Public License, version 3,
23
 * as published by the Free Software Foundation.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28
 * GNU Affero General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU Affero General Public License, version 3,
31
 * along with this program. If not, see <http://www.gnu.org/licenses/>
32
 *
33
 */
34
35
namespace OC\Avatar;
36
37
use OC\KnownUser\KnownUserService;
38
use OC\User\Manager;
39
use OC\User\NoUserException;
40
use OCP\Accounts\IAccountManager;
41
use OCP\Files\IAppData;
42
use OCP\Files\NotFoundException;
43
use OCP\Files\NotPermittedException;
44
use OCP\IAvatar;
45
use OCP\IAvatarManager;
46
use OCP\IConfig;
47
use OCP\IL10N;
48
use OCP\ILogger;
49
use OCP\IUserSession;
50
51
/**
52
 * This class implements methods to access Avatar functionality
53
 */
54
class AvatarManager implements IAvatarManager {
55
56
	/** @var IUserSession */
57
	private $userSession;
58
59
	/** @var Manager */
60
	private $userManager;
61
62
	/** @var IAppData */
63
	private $appData;
64
65
	/** @var IL10N */
66
	private $l;
67
68
	/** @var ILogger  */
69
	private $logger;
70
71
	/** @var IConfig */
72
	private $config;
73
74
	/** @var IAccountManager */
75
	private $accountManager;
76
77
	/** @var KnownUserService */
78
	private $knownUserService;
79
80
	/**
81
	 * AvatarManager constructor.
82
	 *
83
	 * @param Manager $userManager
84
	 * @param IAppData $appData
85
	 * @param IL10N $l
86
	 * @param ILogger $logger
87
	 * @param IConfig $config
88
	 * @param IUserSession $userSession
89
	 */
90
	public function __construct(
91
			IUserSession $userSession,
92
			Manager $userManager,
93
			IAppData $appData,
94
			IL10N $l,
95
			ILogger $logger,
96
			IConfig $config,
97
			IAccountManager $accountManager,
98
			KnownUserService $knownUserService
99
	) {
100
		$this->userSession = $userSession;
101
		$this->userManager = $userManager;
102
		$this->appData = $appData;
103
		$this->l = $l;
104
		$this->logger = $logger;
105
		$this->config = $config;
106
		$this->accountManager = $accountManager;
107
		$this->knownUserService = $knownUserService;
108
	}
109
110
	/**
111
	 * return a user specific instance of \OCP\IAvatar
112
	 * @see \OCP\IAvatar
113
	 * @param string $userId the ownCloud user id
114
	 * @return \OCP\IAvatar
115
	 * @throws \Exception In case the username is potentially dangerous
116
	 * @throws NotFoundException In case there is no user folder yet
117
	 */
118
	public function getAvatar(string $userId) : IAvatar {
119
		$user = $this->userManager->get($userId);
120
		if ($user === null) {
121
			throw new \Exception('user does not exist');
122
		}
123
124
		// sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below)
125
		$userId = $user->getUID();
126
127
		$requestingUser = null;
128
		if ($this->userSession !== null) {
129
			$requestingUser = $this->userSession->getUser();
130
		}
131
132
		try {
133
			$folder = $this->appData->getFolder($userId);
134
		} catch (NotFoundException $e) {
135
			$folder = $this->appData->newFolder($userId);
136
		}
137
138
		$account = $this->accountManager->getAccount($user);
139
		$avatarProperties = $account->getProperty(IAccountManager::PROPERTY_AVATAR);
140
		$avatarScope = $avatarProperties->getScope();
141
142
		if (
143
			// v2-private scope hides the avatar from public access and from unknown users
144
			$avatarScope === IAccountManager::SCOPE_PRIVATE
145
			&& (
146
				// accessing from public link
147
				$requestingUser === null
148
				// logged in, but unknown to user
149
				|| !$this->knownUserService->isKnownToUser($requestingUser->getUID(), $userId)
150
			)) {
151
			// use a placeholder avatar which caches the generated images
152
			return new PlaceholderAvatar($folder, $user, $this->logger);
153
		}
154
155
		return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
156
	}
157
158
	/**
159
	 * Clear generated avatars
160
	 */
161
	public function clearCachedAvatars() {
162
		$users = $this->config->getUsersForUserValue('avatar', 'generated', 'true');
163
		foreach ($users as $userId) {
164
			try {
165
				$folder = $this->appData->getFolder($userId);
166
				$folder->delete();
167
			} catch (NotFoundException $e) {
168
				$this->logger->debug("No cache for the user $userId. Ignoring...");
169
			}
170
			$this->config->setUserValue($userId, 'avatar', 'generated', 'false');
171
		}
172
	}
173
174
	public function deleteUserAvatar(string $userId): void {
175
		try {
176
			$folder = $this->appData->getFolder($userId);
177
			$folder->delete();
178
		} catch (NotFoundException $e) {
179
			$this->logger->debug("No cache for the user $userId. Ignoring avatar deletion");
180
		} catch (NotPermittedException $e) {
181
			$this->logger->error("Unable to delete user avatars for $userId. gnoring avatar deletion");
182
		} catch (NoUserException $e) {
183
			$this->logger->debug("User $userId not found. gnoring avatar deletion");
184
		}
185
		$this->config->deleteUserValue($userId, 'avatar', 'generated');
186
	}
187
188
	/**
189
	 * Returns a GuestAvatar.
190
	 *
191
	 * @param string $name The guest name, e.g. "Albert".
192
	 * @return IAvatar
193
	 */
194
	public function getGuestAvatar(string $name): IAvatar {
195
		return new GuestAvatar($name, $this->logger);
196
	}
197
}
198