Session::destroy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace System;
4
5
class Session
6
{
7
    /**
8
     * Application Object
9
     *
10
     * @var \System\Application
11
     */
12
    private $app;
13
14
    /**
15
     * Constructor
16
     *
17
     * @param \System\Application $app
18
     */
19
    public function __construct(Application $app)
20
    {
21
        $this->app = $app;
22
    }
23
24
    /**
25
     * Start session
26
     *
27
     * @return void
28
     */
29
    public function start()
30
    {
31
        ini_set('session.use_only_cookies', 1);
32
33
        if (!session_id()) {
34
            session_start();
35
        }
36
    }
37
38
    /**
39
     * Set new value to session
40
     *
41
     * @param string $key
42
     * @param mixed $value
43
     * @return void
44
     */
45
    public function set(string $key, $value)
46
    {
47
        $_SESSION[$key] = $value;
48
    }
49
50
    /**
51
     * Get value from session by the given key
52
     *
53
     * @param string $key
54
     * @param mixed $default
55
     * @return mixed
56
     */
57
    public function get(string $key, $default = null)
58
    {
59
        return array_get($_SESSION, $key, $default);
60
    }
61
62
    /**
63
     * Determine if the session has the given key
64
     *
65
     * @param string $key
66
     * @return bool
67
     */
68
    public function has(string $key)
69
    {
70
        return isset($_SESSION[$key]);
71
    }
72
73
    /**
74
     * Remove the given key from session
75
     *
76
     * @param string $key
77
     * @return void
78
     */
79
    public function remove(string $key)
80
    {
81
        unset($_SESSION[$key]);
82
    }
83
84
    /**
85
     * Get value from session by the given key then remove it
86
     *
87
     * @param string $key
88
     * @return mixed
89
     */
90
    public function pull(string $key)
91
    {
92
        $value = $this->get($key);
93
94
        $this->remove($key);
95
96
        return $value;
97
    }
98
99
    /**
100
     * Get all session data
101
     *
102
     * @return array
103
     */
104
    public function all()
105
    {
106
        return $_SESSION;
107
    }
108
109
    /**
110
     * Destroy session
111
     *
112
     * @return void
113
     */
114
    public function destroy()
115
    {
116
        session_destroy();
117
118
        unset($_SESSION);
119
    }
120
}
121