BackendManager   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 10 2
A getCurrent() 0 3 1
A setCurrent() 0 3 1
1
<?php
2
3
/**
4
 * Provides a convenient way to access the current backend and its configuration
5
 *
6
 * @author Sam Stenvall <[email protected]>
7
 * @copyright Copyright &copy; Sam Stenvall 2013-
8
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
9
 */
10
class BackendManager extends CApplicationComponent
11
{
12
	
13
	const SESSION_KEY = 'currentBackendId';
14
15
	/**
16
	 * @var Backend the current backend
17
	 */
18
	private $_backend;
19
20
	/**
21
	 * Initializes the component. The backend to use is chosen here. If a 
22
	 * current backend is stored in the session we use that, otherwise we fall 
23
	 * back on whatever backend is set as default.
24
	 */
25
	public function init()
26
	{
27
		$currentBackendId = Yii::app()->session->get(self::SESSION_KEY);
28
29
		if ($currentBackendId !== null)
30
			$this->_backend = Backend::model()->findByPk($currentBackendId);
31
		else
32
			$this->_backend = Backend::model()->default()->find();
33
34
		parent::init();
35
	}
36
37
	/**
38
	 * @return Backend the current backend, or null if no backend is configured
39
	 */
40
	public function getCurrent()
41
	{
42
		return $this->_backend;
43
	}
44
	
45
	/**
46
	 * Sets the default backend
47
	 * @param Backend $backend
48
	 */
49
	public function setCurrent($backend)
50
	{
51
		Yii::app()->session->add(self::SESSION_KEY, $backend->id);
52
	}
53
54
}