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

Personal::getPanel()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 33
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 27
nc 2
nop 0
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace OCA\Encryption\Panels;
4
5
use OCP\Encryption\Keys\IStorage;
6
use OCP\IConfig;
7
use OCP\IL10N;
8
use OCP\ILogger;
9
use OCP\ISession;
10
use OCP\IUserManager;
11
use OCP\IUserSession;
12
use OCP\Settings\ISettings;
13
use OCP\Template;
14
15
class Personal implements ISettings {
16
17
	/** @var ILogger */
18
	protected $logger;
19
	/** @var IUserSession */
20
	protected $userSession;
21
	/** @var IConfig */
22
	protected $config;
23
	/** @var IL10N */
24
	protected $l;
25
	/** @varIUserManager */
26
	protected $userManager;
27
	/** @var ISession  */
28
	protected $session;
29
	/** @var IStorage */
30
	protected $encKeyStorage;
31
32
	public function __construct(
33
		ILogger $logger,
34
		IUserSession $userSession,
35
		IConfig $config,
36
		IL10N $l,
37
		IUserManager $userManager,
38
		ISession $session,
39
		IStorage $encKeyStorage) {
40
		$this->logger = $logger;
41
		$this->userSession = $userSession;
42
		$this->config = $config;
43
		$this->l = $l;
44
		$this->userManager = $userManager;
45
		$this->session = $session;
46
		$this->encKeyStorage = $encKeyStorage;
47
	}
48
49
	public function getPriority() {
50
		return 0;
51
	}
52
53
	public function getSectionID() {
54
		return 'encryption';
55
	}
56
57
	public function getPanel() {
58
		$session = new \OCA\Encryption\Session($this->session);
59
		$template = new Template('encryption', 'settings-personal');
60
		$crypt = new \OCA\Encryption\Crypto\Crypt(
61
			$this->logger,
62
			$this->userSession,
63
			$this->config,
64
			$this->l);
65
66
		$util = new \OCA\Encryption\Util(
67
			new \OC\Files\View(),
68
			$crypt,
69
			$this->logger,
70
			$this->userSession,
71
			$this->config,
72
			$this->userManager);
73
74
		$user = $this->userSession->getUser()->getUID();
75
		$privateKeySet = $session->isPrivateKeySet();
76
77
		// did we tried to initialize the keys for this session?
78
		$initialized = $session->getStatus();
79
		$recoveryAdminEnabled = $this->config->getAppValue('encryption', 'recoveryAdminEnabled');
80
		$recoveryEnabledForUser = $util->isRecoveryEnabledForUser($user);
81
		if ($recoveryAdminEnabled || !$privateKeySet) {
82
			$template->assign('recoveryEnabled', $recoveryAdminEnabled);
83
			$template->assign('recoveryEnabledForUser', $recoveryEnabledForUser);
84
			$template->assign('privateKeySet', $privateKeySet);
85
			$template->assign('initialized', $initialized);
86
			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...
87
		}
88
		return null;
89
	}
90
}