Passed
Push — master ( 6a78e3...31c3ef )
by Jean-Christophe
19:36
created

AuthControllerOverrideTrait::onBadCreditentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
	 * To override for getting active user, default : USession::get("activeUser")
47
	 * @return string
48
	 */
49 1
	public function _getActiveUser(){
50 1
		return USession::get($this->_getUserSessionKey());
0 ignored issues
show
Bug introduced by
It seems like _getUserSessionKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
		return USession::get($this->/** @scrutinizer ignore-call */ _getUserSessionKey());
Loading history...
51
	}
52
	
53
	/**
54
	 * Checks if user is valid for the action
55
	 * @param string $action
56
	 * return boolean true if activeUser is valid
57
	 */
58
	abstract public function _isValidUser($action=null):bool;
59
	
60
	/**
61
	 * Returns the value from connected user to save it in the cookie for auto connection
62
	 * @param object $connected
63
	 */
64
	protected function toCookie($connected){
65
		return;
66
	}
67
	
68
	/**
69
	 * Loads the user from database using the cookie value
70
	 * @param string $cookie
71
	 */
72
	protected function fromCookie($cookie){
73
		return;
74
	}
75
	
76
	
77
	/**
78
	 * Saves the connected user identifier in a cookie
79
	 * @param object $connected
80
	 */
81
	protected function rememberMe($connected){
82
		$id= $this->toCookie($connected);
83
		if(isset($id)){
84
			UCookie::set($this->_getUserSessionKey(),$id);
85
		}
86
	}
87
	
88
	/**
89
	 * Returns the cookie for auto connection
90
	 * @return NULL|string
91
	 */
92 1
	protected function getCookieUser(){
93 1
		return UCookie::get($this->_getUserSessionKey());
94
	}
95
	
96
	/**
97
	 * To override for changing view files
98
	 * @return AuthFiles
99
	 */
100
	protected function getFiles ():AuthFiles{
101
		return new AuthFiles();
102
	}
103
104
}
105
106