Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| CacheSessionHandler | |
0.00% |
0 / 17 |
|
0.00% |
0 / 8 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| create_sid | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| open | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| close | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| read | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| write | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| destroy | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| gc | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\DataStorage\Session |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\DataStorage\Session; |
| 16 | |
| 17 | use phpOMS\DataStorage\Cache\CacheStatus; |
| 18 | use phpOMS\DataStorage\Cache\Connection\ConnectionAbstract; |
| 19 | |
| 20 | /** |
| 21 | * Cache session handler. |
| 22 | * |
| 23 | * @package phpOMS\DataStorage\Session |
| 24 | * @license OMS License 2.0 |
| 25 | * @link https://jingga.app |
| 26 | * @since 1.0.0 |
| 27 | * |
| 28 | * @SuppressWarnings(PHPMD.Superglobals) |
| 29 | */ |
| 30 | final class CacheSessionHandler implements \SessionHandlerInterface, \SessionIdInterface |
| 31 | { |
| 32 | /** |
| 33 | * Cache connection |
| 34 | * |
| 35 | * @var ConnectionAbstract |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | private ConnectionAbstract $con; |
| 39 | |
| 40 | /** |
| 41 | * Expiration time |
| 42 | * |
| 43 | * @var int |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | private int $expire = 3600; |
| 47 | |
| 48 | /** |
| 49 | * Constructor |
| 50 | * |
| 51 | * @param ConnectionAbstract $con ConnectionAbstract |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | public function __construct(ConnectionAbstract $con, int $expire = 3600) |
| 56 | { |
| 57 | $this->con = $con; |
| 58 | $this->expire = $expire; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Create a session id string |
| 63 | * |
| 64 | * @return string |
| 65 | * |
| 66 | * @since 1.0.0 |
| 67 | */ |
| 68 | public function create_sid() : string |
| 69 | { |
| 70 | return ($sid = \session_create_id('s-')) === false ? '' : $sid; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Open the session storage |
| 75 | * |
| 76 | * @param string $savePath Path of the session data |
| 77 | * @param string $sessionName Name of the session |
| 78 | * |
| 79 | * @return bool |
| 80 | * |
| 81 | * @since 1.0.0 |
| 82 | */ |
| 83 | public function open(string $savePath, string $sessionName) : bool |
| 84 | { |
| 85 | if ($this->con->getStatus() !== CacheStatus::OK) { |
| 86 | $this->con->connect(); |
| 87 | } |
| 88 | |
| 89 | return $this->con->getStatus() === CacheStatus::OK; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Closing the cache connection doesn't happen in here and must be implemented in the application |
| 94 | * |
| 95 | * @return bool |
| 96 | * |
| 97 | * @since 1.0.0 |
| 98 | */ |
| 99 | public function close() : bool |
| 100 | { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Read the session data (also prolongs the expire) |
| 106 | * |
| 107 | * @param string $id Session id |
| 108 | * |
| 109 | * @return false|string |
| 110 | * |
| 111 | * @since 1.0.0 |
| 112 | */ |
| 113 | public function read(string $id) : string|false |
| 114 | { |
| 115 | $data = $this->con->get($id); |
| 116 | |
| 117 | if ($data === null) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | $this->con->updateExpire($this->expire); |
| 122 | |
| 123 | return (string) $data; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Write session data |
| 128 | * |
| 129 | * @param string $id Session id |
| 130 | * @param string $data Session data |
| 131 | * |
| 132 | * @return bool |
| 133 | * |
| 134 | * @since 1.0.0 |
| 135 | */ |
| 136 | public function write(string $id, string $data) : bool |
| 137 | { |
| 138 | $this->con->set($id, $data, -1); |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Destroy the session |
| 145 | * |
| 146 | * @param string $id Session id |
| 147 | * |
| 148 | * @return bool |
| 149 | * |
| 150 | * @since 1.0.0 |
| 151 | */ |
| 152 | public function destroy(string $id) : bool |
| 153 | { |
| 154 | $this->con->delete($id); |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Garbage collect session data |
| 161 | * |
| 162 | * @param int $maxlifetime Maximum session data life time |
| 163 | * |
| 164 | * @return int|false |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | */ |
| 168 | public function gc(int $maxlifetime) : int|false |
| 169 | { |
| 170 | return (int) $this->con->flush($maxlifetime); |
| 171 | } |
| 172 | } |