1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nofw\Session; |
4
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
6
|
|
|
use Psr\Cache\InvalidArgumentException; |
7
|
|
|
use Psr\Log\LoggerAwareInterface; |
8
|
|
|
use Psr\Log\LoggerAwareTrait; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\Log\NullLogger; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PSR-6 Session handler implementation. |
14
|
|
|
* |
15
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class CacheSessionHandler implements \SessionHandlerInterface, LoggerAwareInterface |
18
|
|
|
{ |
19
|
|
|
use LoggerAwareTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var CacheItemPoolInterface |
23
|
|
|
*/ |
24
|
|
|
private $pool; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param CacheItemPoolInterface $pool |
28
|
|
|
* @param LoggerInterface|null $logger |
29
|
|
|
*/ |
30
|
14 |
|
public function __construct(CacheItemPoolInterface $pool, LoggerInterface $logger = null) |
31
|
|
|
{ |
32
|
14 |
|
$this->pool = $pool; |
33
|
14 |
|
$this->logger = $logger ?? new NullLogger(); |
34
|
14 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
1 |
|
public function close() |
40
|
|
|
{ |
41
|
1 |
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
3 |
View Code Duplication |
public function destroy($session_id) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
try { |
50
|
3 |
|
return $this->pool->deleteItem($session_id); |
51
|
1 |
|
} catch (InvalidArgumentException $e) { |
52
|
1 |
|
$this->logger->error( |
53
|
1 |
|
$e->getMessage(), |
54
|
|
|
[ |
55
|
1 |
|
'exception' => $e, |
56
|
1 |
|
'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **', |
57
|
1 |
|
'operation' => 'destroy', |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
|
61
|
1 |
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function gc($maxlifetime) |
69
|
|
|
{ |
70
|
1 |
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
1 |
|
public function open($save_path, $name) |
77
|
|
|
{ |
78
|
1 |
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
3 |
View Code Duplication |
public function read($session_id) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
try { |
87
|
3 |
|
$item = $this->pool->getItem($session_id); |
88
|
|
|
|
89
|
2 |
|
if ($item->isHit()) { |
90
|
2 |
|
return $item->get(); |
91
|
|
|
} |
92
|
1 |
|
} catch (InvalidArgumentException $e) { |
93
|
1 |
|
$this->logger->error( |
94
|
1 |
|
$e->getMessage(), |
95
|
|
|
[ |
96
|
1 |
|
'exception' => $e, |
97
|
1 |
|
'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **', |
98
|
1 |
|
'operation' => 'read', |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
return ''; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
3 |
View Code Duplication |
public function write($session_id, $session_data) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
try { |
112
|
3 |
|
$item = $this->pool->getItem($session_id); |
113
|
|
|
|
114
|
2 |
|
$item->set($session_data); |
115
|
|
|
|
116
|
2 |
|
return $this->pool->save($item); |
117
|
1 |
|
} catch (InvalidArgumentException $e) { |
118
|
1 |
|
$this->logger->error( |
119
|
1 |
|
$e->getMessage(), |
|
|
|
|
120
|
|
|
[ |
121
|
1 |
|
'exception' => $e, |
122
|
1 |
|
'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **', |
123
|
1 |
|
'operation' => 'write', |
124
|
|
|
] |
125
|
|
|
); |
126
|
|
|
|
127
|
1 |
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.