FlashBag   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 310
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 0
dl 0
loc 310
rs 9.3999
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A replaceKeep() 0 6 1
A addKeep() 0 8 2
A setKeep() 0 6 1
A isKept() 0 4 1
A removeKeep() 0 8 2
A replace() 0 6 1
A add() 0 8 2
A set() 0 6 1
A peek() 0 8 2
A peekAll() 0 4 1
A get() 0 11 2
A has() 0 4 1
A all() 0 11 3
A keys() 0 4 1
A count() 0 4 1
A getName() 0 4 1
A getStorageKey() 0 4 1
A initialize() 0 13 3
A flush() 0 10 4
A clear() 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\Flash;
12
13
/**
14
 * @author      Iqbal Maulana <[email protected]>
15
 * @created     8/3/15
16
 */
17
class FlashBag implements FlashBagInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $name = 'flashes';
23
24
    /**
25
     * @var string
26
     */
27
    private $storageKey;
28
29
    /**
30
     * @var array
31
     */
32
    private $flashes = array();
33
34
    /**
35
     * @var array
36
     */
37
    private $keeps = array();
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param string $storageKey
43
     */
44
    public function __construct($storageKey = '___BOROBUDUR_FLASHES')
45
    {
46
        $this->storageKey = $storageKey;
47
    }
48
49
    /**
50
     * Clear keep keys and replace with sets of it.
51
     *
52
     * @param array $keys
53
     *
54
     * @return $this
55
     */
56
    public function replaceKeep(array $keys)
57
    {
58
        $this->keeps = array();
59
60
        return $this->add($keys);
61
    }
62
63
    /**
64
     * Add sets of keep keys.
65
     *
66
     * @param array $keys
67
     *
68
     * @return $this
69
     */
70
    public function addKeep(array $keys)
71
    {
72
        foreach ($keys as $key) {
73
            $this->setKeep($key);
74
        }
75
76
        return $this;
77
    }
78
79
    /**
80
     * Set keep key.
81
     *
82
     * Keep data with specified key.
83
     *
84
     * @param string $key
85
     *
86
     * @return $this
87
     */
88
    public function setKeep($key)
89
    {
90
        $this->keeps[] = $key;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Check if specified key is kept.
97
     *
98
     * @param string $key
99
     *
100
     * @return bool
101
     */
102
    public function isKept($key)
103
    {
104
        return in_array($key, $this->keeps);
105
    }
106
107
    /**
108
     * Remove keep key.
109
     *
110
     * @param string $key
111
     *
112
     * @return $this
113
     */
114
    public function removeKeep($key)
115
    {
116
        if ($this->isKept($key)) {
117
            unset($this->keeps[$key]);
118
        }
119
120
        return $this;
121
    }
122
123
    /**
124
     * Clear all flash data and replace with sets of it.
125
     *
126
     * @param array $values
127
     *
128
     * @return $this
129
     */
130
    public function replace(array $values)
131
    {
132
        $this->flashes = array();
133
134
        return $this->add($values);
135
    }
136
137
    /**
138
     * Add sets of flash data.
139
     *
140
     * @param array $values
141
     *
142
     * @return $this
143
     */
144
    public function add(array $values)
145
    {
146
        foreach ($values as $key => $value) {
147
            $this->set($key, $value);
148
        }
149
150
        return $this;
151
    }
152
153
    /**
154
     * Set flash data.
155
     *
156
     * @param string $key
157
     * @param mixed  $value
158
     *
159
     * @return $this
160
     */
161
    public function set($key, $value)
162
    {
163
        $this->flashes[$key] = $value;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Get flash data by key.
170
     *
171
     * @param string     $key
172
     * @param mixed|null $default
173
     *
174
     * @return mixed
175
     */
176
    public function peek($key, $default = null)
177
    {
178
        if ($this->has($key)) {
179
            return $this->flashes[$key];
180
        }
181
182
        return $default;
183
    }
184
185
    /**
186
     * Get all flash data.
187
     *
188
     * @return array
189
     */
190
    public function peekAll()
191
    {
192
        return $this->flashes;
193
    }
194
195
    /**
196
     * Get flash data by key and clear it.
197
     *
198
     * @param string     $key
199
     * @param mixed|null $default
200
     *
201
     * @return mixed
202
     */
203
    public function get($key, $default = null)
204
    {
205
        if ($this->has($key)) {
206
            $flash = $this->flashes[$key];
207
            $this->flush($key);
208
209
            return $flash;
210
        }
211
212
        return $default;
213
    }
214
215
    /**
216
     * Check if flash data exist or not by key.
217
     *
218
     * @param string $key
219
     *
220
     * @return bool
221
     */
222
    public function has($key)
223
    {
224
        return array_key_exists($key, $this->flashes);
225
    }
226
227
    /**
228
     * Get all flash data and clear it.
229
     *
230
     * @return array
231
     */
232
    public function all()
233
    {
234
        $flashes = $this->flashes;
235
        if (!empty($this->keeps)) {
236
            foreach ($this->keys() as $key) {
237
                $this->flush($key);
238
            }
239
        }
240
241
        return $flashes;
242
    }
243
244
    /**
245
     * Get all flash data keys.
246
     *
247
     * @return array
248
     */
249
    public function keys()
250
    {
251
        return array_keys($this->flashes);
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function count()
258
    {
259
        return count($this->flashes);
260
    }
261
262
    /**
263
     * Get session bag name.
264
     *
265
     * @return string
266
     */
267
    public function getName()
268
    {
269
        return $this->name;
270
    }
271
272
    /**
273
     * Identity storage.
274
     *
275
     * @return string
276
     */
277
    public function getStorageKey()
278
    {
279
        return $this->storageKey;
280
    }
281
282
    /**
283
     * Initialize sessions.
284
     *
285
     * @param array $sessions
286
     */
287
    public function initialize(array &$sessions)
288
    {
289
        $this->flashes = &$sessions['flashes'];
290
        $this->keeps = &$sessions['keeps'];
291
292
        if (null === $this->flashes) {
293
            $this->flashes = array();
294
        }
295
296
        if (null === $this->keeps) {
297
            $this->keeps = array();
298
        }
299
    }
300
301
    /**
302
     * Flush un-keep flash.
303
     *
304
     * @param string $key
305
     */
306
    private function flush($key)
307
    {
308
        if (!empty($this->keeps)) {
309
            if (!in_array($key, $this->keeps) && $this->has($key)) {
310
                unset($this->flashes[$key]);
311
            }
312
        } else {
313
            unset($this->flashes[$key]);
314
        }
315
    }
316
317
    /**
318
     * Clear session and return all data.
319
     *
320
     * @return array
321
     */
322
    public function clear()
323
    {
324
        return $this->all();
325
    }
326
}
327