Session   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 1
cbo 5
dl 0
loc 190
rs 10
c 1
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 4 1
A isStarted() 0 4 1
A getId() 0 4 1
A getName() 0 4 1
A getBag() 0 4 1
A getAttributeBag() 0 4 1
A getFlashBag() 0 4 1
A setSaveHandler() 0 4 1
A __construct() 0 17 3
A setId() 0 6 1
A setName() 0 6 1
A save() 0 6 1
A clear() 0 6 1
A setBag() 0 6 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;
12
13
use Borobudur\Http\Session\Attribute\AttributeBag;
14
use Borobudur\Http\Session\Attribute\AttributeBagInterface;
15
use Borobudur\Http\Session\Flash\FlashBag;
16
use Borobudur\Http\Session\Flash\FlashBagInterface;
17
use Borobudur\Http\Session\SessionBagInterface;
18
use Borobudur\Http\Session\Storage\SessionStorage;
19
use SessionHandlerInterface;
20
21
/**
22
 * @author      Iqbal Maulana <[email protected]>
23
 * @created     8/3/15
24
 */
25
class Session
26
{
27
    /**
28
     * @var SessionStorage
29
     */
30
    protected $storage;
31
32
    /**
33
     * @var string
34
     */
35
    private $attributeName;
36
37
    /**
38
     * @var string
39
     */
40
    private $flashName;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param SessionHandlerInterface    $handler
46
     * @param AttributeBagInterface|null $attributeBag
47
     * @param FlashBagInterface|null     $flashBag
48
     */
49
    public function __construct(
50
        SessionHandlerInterface $handler,
51
        AttributeBagInterface $attributeBag = null,
52
        FlashBagInterface $flashBag = null
53
    ) {
54
        $this->storage = new SessionStorage($handler);
55
56
        // Register attribute bag.
57
        $attribute = $attributeBag ?: new AttributeBag();
58
        $this->storage->setBag($attribute);
59
        $this->attributeName = $attribute->getName();
60
61
        // Register flash bag.
62
        $flash = $flashBag ?: new FlashBag();
63
        $this->storage->setBag($flash);
64
        $this->flashName = $flash->getName();
65
    }
66
67
    /**
68
     * Start session.
69
     *
70
     * @return bool
71
     */
72
    public function start()
73
    {
74
        return $this->storage->start();
75
    }
76
77
    /**
78
     * Check if session has been started.
79
     *
80
     * @return bool
81
     */
82
    public function isStarted()
83
    {
84
        return $this->storage->isStarted();
85
    }
86
87
    /**
88
     * Get session id.
89
     *
90
     * @return string
91
     */
92
    public function getId()
93
    {
94
        return $this->storage->getId();
95
    }
96
97
    /**
98
     * Set session id.
99
     *
100
     * @param string $id
101
     *
102
     * @return $this
103
     */
104
    public function setId($id)
105
    {
106
        $this->storage->setId($id);
107
108
        return $this;
109
    }
110
111
    /**
112
     * Get session name.
113
     *
114
     * @return string
115
     */
116
    public function getName()
117
    {
118
        return $this->storage->getName();
119
    }
120
121
    /**
122
     * Set session name.
123
     *
124
     * @param string $name
125
     *
126
     * @return $this
127
     */
128
    public function setName($name)
129
    {
130
        $this->storage->setName($name);
131
132
        return $this;
133
    }
134
135
    /**
136
     * Persist session.
137
     *
138
     * @return $this
139
     */
140
    public function save()
141
    {
142
        $this->storage->save();
143
144
        return $this;
145
    }
146
147
    /**
148
     * Clear session.
149
     *
150
     * @return $this
151
     */
152
    public function clear()
153
    {
154
        $this->storage->clear();
155
156
        return $this;
157
    }
158
159
    /**
160
     * Set session bag.
161
     *
162
     * @param SessionBagInterface $bag
163
     *
164
     * @return $this
165
     */
166
    public function setBag(SessionBagInterface $bag)
167
    {
168
        $this->storage->setBag($bag);
169
170
        return $this;
171
    }
172
173
    /**
174
     * Get session bag.
175
     *
176
     * @param string $name
177
     *
178
     * @return SessionBagInterface
179
     */
180
    public function getBag($name)
181
    {
182
        return $this->storage->getBag($name);
183
    }
184
185
    /**
186
     * Get attribute bag.
187
     *
188
     * @return AttributeBagInterface
189
     */
190
    public function getAttributeBag()
191
    {
192
        return $this->getBag($this->attributeName);
193
    }
194
195
    /**
196
     * Get flash bag.
197
     *
198
     * @return FlashBagInterface
199
     */
200
    public function getFlashBag()
201
    {
202
        return $this->getBag($this->flashName);
203
    }
204
205
    /**
206
     * Set session save handler.
207
     *
208
     * @param SessionHandlerInterface $handler
209
     */
210
    public function setSaveHandler(SessionHandlerInterface $handler)
211
    {
212
        $this->storage->setSaveHandler($handler);
213
    }
214
}
215