Session   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 0
dl 0
loc 165
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPrefix() 0 4 2
A setPrefix() 0 5 1
A isStarted() 0 4 1
A init() 0 7 2
A set() 0 12 4
A pull() 0 11 2
A get() 0 5 1
A id() 0 4 1
A regenerate() 0 5 1
A display() 0 4 1
A destroy() 0 15 2
1
<?php
2
3
namespace DrMVC\Session;
4
5
/**
6
 * Class Session
7
 * @package DrMVC\Session
8
 */
9
class Session implements SessionInterface
10
{
11
    /**
12
     * Session prefix name
13
     * @var string
14
     */
15
    private $_prefix;
16
17
    /**
18
     * Session constructor.
19
     *
20
     * @param   string|null $prefix
21
     */
22
    public function __construct(string $prefix = null)
23
    {
24
        $this->setPrefix($prefix);
25
    }
26
27
    /**
28
     * Generate prefix with key, or return only prefix
29
     *
30
     * @param   string|null $key
31
     * @return  string|null
32
     */
33
    public function getPrefix(string $key = null)
34
    {
35
        return (null !== $key) ? $this->_prefix . $key : $this->_prefix;
36
    }
37
38
    /**
39
     * @param   string|null $prefix
40
     * @return  SessionInterface
41
     */
42
    public function setPrefix(string $prefix = null): SessionInterface
43
    {
44
        $this->_prefix = $prefix;
45
        return $this;
46
    }
47
48
    /**
49
     * @return bool
50
     */
51
    public function isStarted(): bool
52
    {
53
        return (session_status() === PHP_SESSION_ACTIVE);
54
    }
55
56
    /**
57
     * if session has not started, start sessions
58
     *
59
     * @return  string
60
     */
61
    public function init(): string
62
    {
63
        if (!$this->isStarted()) {
64
            session_start();
65
        }
66
        return $this->id();
67
    }
68
69
    /**
70
     * Add value to a session.
71
     *
72
     * @param   array|string $name name the data to save
73
     * @param   string|bool $value the data to save
74
     * @return  SessionInterface
75
     */
76
    public function set($name, $value = false): SessionInterface
77
    {
78
        if (\is_array($name) && false === $value) {
79
            foreach ($name as $key => $val) {
80
                $_SESSION[$this->getPrefix($key)] = $val;
81
            }
82
        } else {
83
            $_SESSION[$this->getPrefix($name)] = $value;
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 76 can also be of type array; however, DrMVC\Session\Session::getPrefix() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
84
        }
85
86
        return $this;
87
    }
88
89
    /**
90
     * Extract item from session then delete from the session,
91
     * finally return the item.
92
     *
93
     * @param   string $name item to extract
94
     * @return  mixed
95
     */
96
    public function pull(string $name): string
97
    {
98
        $name = $this->getPrefix($name);
99
100
        if (isset($_SESSION[$name])) {
101
            $value = $_SESSION[$name];
102
            unset($_SESSION[$name]);
103
            return $value;
104
        }
105
        return false;
106
    }
107
108
    /**
109
     * Get item from session
110
     *
111
     * @param   string $name item to look for in session
112
     * @return  mixed
113
     */
114
    public function get(string $name)
115
    {
116
        $key = $this->getPrefix() . $name;
117
        return $_SESSION[$key] ?? false;
118
    }
119
120
    /**
121
     * Return session id
122
     *
123
     * @return  string with the session id.
124
     */
125
    public function id(): string
126
    {
127
        return session_id();
128
    }
129
130
    /**
131
     * Regenerate session_id and return new id
132
     *
133
     * @return  string
134
     */
135
    public function regenerate(): string
136
    {
137
        session_regenerate_id(true);
138
        return $this->id();
139
    }
140
141
    /**
142
     * Return the session array.
143
     *
144
     * @return  array of session indexes
145
     */
146
    public function display(): array
147
    {
148
        return $_SESSION;
149
    }
150
151
    /**
152
     * Remove some single key, remove keys by prefix or destroy session
153
     *
154
     * @param   string $name session name to destroy
155
     * @return  SessionInterface
156
     */
157
    public function destroy($name = null): SessionInterface
158
    {
159
        $name = $this->getPrefix($name) ?? null;
160
161
        if (null === $name) {
162
            // Clean up if key name is not provided
163
            session_unset();
164
            session_destroy();
165
        } else {
166
            // Or just remove single key
167
            unset($_SESSION[$name]);
168
        }
169
170
        return $this;
171
    }
172
173
}
174