Test Failed
Push — master ( cf3dbd...f03535 )
by Jean-Christophe
27:09
created

AuthControllerCoreTrait::newAccountCreationRule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Ubiquity\controllers\auth;
4
5
use Ubiquity\utils\http\URequest;
6
use Ubiquity\utils\http\USession;
7
use Ubiquity\utils\flash\FlashMessage;
8
use Ubiquity\utils\http\UResponse;
9
10
/**
11
 *
12
 * @author jcheron <[email protected]>
13
 * @property AuthFiles $authFiles
14
 * @property string $_loginCaption
15
 * @property \Ajax\php\ubiquity\JsUtils $jquery
16
 */
17
trait AuthControllerCoreTrait {
18
19
	abstract public function loadView($viewName, $pData = NULL, $asString = false);
20
21
	abstract protected function attemptsTimeout();
22
23
	abstract protected function getFiles(): AuthFiles;
24
25
	abstract public function _getBodySelector();
26
27
	abstract public function _getBaseRoute();
28
	
29
	abstract protected function _newAccountCreationRule(string $accountName):?bool;
30
	
31
	abstract public function _getLoginInputName();
32
	
33
	abstract protected function hasAccountCreation():bool;
34
	
35
	abstract protected function canCreateAccountMessage(FlashMessage $fMessage);
36
37 1
	abstract protected function getAccountRecoveryLink():string;
38 1
	
39
	protected function getBaseUrl():string {
40
		return URequest::getUrl ( $this->_getBaseRoute () );
41 1
	}
42 1
	
43
	protected function useAjax():bool{
44
		return true;
45 1
	}
46 1
47
	#[\Ubiquity\attributes\items\router\NoRoute()]
48
	public function message($type, $header, $body, $icon = 'info', $id = null):string {
49 1
		return $this->loadView ( $this->_getFiles ()->getViewMessage (), \get_defined_vars (), true );
50 1
	}
51
52
	protected function fMessage(FlashMessage $fMessage, $id = null):string {
53
		return $this->message ( $fMessage->getType (), $fMessage->getTitle (), $fMessage->getContent (), $fMessage->getIcon (), $id );
54
	}
55
	
56
	#[\Ubiquity\attributes\items\router\Post]
57
	public function newAccountCreationRule(){
58
		if (URequest::isPost()) {
59
			$result = [];
60
			UResponse::asJSON();
61
			$result['result'] = $this->_newAccountCreationRule(URequest::post($this->_getLoginInputName()));
1 ignored issue
show
Bug introduced by
It seems like Ubiquity\utils\http\UReq...->_getLoginInputName()) can also be of type null; however, parameter $accountName of Ubiquity\controllers\aut...ewAccountCreationRule() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

61
			$result['result'] = $this->_newAccountCreationRule(/** @scrutinizer ignore-type */ URequest::post($this->_getLoginInputName()));
Loading history...
62
			echo \json_encode($result);
63
		}
64
	}
65
66
	protected function noAttempts() {
67
		$timeout = $this->attemptsTimeout ();
68
		$plus = '';
69
		if (\is_numeric ( $timeout )) {
70
			$this->jquery->exec ( "$('._login').addClass('disabled');", true );
71
			$plus = " You can try again {_timer}";
72
			$this->jquery->exec ( "var startTimer=function(duration, display) {var timer = duration, minutes, seconds;
73
											var interval=setInterval(function () {
74
												minutes = parseInt(timer / 60, 10);seconds = parseInt(timer % 60, 10);
75
												minutes = minutes < 10 ? '0' + minutes : minutes;
76
												seconds = seconds < 10 ? '0' + seconds : seconds;
77
												display.html(minutes + ':' + seconds);
78
												if (--timer < 0) {clearInterval(interval);$('#timeout-message').hide();$('#bad-login').removeClass('attached');$('._login').removeClass('disabled');}
79
											}, 1000);
80
										}", true );
81
			$timeToLeft = USession::getTimeout ( $this->_attemptsSessionKey );
82
			$this->jquery->exec ( "startTimer({$timeToLeft},$('#timer'));", true );
83
			$this->jquery->compile ( $this->view );
84 1
		}
85 1
		return new FlashMessage ( "<i class='ui warning icon'></i> You have no more attempt of connection !" . $plus, null, "bottom attached error", "" );
86 1
	}
87
88
	protected function authLoadView($viewName, $vars = [ ]):void {
89
		if($this->useAjax()){
90 1
			$loadView=function($vn,$v){$this->jquery->renderView($vn,$v);};
91 1
		}else{
92 1
			$loadView=function($vn,$v) {$this->loadView($vn, $v);};
93
		}
94
		if($this->hasAccountCreation()){
95
			$vars['resetPassword']=$this->getAccountRecoveryLink();
96
		}
97 1
		$files = $this->_getFiles ();
98
		$mainTemplate = $files->getBaseTemplate ();
99 1
		if (isset ( $mainTemplate )) {
100
			$vars ['_viewname'] = $viewName;
101 1
			$vars ['_base'] = $mainTemplate;
102 1
			$loadView ( $files->getViewBaseTemplate (), $vars );
103
		} else {
104
			$loadView ( $viewName, $vars );
105 1
		}
106 1
	}
107 1
108
	protected function getOriginalURL() {
109 1
		return USession::get ( 'urlParts' );
110
	}
111
112
	protected function _getFiles(): AuthFiles {
113
		if (! isset ( $this->authFiles )) {
114
			$this->authFiles = $this->getFiles ();
115
		}
116 1
		return $this->authFiles;
117 1
	}
118
119
	protected function getViewVars($viewname) {
120
		return [ 'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector (),'_loginCaption' => $this->_loginCaption ];
121
	}
122
	
123
	protected function addAccountCreationViewData(array &$vData,$forMessage=false){
124
		if($this->hasAccountCreation()){
125
			if($forMessage){
126
				$fMessage = new FlashMessage ( "<p>You can create one now!</p><a href='{createAccountUrl}' class='ui button black ajax _create' data-target='{accountCreationTarget}'>Create account</a>", "Don't have an account yet?", "", "question" );
127 1
				$this->canCreateAccountMessage ( $fMessage->parseContent(['accountCreationTarget'=>$this->_getBodySelector(),'createAccountUrl'=>$this->getBaseUrl().'/addAccount']) );
128
				$vData['canCreateAccountMessage'] = $this->fMessage ( $fMessage );
129
			}else{
130
				$vData['createAccountUrl']=$this->getBaseUrl().'/addAccount';
131
				$vData['accountCreationTarget']=$this->_getBodySelector();
132
			}
133
		}
134
	}
135
}
136
137