1 | <?php |
||
28 | final class SessionAdapter implements ZendSessionInterface |
||
29 | { |
||
30 | private const SESSION_REGENERATED_NAME = '_regenerated'; |
||
31 | |||
32 | /** @var SessionInterface */ |
||
33 | private $session; |
||
34 | |||
35 | /** @var Clock */ |
||
36 | private $clock; |
||
37 | |||
38 | public function __construct(SessionInterface $session, ?Clock $clock = null) |
||
39 | { |
||
40 | $this->session = $session; |
||
41 | $this->clock = $clock ?? new SystemClock(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return array |
||
46 | */ |
||
47 | public function toArray() : array |
||
48 | { |
||
49 | return (array) $this->session->jsonSerialize(); |
||
50 | } |
||
51 | |||
52 | /** {@inheritDoc} */ |
||
53 | public function get(string $name, $default = null) |
||
54 | { |
||
55 | return $this->session->get($name, $default); |
||
56 | } |
||
57 | |||
58 | public function has(string $name) : bool |
||
59 | { |
||
60 | return $this->session->has($name); |
||
61 | } |
||
62 | |||
63 | /** {@inheritDoc} */ |
||
64 | public function set(string $name, $value) : void |
||
65 | { |
||
66 | $this->session->set($name, $value); |
||
67 | } |
||
68 | |||
69 | public function unset(string $name) : void |
||
70 | { |
||
71 | $this->session->remove($name); |
||
72 | } |
||
73 | |||
74 | public function clear() : void |
||
75 | { |
||
76 | $this->session->clear(); |
||
77 | } |
||
78 | |||
79 | public function hasChanged() : bool |
||
80 | { |
||
81 | return $this->session->hasChanged(); |
||
82 | } |
||
83 | |||
84 | public function regenerate() : ZendSessionInterface |
||
85 | { |
||
86 | $this->session->set(self::SESSION_REGENERATED_NAME, $this->timestamp()); |
||
87 | |||
88 | return $this; |
||
89 | } |
||
90 | |||
91 | public function isRegenerated() : bool |
||
95 | |||
96 | private function timestamp() : int |
||
97 | { |
||
98 | return $this->clock->now()->getTimestamp(); |
||
99 | } |
||
100 | } |
||
101 |