SaveHandlerProxy   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 152
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSaveHandler() 0 4 1
A setSaveHandler() 0 5 1
A isActive() 0 4 1
A getId() 0 4 1
A setId() 0 10 2
A getName() 0 4 1
A setName() 0 10 2
A close() 0 4 1
A destroy() 0 4 1
A gc() 0 4 1
A open() 0 4 1
A read() 0 4 1
A write() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http\Session\Storage;
12
13
use Borobudur\Http\Session\Exception\LogicException;
14
use SessionHandlerInterface;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     8/3/15
19
 */
20
class SaveHandlerProxy implements SessionHandlerInterface
21
{
22
    /**
23
     * @var SessionHandlerInterface
24
     */
25
    protected $handler;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param SessionHandlerInterface $handler
31
     */
32
    public function __construct(SessionHandlerInterface $handler)
33
    {
34
        $this->handler = $handler;
35
    }
36
37
    /**
38
     * Get session save handler.
39
     *
40
     * @return SessionHandlerInterface
41
     */
42
    public function getSaveHandler()
43
    {
44
        return $this->handler;
45
    }
46
47
    /**
48
     * Set session save handler.
49
     *
50
     * @param SessionHandlerInterface $handler
51
     */
52
    public function setSaveHandler(SessionHandlerInterface $handler)
53
    {
54
        $this->handler = $handler;
55
        session_set_save_handler($handler, false);
56
    }
57
58
    /**
59
     * Check if session has been started.
60
     *
61
     * @return bool
62
     */
63
    public function isActive()
64
    {
65
        return PHP_SESSION_ACTIVE === session_status();
66
    }
67
68
    /**
69
     * Get session id.
70
     *
71
     * @return string
72
     */
73
    public function getId()
74
    {
75
        return session_id();
76
    }
77
78
    /**
79
     * Set session id.
80
     *
81
     * @param string $id
82
     *
83
     * @return $this
84
     */
85
    public function setId($id)
86
    {
87
        if ($this->isActive()) {
88
            throw new LogicException('Cannot change session ID when session is active.');
89
        }
90
91
        session_id($id);
92
93
        return $this;
94
    }
95
96
    /**
97
     * Get session name.
98
     *
99
     * @return string
100
     */
101
    public function getName()
102
    {
103
        return session_name();
104
    }
105
106
    /**
107
     * Set session name.
108
     *
109
     * @param string $name
110
     *
111
     * @return $this
112
     */
113
    public function setName($name)
114
    {
115
        if ($this->isActive()) {
116
            throw new LogicException('Cannot change session name when session is active.');
117
        }
118
119
        session_name($name);
120
121
        return $this;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function close()
128
    {
129
        return $this->handler->close();
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function destroy($sessionId)
136
    {
137
        return $this->handler->destroy($sessionId);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function gc($maxLifeTime)
144
    {
145
        return $this->handler->gc($maxLifeTime);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function open($savePath, $sessionId)
152
    {
153
        return $this->handler->open($savePath, $sessionId);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function read($sessionId)
160
    {
161
        return $this->handler->read($sessionId);
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function write($sessionId, $sessionData)
168
    {
169
        return $this->handler->write($sessionId, $sessionData);
170
    }
171
}
172