Completed
Pull Request — master (#98)
by
unknown
02:12
created

Session::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
namespace Zewa\HTTP;
4
5
use Zewa\Container;
6
use Zewa\Security;
7
8
final class Session extends SuperGlobal
9
{
10
    /**
11
     * @var array
12
     */
13
    private $flashdata = [];
14
15
    /**
16
     * @var string index for flash data
17
     */
18
    private $flashdataId = '__flash_data';
19
20 8
    public function __construct(Container $container, Security $security)
21
    {
22 8
        parent::__construct($container, $security);
23
24 8
        $this->flashGarbageCollection();
25 8
        $session = $_SESSION ?? [];
26 8
        $this->registerGlobal($session);
27 8
    }
28
29
    /**
30
     * Processes current requests flashdata, recycles old.
31
     * @access private
32
     */
33 8
    private function flashGarbageCollection()
34
    {
35 8
        $flashdata = $_SESSION[$this->flashdataId] ?? null;
36 8
        if ($flashdata !== null) {
37 2
            $flashdata = unserialize(base64_decode($flashdata));
38 2
            unset($_SESSION[$this->flashdataId]);
39
40 2
            if (!empty($flashdata)) {
41 1
                $this->incrementFlashStorage($flashdata);
42 1
                if (!empty($flashdata)) {
43 1
                    $_SESSION[$this->flashdataId] = base64_encode(serialize($this->flashdata));
44
                }
45
            }
46
        }
47 8
    }
48
49 1
    private function incrementFlashStorage(array $storage)
50
    {
51 1
        foreach ($storage as $variable => $data) {
52 1
            $storage[$variable]['increment'] ++;
53 1
            if ($storage[$variable]['increment'] > 1) {
54 1
                unset($_SESSION[$variable], $storage[$variable]);
55
            } else {
56 1
                $this->flashdata[$variable] = $data['value'];
57
            }
58
        }
59 1
    }
60
61
    // Because sessions persist, we need to do a little more work here..
62
    // In addition, most superglobals are immuteable, whereas session is not
63
    public function set(string $key, $value)
64
    {
65
        $key = $this->security->normalize($key);
66
        $value = $this->security->normalize($value);
67
        parent::set($key, $value); // TODO: Change the autogenerated stub
68
        $_SESSION[$key] = $value;
69
    }
70
71
    /**
72
     * @param $name
73
     * @param $value
74
     */
75
    public function setFlash($name, $value)
76
    {
77
        $current = $this->get($this->flashdataId);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Zewa\HTTP\Session>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
        $append = base64_encode(serialize(['value' => $value, 'inc'   => 0]));
79
        array_push($current, [$name => $append]);
80
81
        $_SESSION[$this->flashdataId] = $this->security->normalize($current);
82
    }
83
84
    /**
85
     * @param string $key
86
     * @param null $default
87
     * @return array|null
88
     */
89
    public function getFlash(string $key, $default = null)
90
    {
91
        return $this->flashdata[$key]['value'] ?? $default;
92
    }
93
94
    /**
95
     * destroys a session and related cookies
96
     */
97
    public function destroy()
98
    {
99
        $_SESSION = [];
100
101
        if (ini_get("session.use_cookies")) {
102
            $params = session_get_cookie_params();
103
            $time = time() - 42000;
104
            $path = $params['path'];
105
            $domain = $params['domain'];
106
            $secure = $params['secure'];
107
            $http = $params['httponly'];
108
            setcookie(session_name(), '', $time, $path, $domain, $secure, $http);
109
        }
110
111
        session_destroy();
112
    }
113
}
114