Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
33.33% |
5 / 15 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Session | |
33.33% |
5 / 15 |
|
83.33% |
5 / 6 |
72.07 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resetConfigure | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| clear | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| session_start_if_cookie_set | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Utils; |
| 4 | |
| 5 | class Session extends AbstractSession { |
| 6 | use WithUtilsTrait; |
| 7 | |
| 8 | public function __construct() { |
| 9 | self::session_start_if_cookie_set(); |
| 10 | } |
| 11 | |
| 12 | /** @param array{timeout?: int} $config */ |
| 13 | public function resetConfigure(array $config): void { |
| 14 | global $_SESSION; |
| 15 | $session_already_exists = session_id() != '' && isset($_SESSION); |
| 16 | if ($session_already_exists) { |
| 17 | $this->clear(); |
| 18 | } |
| 19 | |
| 20 | $timeout = $config['timeout'] ?? 3600; |
| 21 | ini_set('session.gc_maxlifetime', $timeout); |
| 22 | session_set_cookie_params($timeout); |
| 23 | |
| 24 | $session_can_be_created = !headers_sent(); |
| 25 | $was_successful = false; |
| 26 | if ($session_can_be_created) { |
| 27 | // @codeCoverageIgnoreStart |
| 28 | // Reason: Cannot start session in tests. |
| 29 | // $was_successful = session_start(); |
| 30 | $was_successful = session_start() && session_regenerate_id(true); |
| 31 | // @codeCoverageIgnoreEnd |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function has(string $key): bool { |
| 36 | return isset($_SESSION[$key]); |
| 37 | } |
| 38 | |
| 39 | public function get(string $key): ?string { |
| 40 | return $_SESSION[$key] ?? null; |
| 41 | } |
| 42 | |
| 43 | public function set(string $key, ?string $new_value): void { |
| 44 | $_SESSION[$key] = $new_value; |
| 45 | } |
| 46 | |
| 47 | public function delete(string $key): void { |
| 48 | unset($_SESSION[$key]); |
| 49 | } |
| 50 | |
| 51 | // @codeCoverageIgnoreStart |
| 52 | // Reason: Cannot start/destroy session in tests. |
| 53 | |
| 54 | public function clear(): void { |
| 55 | @session_unset(); |
| 56 | @session_destroy(); |
| 57 | $name = session_name() ?: ''; |
| 58 | @setcookie($name, '', time() - 3600, '/'); |
| 59 | } |
| 60 | |
| 61 | public static function session_start_if_cookie_set(): void { |
| 62 | if (isset($_COOKIE[session_name()])) { |
| 63 | @session_start(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // @codeCoverageIgnoreEnd |
| 68 | } |