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

FileSharing::getPanel()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 87
rs 8.2836
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Tom Needham <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Settings\Panels\Admin;
23
24
use OC\Helper\LocaleHelper;
25
use OC\Settings\Panels\Helper;
26
use OCP\IConfig;
27
use OCP\Settings\ISettings;
28
use OCP\Template;
29
use OCP\IL10N;
30
31
class FileSharing implements ISettings {
32
33
	/** @var IConfig */
34
	protected $config;
35
	/** @var Helper */
36
	protected $helper;
37
	/** @var IL10N */
38
	protected $l;
39
40
	public function __construct(IConfig $config, Helper $helper, IL10N $l) {
41
		$this->config = $config;
42
		$this->helper = $helper;
43
		$this->l = $l;
44
	}
45
46
	public function getPriority() {
47
		return 99;
48
	}
49
50
	public function getPanel() {
51
		$this->lfactory = \OC::$server->getL10NFactory();
0 ignored issues
show
Bug introduced by
The property lfactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
		$activeLangCode = $this->config->getAppValue(
53
			'core',
54
			'shareapi_public_notification_lang',
55
			'owner'
56
		);
57
		$this->localeHelper = new LocaleHelper();
0 ignored issues
show
Bug introduced by
The property localeHelper does not seem to exist. Did you mean helper?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
58
		list($userLang, $commonLanguages, $languages) = $this->localeHelper->getNormalizedLanguages(
0 ignored issues
show
Bug introduced by
The property localeHelper does not seem to exist. Did you mean helper?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
59
			$this->lfactory,
60
			$activeLangCode
61
		);
62
63
		// Allow reset to the defaults when mail notification is sent in the lang of owner
64
		if ($userLang['code']  === "owner") {
65
			$userLang['name'] = $this->l->t("Owner language");
66
		} else {
67
			\array_push(
68
				$commonLanguages,
69
				[
70
					'code' => 'owner',
71
					'name' => $this->l->t("Owner language")
72
				]
73
			);
74
		}
75
76
		$selector = new Template('settings', 'language');
77
		$selector->assign('selectName', 'shareapi_public_notification_lang');
78
		$selector->assign('selectId', 'shareapiPublicNotificationLang');
79
		$selector->assign('activelanguage', $userLang);
80
		$selector->assign('commonlanguages', $commonLanguages);
81
		$selector->assign('languages', $languages);
82
83
		$template = new Template('settings', 'panels/admin/filesharing');
84
		$template->assign('allowResharing', $this->config->getAppValue('core', 'shareapi_allow_resharing', 'yes'));
85
		$template->assign('shareAPIEnabled', $this->config->getAppValue('core', 'shareapi_enabled', 'yes'));
86
		$template->assign('allowLinks', $this->config->getAppValue('core', 'shareapi_allow_links', 'yes'));
87
		$template->assign('allowPublicUpload', $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes'));
88
		$template->assign('enforceLinkPasswordReadOnly', $this->config->getAppValue('core', 'shareapi_enforce_links_password_read_only', 'no'));
89
		$template->assign('enforceLinkPasswordReadWrite', $this->config->getAppValue('core', 'shareapi_enforce_links_password_read_write', 'no'));
90
		$template->assign('enforceLinkPasswordWriteOnly', $this->config->getAppValue('core', 'shareapi_enforce_links_password_write_only', 'no'));
91
		$template->assign('shareDefaultExpireDateSet', $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no'));
92
		$template->assign('allowPublicMailNotification', $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no'));
93
		$template->assign('publicMailNotificationLang', $selector->fetchPage());
94
		$template->assign('allowSocialShare', $this->config->getAppValue('core', 'shareapi_allow_social_share', 'yes'));
95
		$template->assign('allowGroupSharing', $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes'));
96
		$template->assign('onlyShareWithGroupMembers', $this->helper->shareWithGroupMembersOnly());
97
		$template->assign('onlyShareWithMembershipGroups', $this->config->getAppValue('core', 'shareapi_only_share_with_membership_groups', 'no') === 'yes');
98
		$template->assign('allowMailNotification', $this->config->getAppValue('core', 'shareapi_allow_mail_notification', 'no'));
99
		$template->assign('allowShareDialogUserEnumeration', $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'));
100
		$template->assign('shareDialogUserEnumerationGroupMembers', $this->config->getAppValue('core', 'shareapi_share_dialog_user_enumeration_group_members', 'no'));
101
		$excludeGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes' ? true : false;
102
		$template->assign('shareExcludeGroups', $excludeGroups);
103
		$excludedGroupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
104
		$excludedGroupsList = \json_decode($excludedGroupsList);
105
		$template->assign('shareExcludedGroupsList', $excludedGroupsList !== null ? \implode('|', $excludedGroupsList) : '');
106
		$template->assign('shareExpireAfterNDays', $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'));
107
		$template->assign('shareEnforceExpireDate', $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no'));
108
		$template->assign('autoAcceptShare', $this->config->getAppValue('core', 'shareapi_auto_accept_share', 'yes'));
109
110
		$permList = [
111
			[
112
				'id' => 'cancreate',
113
				'label' => $this->l->t('Create'),
114
				'value' => \OCP\Constants::PERMISSION_CREATE
115
			],
116
			[
117
				'id' => 'canupdate',
118
				'label' => $this->l->t('Change'),
119
				'value' => \OCP\Constants::PERMISSION_UPDATE
120
			],
121
			[
122
				'id' => 'candelete',
123
				'label' => $this->l->t('Delete'),
124
				'value' => \OCP\Constants::PERMISSION_DELETE
125
			],
126
			[
127
				'id' => 'canshare',
128
				'label' => $this->l->t('Share'),
129
				'value' => \OCP\Constants::PERMISSION_SHARE
130
			],
131
		];
132
		$template->assign('shareApiDefaultPermissions', $this->config->getAppValue('core', 'shareapi_default_permissions', \OCP\Constants::PERMISSION_ALL));
133
		$template->assign('shareApiDefaultPermissionsCheckboxes', $permList);
134
		$template->assign('coreUserAdditionalInfo', $this->config->getAppValue('core', 'user_additional_info_field', ''));
135
		return $template;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $template; (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...
136
	}
137
138
	public function getSectionID() {
139
		return 'sharing';
140
	}
141
}
142