Completed
Push — master ( ff3e8c...ea9b1c )
by Lukas
13:22 queued 02:44
created

AvatarManager::getAvatar()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 9.4285
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 *
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\IAppData;
31
use OCP\Files\NotFoundException;
32
use OCP\IAvatarManager;
33
use OCP\IConfig;
34
use OCP\ILogger;
35
use OCP\IUserManager;
36
use OCP\IL10N;
37
38
/**
39
 * This class implements methods to access Avatar functionality
40
 */
41
class AvatarManager implements IAvatarManager {
42
43
	/** @var  IUserManager */
44
	private $userManager;
45
46
	/** @var IAppData */
47
	private $appData;
48
49
	/** @var IL10N */
50
	private $l;
51
52
	/** @var ILogger  */
53
	private $logger;
54
55
	/** @var IConfig */
56
	private $config;
57
58
	/**
59
	 * AvatarManager constructor.
60
	 *
61
	 * @param IUserManager $userManager
62
	 * @param IAppData $appData
63
	 * @param IL10N $l
64
	 * @param ILogger $logger
65
	 * @param IConfig $config
66
	 */
67 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
			IUserManager $userManager,
69
			IAppData $appData,
70
			IL10N $l,
71
			ILogger $logger,
72
			IConfig $config) {
73
		$this->userManager = $userManager;
74
		$this->appData = $appData;
75
		$this->l = $l;
76
		$this->logger = $logger;
77
		$this->config = $config;
78
	}
79
80
	/**
81
	 * return a user specific instance of \OCP\IAvatar
82
	 * @see \OCP\IAvatar
83
	 * @param string $userId the ownCloud user id
84
	 * @return \OCP\IAvatar
85
	 * @throws \Exception In case the username is potentially dangerous
86
	 * @throws NotFoundException In case there is no user folder yet
87
	 */
88
	public function getAvatar($userId) {
89
		$user = $this->userManager->get($userId);
90
		if (is_null($user)) {
91
			throw new \Exception('user does not exist');
92
		}
93
94
		// sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below)
95
		$userId = $user->getUID();
96
97
		try {
98
			$folder = $this->appData->getFolder($userId);
99
		} catch (NotFoundException $e) {
100
			$folder = $this->appData->newFolder($userId);
101
		}
102
103
		return new Avatar($folder, $this->l, $user, $this->logger, $this->config);
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...
104
	}
105
}
106