Passed
Push — session ( 40b06a )
by Fabio
25:12 queued 16:27
created

THttpSessionHandler::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * THttpSessionHandler class
5
 *
6
 * @author Fabio Bas <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Web;
12
13
use Prado\Exceptions\TInvalidDataValueException;
14
15
/**
16
 * THttpSessionHandler class
17
 *
18
 * THttpSessionHandler contains methods called internally by PHP when using
19
 * {@see THttpSession::setUseCustomStorage THttpSession::UseCustomStorage}
20
 *
21
 * @author Fabio Bas <[email protected]>
22
 * @since 4.3.1
23
 */
24
class THttpSessionHandler implements \SessionHandlerInterface
25
{
26
	/**
27
	 * @var THttpSession
28
	 */
29
	private $_session;
30
31
	/**
32
	 * Constructor.
33
	 * @param object $session
34
	 * @throws TInvalidDataTypeException if the object is not a THttpSession
35
	 */
36
	public function __construct($session)
37
	{
38
		if (!($session instanceof THttpSession)) {
39
			throw new TInvalidDataValueException(500, 'httpsession_handler_invalid');
40
		}
41
42
		$this->_session = $session;
43
	}
44
45
	/**
46
	 * Session close handler.
47
	 * @return bool whether session is closed successfully
48
	 */
49
	public function close(): bool {
50
		return $this->_session->_close();
51
	}
52
53
	/**
54
	 * Session destroy handler.
55
	 * @param string $id session ID
56
	 * @return bool whether session is destroyed successfully
57
	 */
58
	public function destroy(string $id): bool {
59
		return $this->_session->_destroy($id);
60
	}
61
62
	/**
63
	 * Session GC (garbage collection) handler.
64
	 * @param int $max_lifetime the number of seconds after which data will be seen as 'garbage' and cleaned up.
65
	 * @return bool whether session is GCed successfully
66
	 */
67
	public function gc(int $max_lifetime): int|false {
68
		return $this->_session->_gc($max_lifetime);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_session->_gc($max_lifetime) returns the type true which is incompatible with the type-hinted return false|integer.
Loading history...
69
	}
70
71
	/**
72
	 * Session open handler.
73
	 * @param string $path session save path
74
	 * @param string $name session name
75
	 * @return bool whether session is opened successfully
76
	 */
77
	public function open(string $path, string $name): bool {
78
		return $this->_session->_open($path, $name);
79
	}
80
81
	/**
82
	 * Session read handler.
83
	 * @param string $id session ID
84
	 * @return string|false the session data
85
	 */
86
	public function read(string $id): string|false {
87
		return $this->_session->_read($id);
88
	}
89
90
	/**
91
	 * Session write handler.
92
	 * @param string $id session ID
93
	 * @param string $data session data
94
	 * @return bool whether session write is successful
95
	 */
96
	public function write(string $id, string $data): bool {
97
		return $this->_session->_write($id, $data);
98
	}
99
}
100