Completed
Push — master ( 5c0dbc...42c9a9 )
by Nazar
04:27
created

Session::cdb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	cs\Session\Data,
11
	cs\Session\Management;
12
13
/**
14
 * Class responsible for current user session
15
 *
16
 * Provides next events:
17
 *
18
 *  System/Session/init/before
19
 *
20
 *  System/Session/init/after
21
 *
22
 *  System/Session/load
23
 *  ['session_data' => $session_data]
24
 *
25
 *  System/Session/add
26
 *  ['session_data' => $session_data]
27
 *
28
 *  System/Session/del/before
29
 *  ['id' => $session_id]
30
 *
31
 *  System/Session/del/after
32
 *  ['id' => $session_id]
33
 *
34
 *  System/Session/del_all
35
 *  ['id' => $user_id]
36
 *
37
 * @method static $this instance($check = false)
38
 */
39
class Session {
40
	use
41
		CRUD,
42
		Singleton,
43
		Data,
44
		Management;
45
	const INIT_STATE_METHOD          = 'init';
46
	const INITIAL_SESSION_EXPIRATION = 300;
47
	/**
48
	 * @var Cache\Prefix
49
	 */
50
	protected $cache;
51
	/**
52
	 * @var Cache\Prefix
53
	 */
54
	protected $users_cache;
55
	protected $data_model = [
56
		'id'          => 'text',
57
		'user'        => 'int:0',
58
		'created'     => 'int:0',
59
		'expire'      => 'int:0',
60
		'user_agent'  => 'text',
61
		'remote_addr' => 'text',
62
		'ip'          => 'text',
63
		'data'        => 'json'
64
	];
65
	protected $table      = '[prefix]sessions';
66
	/**
67
	 * Returns database index
68
	 *
69
	 * @return int
70
	 */
71 22
	protected function cdb () {
72 22
		return Config::instance()->module('System')->db('users');
73
	}
74 30
	protected function init () {
75 30
		if (!$this->cache) {
76 30
			$this->cache       = Cache::prefix('sessions');
77 30
			$this->users_cache = Cache::prefix('users');
78
		}
79 30
		$this->session_id = null;
80 30
		$this->user_id    = User::GUEST_ID;
81 30
		Event::instance()->fire('System/Session/init/before');
82 30
		$this->init_session();
83 30
		Event::instance()->fire('System/Session/init/after');
84 30
	}
85
}
86