Passed
Pull Request — master (#15)
by Alexander
01:46
created

Session::check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace alkemann\h2l;
4
5
/**
6
 * Class Session implementation that access $_SESSION directly
7
 *
8
 * @codeCoverageIgnore
9
 * @package alkemann\h2l
10
 */
11
class Session implements interfaces\SessionInterface
12
{
13
    /**
14
     * @param string $key
15
     * @return mixed|null
16
     */
17
    public function get(string $key)
18
    {
19
        if (session_status() != \PHP_SESSION_ACTIVE) {
20
            session_start();
21
        }
22
        if (isset($_SESSION[$key])) {
23
            return $_SESSION[$key];
24
        }
25
        if (strpos($key, '.') !== false) {
26
            return Util::getFromArrayByKey($key, $_SESSION);
27
        }
28
        return null;
29
    }
30
31
    /**
32
     * @param string $key
33
     * @param $value
34
     * @throws \Error if trying to set with dot notation
35
     */
36
    public function set(string $key, $value): void
37
    {
38
        if ($this->active() === false) {
39
            session_start();
40
        }
41
        if (strpos($key, '.') !== false) {
42
            throw new \Error("Dot notation setting of Session values not implemented yet");
43
        }
44
        $_SESSION[$key] = $value;
45
    }
46
47
    /**
48
     * @param string $key
49
     */
50
    public function unset(string $key): void
51
    {
52
        unset($_SESSION[$key]);
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    private function active(): bool
59
    {
60
        return session_status() === \PHP_SESSION_ACTIVE;
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    public function destroy(): bool
67
    {
68
        session_destroy();
69
        if ($this->active()) {
70
            session_destroy();
71
            unset($_SESSION);
72
73
            return $this->active() === false;
74
        }
75
        return false;
76
    }
77
78
    /**
79
     * @param string $key
80
     * @return bool
81
     */
82
    public function check(string $key): bool
83
    {
84
        return isset($_SESSION[$key]);
85
    }
86
}
87