DBSessionHandler::gc()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Class DBSessionHandler
4
 *
5
 * @filesource   DBSessionHandler.php
6
 * @created      06.03.2017
7
 * @package      chillerlan\Session
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Session;
14
15
use chillerlan\Database\Database;
16
use chillerlan\Settings\SettingsContainerInterface;
17
use Psr\Log\LoggerInterface;
18
19
class DBSessionHandler extends SessionHandlerAbstract{
20
21
	/**
22
	 * @var \chillerlan\Database\Database
23
	 */
24
	protected $db;
25
26
	/**
27
	 * DBSessionHandler constructor.
28
	 *
29
	 * @param \chillerlan\Database\Database                  $db
30
	 * @param \chillerlan\Settings\SettingsContainerInterface $options
31
	 * @param \Psr\Log\LoggerInterface|null                  $logger
32
	 */
33
	public function __construct(Database $db, SettingsContainerInterface $options = null, LoggerInterface $logger = null){
34
		parent::__construct($options, $logger);
35
36
		$this->db = $db->connect();
37
	}
38
39
	/** @inheritdoc */
40
	public function close():bool{
41
		return true;
42
	}
43
44
	/** @inheritdoc */
45
	public function destroy($session_id):bool{
46
47
		$this->db->delete
48
			->from($this->options->db_table)
49
			->where('id', $session_id)
50
			->query();
51
52
		return true;
53
	}
54
55
	/** @inheritdoc */
56
	public function gc($maxlifetime):bool{
57
58
		$this->db->delete
59
			->from($this->options->db_table)
60
			->where('time', \time() - $maxlifetime, '<')
61
			->query();
62
63
		return true;
64
	}
65
66
	/** @inheritdoc */
67
	public function open($save_path, $name):bool{
68
		return true;
69
	}
70
71
	/**
72
	 * @param string $session_id
73
	 *
74
	 * @return string
75
	 * @throws \chillerlan\Session\SessionHandlerException
76
	 */
77
	public function read($session_id):string{
78
79
		if(empty($session_id)){
80
			throw new SessionHandlerException('invalid session id');
81
		}
82
83
		$q = $this->db->select
84
			->cols(['data'])
85
			->from([$this->options->db_table])
86
			->where('id', $session_id)
87
			->query();
88
89
		try{
90
91
			if(!$q || !isset($q[0])){
92
				return '';
93
			}
94
95
			return $this->options->use_encryption ? $this->decrypt($q[0]->data) : $q[0]->data;
96
		}
97
		catch(\Exception $e){
98
			throw new SessionHandlerException($e->getMessage());
99
		}
100
101
	}
102
103
	/**
104
	 * @inheritdoc
105
	 * @throws \chillerlan\Session\SessionHandlerException
106
	 */
107
	public function write($session_id, $session_data):bool{
108
109
		if(empty($session_id)){
110
			throw new SessionHandlerException('invalid session id');
111
		}
112
113
		$q = $this->db->insert
114
			->into($this->options->db_table, 'REPLACE', 'id')
115
			->values([
116
				'id'   => $session_id,
117
				'time' => \time(),
118
				'data' => $this->options->use_encryption ? $this->encrypt($session_data) : $session_data,
119
			])
120
			->query();
121
122
		return (bool)$q;
123
	}
124
125
}
126