Completed
Push — master ( 8a6036...5a9909 )
by
unknown
11:22
created

Profile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Tom Needham <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Robin Appelman <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2018, ownCloud GmbH
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Settings\Panels\Personal;
26
27
use OC\Helper\LocaleHelper;
28
use OCP\Settings\ISettings;
29
use OCP\Template;
30
use OCP\IGroupManager;
31
use OCP\IUserSession;
32
use OCP\IConfig;
33
use OCP\L10N\IFactory;
34
35
class Profile implements ISettings {
36
37
	/* @var IConfig */
38
	protected $config;
39
	/* @var IGroupManager */
40
	protected $groupManager;
41
	/* @var IUserSession */
42
	protected $userSession;
43
	/** @var IFactory */
44
	protected $lfactory;
45
46
	/**
47
	 * @var LocaleHelper
48
	 */
49
	private $localeHelper;
50
51
	/**
52
	 * Profile constructor.
53
	 *
54
	 * @param IConfig $config
55
	 * @param IGroupManager $groupManager
56
	 * @param IUserSession $userSession
57
	 * @param IFactory $lfactory
58
	 * @param LocaleHelper $localeHelper
59
	 */
60
	public function __construct(IConfig $config,
61
								   IGroupManager $groupManager,
62
								   IUserSession $userSession,
63
								   IFactory $lfactory,
64
								   LocaleHelper $localeHelper
65
	) {
66
		$this->config = $config;
67
		$this->groupManager = $groupManager;
68
		$this->userSession = $userSession;
69
		$this->lfactory = $lfactory;
70
		$this->localeHelper = $localeHelper;
71
	}
72
73
	public function getPriority() {
74
		return 100;
75
	}
76
77
	public function getPanel() {
78
		$activeLangCode = $this->config->getUserValue(
79
			$this->userSession->getUser()->getUID(),
80
			'core',
81
			'lang',
82
			$this->lfactory->findLanguage()
83
		);
84
85
		list($userLang, $commonLanguages, $languages) = $this->localeHelper->getNormalizedLanguages(
86
			$this->lfactory,
87
			$activeLangCode
88
		);
89
90
		$selector = new Template('settings', 'language');
91
		$selector->assign('selectName', 'lang');
92
		$selector->assign('selectId', 'languageinput');
93
		$selector->assign('activelanguage', $userLang);
94
		$selector->assign('commonlanguages', $commonLanguages);
95
		$selector->assign('languages', $languages);
96
97
		$tmpl = new Template('settings', 'panels/personal/profile');
98
		$tmpl->assign('email', $this->userSession->getUser()->getEMailAddress());
99
		$tmpl->assign('displayName', $this->userSession->getUser()->getDisplayName());
100
		$tmpl->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true);
101
		$tmpl->assign('avatarChangeSupported', $this->userSession->getUser()->canChangeAvatar());
102
		$tmpl->assign('displayNameChangeSupported', $this->userSession->getUser()->canChangeDisplayName());
103
		$tmpl->assign('passwordChangeSupported', $this->userSession->getUser()->canChangePassword());
104
		$groups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
0 ignored issues
show
Bug introduced by
It seems like $this->userSession->getUser() can be null; however, getUserGroupIds() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
105
		\sort($groups);
106
		$tmpl->assign('groups', $groups);
107
		$tmpl->assign('languageSelector', $selector->fetchPage());
108
		return $tmpl;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $tmpl; (OCP\Template) is incompatible with the return type declared by the interface OCP\Settings\ISettings::getPanel of type OCP\AppFramework\Http\TemplateResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
109
	}
110
111
	public function getSectionID() {
112
		return 'general';
113
	}
114
}
115