Completed
Push — master ( d9f7fe...0d6725 )
by Igor
02:53
created

Session   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 129
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A bag() 0 4 1
B start() 0 22 6
A regenerate() 0 12 3
A destroy() 0 9 2
A getId() 0 4 1
A setId() 0 8 2
A getName() 0 4 1
A setName() 0 8 2
1
<?php
2
/**
3
 * @license MIT
4
 */
5
namespace Pivasic\Bundle\Common\Session;
6
7
/**
8
 * Class Session
9
 * @package Pivasic\Bundle\Common\Session
10
 */
11
class Session
12
{
13
    public function __construct()
14
    {
15
        $this->session = null;
16
        //ini_set('session.use_cookies', 1);
17
    }
18
19
    /**
20
     * @return SessionBag|null
21
     */
22
    public function bag()
23
    {
24
        return $this->session;
25
    }
26
27
    /**
28
     * @return bool
29
     *
30
     * @throws \RuntimeException
31
     */
32
    public function start()
33
    {
34
        if (null !== $this->session) {
35
            return true;
36
        }
37
38
        if (\PHP_SESSION_ACTIVE === session_status()) {
39
            throw new \RuntimeException('Failed to start the session: already started by PHP.');
40
        }
41
42
        if (ini_get('session.use_cookies') && headers_sent($file, $line)) {
43
            throw new \RuntimeException(sprintf('Failed to start the session because headers have already been sent by "%s" at line %d.', $file, $line));
44
        }
45
46
        if (!session_start()) {
47
            throw new \RuntimeException('Failed to start the session');
48
        }
49
50
        $this->session = new SessionBag();
51
52
        return true;
53
    }
54
55
    /**
56
     * @param bool|false $destroy
57
     * @param null $lifetime
58
     * @return bool
59
     */
60
    public function regenerate($destroy = false, $lifetime = null)
61
    {
62
        if (\PHP_SESSION_ACTIVE !== session_status()) {
63
            return false;
64
        }
65
66
        if (null !== $lifetime) {
67
            ini_set('session.cookie_lifetime', $lifetime);
68
        }
69
70
        return session_regenerate_id($destroy);
71
    }
72
73
    /**
74
     * Destroy session.
75
     */
76
    public function destroy()
77
    {
78
        session_unset();
79
        session_destroy();
80
        session_write_close();
81
        if (!headers_sent()) {
82
            setcookie(session_name(), '', 0, '/');
83
        }
84
    }
85
86
    /**
87
     * Gets the session ID.
88
     *
89
     * @return string
90
     */
91
    public function getId()
92
    {
93
        return session_id();
94
    }
95
96
    /**
97
     * Sets the session ID.
98
     *
99
     * @param string $id
100
     *
101
     * @throws \LogicException
102
     */
103
    public function setId($id)
104
    {
105
        if (null === $this->session) {
106
            throw new \LogicException('Cannot change the ID of an active session');
107
        }
108
109
        session_id($id);
110
    }
111
112
    /**
113
     * Gets the session name.
114
     *
115
     * @return string
116
     */
117
    public function getName()
118
    {
119
        return session_name();
120
    }
121
122
    /**
123
     * Sets the session name.
124
     *
125
     * @param string $name
126
     *
127
     * @throws \LogicException
128
     */
129
    public function setName($name)
130
    {
131
        if (null === $this->session) {
132
            throw new \LogicException('Cannot change the name of an active session');
133
        }
134
135
        session_name($name);
136
    }
137
138
    private $session;
139
}