Session   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 105
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A startSession() 0 9 3
A setSession() 0 12 4
A deleteSession() 0 9 2
A hasSession() 0 9 4
A getSession() 0 12 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nymfonya\Component\Http;
6
7
use Nymfonya\Component\Http\Cookie;
8
use Nymfonya\Component\Http\Interfaces\SessionInterface;
9
10
class Session extends Cookie implements SessionInterface
11
{
12
13
    protected $session;
14
15
    /**
16
     * instanciate
17
     */
18 3
    public function __construct()
19
    {
20 3
        $this->session = &$_SESSION;
21 3
        parent::__construct();
22
    }
23
24
    /**
25
     * start a session with a session name
26
     *
27
     * @param string $sessionName
28
     * @return SessionInterface
29
     */
30 2
    public function startSession(string $sessionName): SessionInterface
31
    {
32 2
        $isActive = session_status() === PHP_SESSION_ACTIVE ? true : false;
33 2
        if (!$isActive) {
34 2
            session_name(sha1($sessionName));
35 2
            session_start();
36
        }
37 2
        return $this;
38
    }
39
40
    /**
41
     * set
42
     *
43
     * @param string $name
44
     * @param mixed $value
45
     * @param string $key
46
     * @return SessionInterface
47
     */
48 2
    public function setSession(string $name, $value, $key = ''): SessionInterface
49
    {
50 2
        if ($key) {
51 2
            if (!$this->hasSession($name, $key) || !is_array($this->session[$name])) {
52 2
                $this->session[$name] = [];
53
            }
54 2
            $this->session[$name][$key] = $value;
55
        } else {
56 2
            $this->session[$name] = $value;
57
        }
58 2
        return $this;
59
    }
60
61
    /**
62
     * deleteSession
63
     *
64
     * @param string $name
65
     * @param string $key
66
     * @return SessionInterface
67
     */
68 1
    public function deleteSession(string $name, string $key = ''): SessionInterface
69
    {
70 1
        if ($key) {
71 1
            unset($this->session[$name][$key]);
72
        } else {
73 1
            unset($this->session[$name]);
74
        }
75 1
        return $this;
76
    }
77
78
    /**
79
     * hasSession
80
     *
81
     * @param string $name
82
     * @param string $key
83
     * @return boolean
84
     */
85 2
    public function hasSession(string $name, string $key = ''): bool
86
    {
87 2
        if (!$key) {
88 2
            return (isset($this->session[$name])
89 2
                && !empty($this->session[$name]));
90
        }
91 2
        return (isset($this->session[$name][$key])
92 2
            && !empty($this->session[$name][$key]));
93
    }
94
95
    /**
96
     * getSession
97
     *
98
     * @param string $name
99
     * @param string $key
100
     * @return mixed
101
     */
102 2
    public function getSession(string $name, string $key = '')
103
    {
104 2
        if (!$key) {
105 2
            return $this->hasSession($name)
106 2
                ? $this->session[$name]
107 2
                : '';
108
        }
109
        return
110 2
            $this->hasSession($name, $key)
111 2
            ? $this->session[$name][$key]
112 2
            : '';
113
    }
114
}
115