Completed
Push — master ( ab7f39...f31c26 )
by Jean-Christophe
02:08
created

WithAuthTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 81
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B initialize() 0 13 5
A loadView() 0 6 3
A isValid() 0 3 1
A onInvalidControl() 0 13 2
A _getAuthController() 0 7 2
getAuthController() 0 1 ?
A checkConnection() 0 7 2
1
<?php
2
namespace Ubiquity\controllers\auth;
3
4
use Ubiquity\utils\http\URequest;
5
use Ubiquity\controllers\Startup;
6
7
/**
8
 * ControllerBase
9
 * @property \Ajax\php\ubiquity\JsUtils $jquery
10
 **/
11
trait WithAuthTrait{
12
	
13
	/**
14
	 * @var AuthController
15
	 */
16
	protected $authController;
17
	
18
	public function initialize(){
19
		parent::initialize();
20
		$authController=$this->_getAuthController();
21
		if(!URequest::isAjax() && !$authController->_displayInfoAsString()){
22
			$authController->info();
23
			if($this->isValid()){
24
				$this->checkConnection($authController);
25
			}else{
26
				if($authController->_checkConnectionTimeout()!=null)
27
					$this->jquery->clearInterval("_checkConnection");
28
			}
29
		}
30
	}
31
	
32
	/**
33
	 * {@inheritDoc}
34
	 * @see \Ubiquity\controllers\Controller::loadView()
35
	 */
36
	public function loadView($viewName, $pData = NULL, $asString = false) {
37
		if(!URequest::isAjax() && $this->_getAuthController()->_displayInfoAsString()){
38
			$this->view->setVar("_userInfo",$this->_getAuthController()->info());
0 ignored issues
show
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
		}
40
		return parent::loadView ($viewName,$pData,$asString);
41
	}
42
43
	/**
44
	 * {@inheritDoc}
45
	 * @see \Ubiquity\controllers\Controller::isValid()
46
	 */
47
	public function isValid() {
48
		return $this->_getAuthController()->_isValidUser();
49
	}
50
51
	/**
52
	 * {@inheritDoc}
53
	 * @see \Ubiquity\controllers\Controller::onInvalidControl()
54
	 */
55
	public function onInvalidControl() {
56
		$auth=$this->_getAuthController();
57
		if(URequest::isAjax()){
58
			Startup::injectDependences($this, Startup::getConfig());
59
			$this->jquery->get($auth->_getBaseRoute()."/noAccess/".implode(".", Startup::$urlParts),$auth->_getBodySelector(),["historize"=>false]);	
60
			echo $this->jquery->compile($this->view);
61
		}else{
62
			$auth->initialize();
63
			$auth->noAccess(Startup::$urlParts);
64
			$auth->finalize();
65
		}
66
		exit();
67
	}
68
	
69
	/**
70
	 * @return \Ubiquity\controllers\auth\AuthController
71
	 */
72
	protected function _getAuthController():AuthController{
73
		if(!isset($this->authController)){
74
			$this->authController=$this->getAuthController();
75
			Startup::injectDependences($this->authController, Startup::getConfig());
76
		}
77
		return $this->authController;
78
	}
79
	
80
	protected abstract function getAuthController():AuthController;
81
	
82
	
83
	protected function checkConnection($authController){
84
		if($authController->_checkConnectionTimeout()!=null){
85
			$authController->_disconnected();
86
			$this->jquery->ajaxInterval("get",$authController->_getBaseRoute()."/_checkConnection",$authController->_checkConnectionTimeout(),"_checkConnection","",["jsCallback"=>"data=($.isPlainObject(data))?data:JSON.parse(data);if(!data.valid){ $('#disconnected-modal').modal({closable: false}).modal('show');clearInterval(window._checkConnection);}"]);
87
		}
88
			
89
	}
90
91
}
92