Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Session | |
100.00% |
6 / 6 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| destroy | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Dynart\Micro; |
| 4 | |
| 5 | /** |
| 6 | * Session handler |
| 7 | * @package Dynart\Micro |
| 8 | */ |
| 9 | class Session { |
| 10 | |
| 11 | /** |
| 12 | * Starts the session (only once) |
| 13 | */ |
| 14 | public function __construct() { |
| 15 | if (session_status() == PHP_SESSION_NONE) { |
| 16 | session_start(); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Destroys the session |
| 22 | */ |
| 23 | public function destroy() { |
| 24 | $_SESSION = []; |
| 25 | session_destroy(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Returns with a session value by name or default |
| 30 | * @param string $name The name of the session variable |
| 31 | * @param mixed|null $default The default value if the name does not exist |
| 32 | * @return mixed|null The value of the session variable |
| 33 | */ |
| 34 | public function get(string $name, $default = null) { |
| 35 | return array_key_exists($name, $_SESSION) ? $_SESSION[$name] : $default; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Sets a session variable |
| 40 | * @param string $name The name of the session variable |
| 41 | * @param mixed $value The value of the session variable |
| 42 | */ |
| 43 | public function set(string $name, $value) { |
| 44 | $_SESSION[$name] = $value; |
| 45 | } |
| 46 | |
| 47 | } |