Passed
Push — master ( 0ed261...e52b37 )
by Jean-Christophe
17:09
created

AuthControllerCoreTrait::getOriginalURL()   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\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
	protected function getBaseUrl() {
38 1
		return URequest::getUrl ( $this->_getBaseRoute () );
39
	}
40
	
41 1
	protected function useAjax():bool{
42 1
		return true;
43
	}
44
45 1
	public function message($type, $header, $body, $icon = 'info', $id = null) {
46 1
		return $this->loadView ( $this->_getFiles ()->getViewMessage (), get_defined_vars (), true );
47
	}
48
49 1
	protected function fMessage(FlashMessage $fMessage, $id = null) {
50 1
		return $this->message ( $fMessage->getType (), $fMessage->getTitle (), $fMessage->getContent (), $fMessage->getIcon (), $id );
51
	}
52
	
53
	public function _newAccountCreationRule(){
54
		if (URequest::isPost()) {
55
			$result = [];
56
			UResponse::asJSON();
57
			$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

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