Completed
Push — master ( a4c6a1...4caae0 )
by Timo
04:49
created

SessionHelper::saveState()   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 3
crap 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Helpers;
4
5
use Illuminate\Http\Request;
6
7
/**
8
 * Class SessionHelper
9
 * @package hamburgscleanest\DataTables\Helpers
10
 *
11
 * TODO: Facade?
12
 */
13
class SessionHelper {
14
15
    const SESSION_STORAGE = 'data-tables.';
16
17
    /**
18
     * @param Request $request
19
     * @param string $key
20
     * @return string
21
     */
22 3
    private static function getFormattedKey(Request $request, string $key)
23
    {
24
        return
25 3
            self::SESSION_STORAGE .
26 3
            \preg_replace(
27 3
                '/\.|\//',
28 3
                '_',
29 3
                \preg_replace('/(http|https):\/\//', '', $request->url())
30
            ) .
31 3
            '.' . $key;
32
    }
33
34
    /**
35
     * @param Request $request
36
     * @param string $key
37
     * @param mixed $sessionValue
38
     */
39 2
    public static function saveState(Request $request, string $key, $sessionValue)
40
    {
41 2
        $request->session()->put(self::getFormattedKey($request, $key), $sessionValue);
42 2
    }
43
44
    /**
45
     * @param Request $request
46
     * @param string $key
47
     * @param null $default
48
     * @return mixed
49
     */
50 3
    public static function getState(Request $request, string $key, $default = null)
51
    {
52 3
        return $request->session()->get(self::getFormattedKey($request, $key)) ?? $default;
53
    }
54
55
    /**
56
     * @param Request $request
57
     * @param string $key
58
     */
59 1
    public static function removeState(Request $request, string $key)
60
    {
61 1
        $request->session()->remove(self::getFormattedKey($request, $key));
62
    }
63
}