Passed
Push — master ( e56f7d...dff054 )
by Jean-Christophe
20:36
created

AuthControllerOverrideTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 40%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 10
eloc 12
c 2
b 0
f 1
dl 0
loc 98
ccs 8
cts 20
cp 0.4
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toCookie() 0 2 1
A _getBaseRoute() 0 2 1
A getFiles() 0 2 1
A _getUserSessionKey() 0 2 1
A rememberMe() 0 4 2
A onBadCreditentials() 0 2 1
A _getActiveUser() 0 2 1
A fromCookie() 0 2 1
A getCookieUser() 0 2 1
1
<?php
2
3
namespace Ubiquity\controllers\auth;
4
5
use Ubiquity\cache\ClassUtils;
6
use Ubiquity\utils\http\USession;
7
use Ubiquity\utils\http\UCookie;
8
9
/**
10
 * Trait AuthControllerOverrideTrait
11
 *
12
 */
13
trait AuthControllerOverrideTrait {
14
	
15
	abstract public function badLogin();
16
	
17
	/**
18
	 * To override
19
	 * Return the base route for this Auth controller
20
	 * @return string
21
	 */
22
	public function _getBaseRoute(): string {
23
		return ClassUtils::getClassSimpleName(\get_class($this));
24
	}
25
	
26
	/**
27
	 * Processes the data posted by the login form
28
	 * Have to return the connected user instance
29
	 */
30
	abstract protected function _connect();
31
	
32
	/**
33
	 * @param object $connected
34
	 */
35
	abstract protected function onConnect($connected);
36
	
37
	/**
38
	 * To override for defining a new action when creditentials are invalid.
39
	 */
40 1
	protected function onBadCreditentials(){
41 1
		$this->badLogin();
42
	}
43
	
44
45
46
	/**
47
	 * To override for defining user session key, default : "activeUser"
48
	 * @return string
49
	 */
50 1
	public function _getUserSessionKey():string {
51 1
		return 'activeUser';
52
	}
53
	
54
	/**
55
	 * To override for getting active user, default : USession::get("activeUser")
56
	 * @return string
57
	 */
58 1
	public function _getActiveUser(){
59 1
		return USession::get($this->_getUserSessionKey());
60
	}
61
	
62
	/**
63
	 * Checks if user is valid for the action
64
	 * @param string $action
65
	 * return boolean true if activeUser is valid
66
	 */
67
	abstract public function _isValidUser($action=null):bool;
68
	
69
	/**
70
	 * Returns the value from connected user to save it in the cookie for auto connection
71
	 * @param object $connected
72
	 */
73
	protected function toCookie($connected){
74
		return;
75
	}
76
	
77
	/**
78
	 * Loads the user from database using the cookie value
79
	 * @param string $cookie
80
	 */
81
	protected function fromCookie($cookie){
82
		return;
83
	}
84
	
85
	
86
	/**
87
	 * Saves the connected user identifier in a cookie
88
	 * @param object $connected
89
	 */
90
	protected function rememberMe($connected){
91
		$id= $this->toCookie($connected);
92
		if(isset($id)){
93
			UCookie::set($this->_getUserSessionKey(),$id);
94
		}
95
	}
96
	
97
	/**
98
	 * Returns the cookie for auto connection
99
	 * @return NULL|string
100
	 */
101 1
	protected function getCookieUser(){
102 1
		return UCookie::get($this->_getUserSessionKey());
103
	}
104
	
105
	/**
106
	 * To override for changing view files
107
	 * @return AuthFiles
108
	 */
109
	protected function getFiles ():AuthFiles{
110
		return new AuthFiles();
111
	}
112
113
}
114
115