Completed
Push — master ( 242e65...f5b54f )
by
unknown
04:49 queued 02:03
created

Session   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 107
ccs 0
cts 64
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A flashGarbageCollection() 0 16 4
A incrementFlashStorage() 0 11 3
A set() 0 7 1
A setFlash() 0 8 1
A getFlash() 0 4 1
A destroy() 0 16 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
    public function __construct(Container $container, Security $security)
21
    {
22
        parent::__construct($container, $security);
23
24
        $this->flashGarbageCollection();
25
        $session = $_SESSION ?? [];
26
        $this->registerGlobal($session);
27
    }
28
29
    /**
30
     * Processes current requests flashdata, recycles old.
31
     * @access private
32
     */
33
    private function flashGarbageCollection()
34
    {
35
        $flashdata = $_SESSION[$this->flashdataId] ?? null;
36
        if ($flashdata !== null) {
37
38
            $flashdata = unserialize(base64_decode($flashdata));
39
            unset($_SESSION[$this->flashdataId]);
40
41
            if (!empty($flashdata)) {
42
                $this->incrementFlashStorage($flashdata);
43
                if (!empty($flashdata)) {
44
                    $_SESSION[$this->flashdataId] = base64_encode(serialize($this->flashdata));
45
                }
46
            }
47
        }
48
    }
49
50
    private function incrementFlashStorage(array $storage)
51
    {
52
        foreach ($storage as $variable => $data) {
53
            $storage[$variable]['increment'] ++;
54
            if ($storage[$variable]['increment'] > 1) {
55
                unset($_SESSION[$variable], $storage[$variable]);
56
            } else {
57
                $this->flashdata[$variable] = $data['value'];
58
            }
59
        }
60
    }
61
62
    // Because sessions persist, we need to do a little more work here..
63
    // In addition, most superglobals are immuteable, whereas session is not
64
    public function set(string $key, $value)
65
    {
66
        $key = $this->security->normalize($key);
67
        $value = $this->security->normalize($value);
68
        parent::set($key, $value); // TODO: Change the autogenerated stub
69
        $_SESSION[$key] = $value;
70
    }
71
72
    /**
73
     * @param $name
74
     * @param $value
75
     */
76
    public function setFlash($name, $value)
77
    {
78
        $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...
79
        $append = base64_encode(serialize(['value' => $value, 'inc'   => 0]));
80
        array_push($current, [$name => $append]);
81
82
        $_SESSION[$this->flashdataId] = $this->security->normalize($current);
83
    }
84
85
    /**
86
     * @param string $key
87
     * @param null $default
88
     * @return array|null
89
     */
90
    public function getFlash(string $key, $default = null)
91
    {
92
        return $this->flashdata[$key]['value'] ?? $default;
93
    }
94
95
    /**
96
     * destroys a session and related cookies
97
     */
98
    public function destroy()
99
    {
100
        $_SESSION = [];
101
102
        if (ini_get("session.use_cookies")) {
103
            $params = session_get_cookie_params();
104
            $time = time() - 42000;
105
            $path = $params['path'];
106
            $domain = $params['domain'];
107
            $secure = $params['secure'];
108
            $http = $params['httponly'];
109
            setcookie(session_name(), '', $time, $path, $domain, $secure, $http);
110
        }
111
112
        session_destroy();
113
    }
114
}
115