Passed
Push — master ( 5bd0b7...fe2b7e )
by Jean-Christophe
10:05 queued 48s
created

WithAuthTrait::finalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
namespace Ubiquity\controllers\auth;
3
4
use Ubiquity\utils\http\URequest;
5
use Ubiquity\controllers\Startup;
6
7
/**
8
 * ControllerBase
9
 *
10
 * @property \Ajax\php\ubiquity\JsUtils $jquery
11
 * @property \Ubiquity\views\View $view
12
 */
13
trait WithAuthTrait {
14
15
	protected $_checkConnectionContent;
16
17
	/**
18
	 *
19
	 * @var AuthController
20
	 */
21
	protected $authController;
22
23 1
	public function initialize() {
24 1
		parent::initialize();
25 1
		$authController = $this->_getAuthController();
26 1
		if (! URequest::isAjax() || URequest::has('_userInfo')) {
27 1
			if (! $authController->_displayInfoAsString()) {
28 1
				$authController->info();
29
			}
30 1
			if ($this->isValid(Startup::getAction())) {
31 1
				$this->_checkConnectionContent = $this->checkConnection($authController);
32
			} else {
33 1
				if ($authController->_checkConnectionTimeout() !== null)
34
					$this->jquery->clearInterval("_checkConnection");
35
			}
36
		}
37 1
	}
38
39
	/**
40
	 *
41
	 * {@inheritdoc}
42
	 * @see \Ubiquity\controllers\Controller::loadView()
43
	 */
44 1
	public function loadView($viewName, $pData = NULL, $asString = false) {
45 1
		if ((! URequest::isAjax() && $this->_getAuthController()->_displayInfoAsString()) || URequest::has('_userInfo')) {
46
			$this->view->setVar("_userInfo", $this->_getAuthController()
47
				->info());
48
		}
49 1
		return parent::loadView($viewName, $pData, $asString);
50
	}
51
52
	/**
53
	 *
54
	 * {@inheritdoc}
55
	 * @see \Ubiquity\controllers\Controller::isValid()
56
	 */
57 1
	public function isValid($action) {
58 1
		$authCtrl = $this->_getAuthController();
59 1
		$isValid = $authCtrl->_isValidUser($action);
60 1
		if (! $isValid) {
61 1
			$authCtrl->_autoConnect();
62 1
			return $authCtrl->_isValidUser($action);
63
		}
64 1
		return $isValid;
65
	}
66
67
	/**
68
	 *
69
	 * {@inheritdoc}
70
	 * @see \Ubiquity\controllers\Controller::onInvalidControl()
71
	 */
72 1
	public function onInvalidControl() {
73 1
		$auth = $this->_getAuthController();
74 1
		if (URequest::isAjax()) {
75
			$this->jquery->get($auth->_getBaseRoute() . "/noAccess/" . implode(".", Startup::$urlParts), $auth->_getBodySelector(), [
76
				"historize" => false
77
			]);
78
			echo $this->jquery->compile($this->view);
79
		} else {
80 1
			$this->initialize();
81 1
			$auth->noAccess(Startup::$urlParts);
82 1
			$this->finalize();
83
		}
84 1
		exit();
85
	}
86
87
	/**
88
	 *
89
	 * @return \Ubiquity\controllers\auth\AuthController
90
	 */
91 1
	protected function _getAuthController(): AuthController {
92 1
		if (! isset($this->authController)) {
93 1
			$this->authController = $this->getAuthController();
94 1
			Startup::injectDependences($this->authController);
95
		}
96 1
		return $this->authController;
97
	}
98
99
	protected abstract function getAuthController(): AuthController;
100
101 1
	protected function checkConnection($authController) {
102 1
		if ($authController->_checkConnectionTimeout() !== null) {
103
			$ret = $authController->_disconnected();
104
			$this->jquery->ajaxInterval("get", $authController->_getBaseRoute() . "/checkConnection/", $authController->_checkConnectionTimeout(), "_checkConnection", "", [
105
				"historize" => false,
106
				"jsCallback" => "data=($.isPlainObject(data))?data:JSON.parse(data);if(!data.valid){ $('#disconnected-modal').modal({closable: false}).modal('show');clearInterval(window._checkConnection);}"
107
			]);
108
			return $ret;
109
		}
110 1
	}
111
112 1
	public function finalize() {
113 1
		parent::finalize();
114 1
		if (isset($this->_checkConnectionContent)) {
115
			echo $this->_checkConnectionContent;
116
		}
117 1
	}
118
}
119