Completed
Push — master ( 895c5c...49cc4e )
by Timo
11:39
created

SessionHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Helpers;
4
5
/**
6
 * Class SessionHelper
7
 * @package hamburgscleanest\DataTables\Helpers
8
 */
9
class SessionHelper {
10
11
    const SESSION_STORAGE = 'data-tables.';
12
13
    /**
14
     * Save the state for the given key.
15
     *
16
     * @param string $key
17
     * @param mixed $sessionValue
18
     */
19 2
    public function saveState(string $key, $sessionValue) : void
20
    {
21 2
        \request()->session()->put($this->_getFormattedKey($key), $sessionValue);
22 2
    }
23
24
    /**
25
     * @param string $key
26
     * @return string
27
     */
28 3
    private function _getFormattedKey(string $key) : string
29
    {
30
        return
31 3
            self::SESSION_STORAGE .
32 3
            \preg_replace(
33 3
                '/\.|\//',
34 3
                '_',
35 3
                \preg_replace('/(http|https):\/\//', '', \request()->url())
36
            ) .
37 3
            '.' . $key;
38
    }
39
40
    /**
41
     * Get the state for the given key.
42
     *
43
     * @param string $key
44
     * @param null $default
45
     * @return mixed
46
     */
47 3
    public function getState(string $key, $default = null)
48
    {
49 3
        return \request()->session()->get($this->_getFormattedKey($key)) ?? $default;
50
    }
51
52
    /**
53
     * Remove the state for the given key.
54
     *
55
     * @param string $key
56
     */
57 1
    public function removeState(string $key) : void
58
    {
59 1
        \request()->session()->remove($this->_getFormattedKey($key));
60
    }
61
}