SessionHelper::getState()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
     * @throws \RuntimeException
19
     */
20 2
    public function saveState(string $key, $sessionValue) : void
21
    {
22 2
        \request()->session()->put($this->_getFormattedKey($key), $sessionValue);
23 2
    }
24
25
    /**
26
     * @param string $key
27
     * @return string
28
     */
29 3
    private function _getFormattedKey(string $key) : string
30
    {
31
        return
32 3
            self::SESSION_STORAGE .
33 3
            \preg_replace(
34 3
                '/\.|\//',
35 3
                '_',
36 3
                \preg_replace('/(http|https):\/\//', '', \request()->url())
37
            ) .
38 3
            '.' . $key;
39
    }
40
41
    /**
42
     * Get the state for the given key.
43
     *
44
     * @param string $key
45
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
46
     * @return mixed
47
     * @throws \RuntimeException
48
     */
49 3
    public function getState(string $key, $default = null)
50
    {
51 3
        return \request()->session()->get($this->_getFormattedKey($key)) ?? $default;
52
    }
53
54
    /**
55
     * Remove the state for the given key.
56
     *
57
     * @param string $key
58
     * @throws \RuntimeException
59
     */
60 1
    public function removeState(string $key) : void
61
    {
62 1
        \request()->session()->remove($this->_getFormattedKey($key));
63
    }
64
}