Completed
Push — master ( 33d925...cc7944 )
by Thomas
09:52
created

Admin::getPanel()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace OCA\Encryption\Panels;
4
5
use OCP\IL10N;
6
use OCP\Settings\ISettings;
7
use OCP\Template;
8
use OCA\Encryption\Crypto\Crypt;
9
use OCA\Encryption\Util;
10
use OC\Files\View;
11
use OCP\IConfig;
12
use OCA\Encryption\Session;
13
use OCP\ILogger;
14
use OCP\IUserManager;
15
use OCP\ISession;
16
use OCP\IUserSession;
17
18
class Admin implements ISettings {
19
20
	/** @var IConfig */
21
	protected $config;
22
	/** @var ILogger */
23
	protected $logger;
24
	/** @var IUserSession */
25
	protected $userSession;
26
	/** @var IUserManager */
27
	protected $userManager;
28
	/** @var ISession */
29
	protected $session;
30
	/** @var IL10N */
31
	protected $l;
32
33 View Code Duplication
	public function __construct(IConfig $config,
34
								ILogger $logger,
35
								IUserSession $userSession,
36
								IUserManager $userManager,
37
								ISession $session,
38
								IL10N $l) {
39
		$this->config = $config;
40
		$this->logger = $logger;
41
		$this->userSession = $userSession;
42
		$this->userManager = $userManager;
43
		$this->session = $session;
44
		$this->l = $l;
45
	}
46
47
	public function getPriority() {
48
		return 0;
49
	}
50
51
	public function getSectionID() {
52
		return 'encryption';
53
	}
54
55
	public function getPanel() {
56
		$tmpl = new Template('encryption', 'settings-admin');
57
		$crypt = new Crypt(
58
			$this->logger,
59
			$this->userSession,
60
			$this->config,
61
			$this->l);
62
		$util = new Util(
63
			new View(),
64
			$crypt,
65
			$this->logger,
66
			$this->userSession,
67
			$this->config,
68
			$this->userManager
69
		);
70
		// Check if an adminRecovery account is enabled for recovering files after lost pwd
71
		$recoveryAdminEnabled = $this->config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
72
		$session = new Session($this->session);
73
		$encryptHomeStorage = $util->shouldEncryptHomeStorage();
74
		$tmpl->assign('recoveryEnabled', $recoveryAdminEnabled);
75
		$tmpl->assign('initStatus', $session->getStatus());
76
		$tmpl->assign('encryptHomeStorage', $encryptHomeStorage);
77
		$tmpl->assign('masterKeyEnabled', $util->isMasterKeyEnabled());
78
		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...
79
	}
80
}