Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Session
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 destroy
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 id
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Dynart\Micro;
4
5/**
6 * Session handler
7 * @package Dynart\Micro
8 */
9class 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     * @return mixed The session ID
30     */
31    public function id() {
32        return session_id();
33    }
34
35    /**
36     * Returns with a session value by name or default
37     * @param string $name The name of the session variable
38     * @param mixed|null $default The default value if the name does not exist
39     * @return mixed|null The value of the session variable
40     */
41    public function get(string $name, $default = null) {
42        return array_key_exists($name, $_SESSION) ? $_SESSION[$name] : $default;
43    }
44
45    /**
46     * Sets a session variable
47     * @param string $name The name of the session variable
48     * @param mixed $value The value of the session variable
49     */
50    public function set(string $name, $value) {
51        $_SESSION[$name] = $value;
52    }
53}