Completed
Push — master ( 88d4e7...879ddc )
by Pierre
17:36 queued 14:17
created

Session   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 101
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

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