Completed
Push — master ( afd267...dbe270 )
by Nazar
04:25
created

Session::add()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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