Completed
Branch master (b52e58)
by Pierre
03:02 queued 37s
created

Session::startSession()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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