Completed
Push — master ( a2f223...c69a80 )
by
unknown
30s
created

Session::flashManagement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
crap 3
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
    public $flashdataId = '__flash_data';
19
20 8
    public function __construct(Container $container, Security $security)
21
    {
22 8
        parent::__construct($container, $security);
23
24 8
        $this->flashManagement();
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 flashManagement()
34
    {
35 8
        $flashdata = $_SESSION[$this->flashdataId] ?? null;
36
37 8
        if ($flashdata !== null) {
38 2
            $flashdata = unserialize(base64_decode($flashdata));
39 2
            unset($_SESSION[$this->flashdataId]);
40 2
            if (!empty($flashdata)) {
41 2
                $this->flashdata = $flashdata;
42 2
                $this->incrementFlashStorage();
43
            }
44
        }
45 8
    }
46
47 2
    private function incrementFlashStorage()
48
    {
49 2
        foreach ($this->flashdata as $variable => $data) {
50 2
            if ($this->flashdata[$variable]['increment'] > 1) {
51 1
                unset($_SESSION[$variable], $this->flashdata[$variable]);
52
            } else {
53 1
                $this->flashdata[$variable]['value'] = $data['value'];
54 2
                $this->flashdata[$variable]['increment'] ++;
55
            }
56
        }
57
58 2
        if (!empty($this->flashdata)) {
59 1
            $_SESSION[$this->flashdataId] = base64_encode(serialize($this->flashdata));
60
        }
61 2
    }
62
63
    // Because sessions persist, we need to do a little more work here..
64
    // In addition, most superglobals are immuteable, whereas session is not
65
    public function set(string $key, $value)
66
    {
67
        $key = $this->security->normalize($key);
68
        $value = $this->security->normalize($value);
69
        parent::set($key, $value); // TODO: Change the autogenerated stub
70
        $_SESSION[$key] = $value;
71
    }
72
73
    /**
74
     * @param $name
75
     * @param $value
76
     */
77
    public function setFlash($name, $value)
78
    {
79
        $current = $this->fetch($this->flashdataId);
80
        $append = base64_encode(serialize(['value' => $value, 'increment'   => 0]));
81
        array_push($current, [$name => $append]);
82
83
        $flash = $this->security->normalize($current);
84
        $_SESSION[$this->flashdataId] = $flash;
85
        $this->flashdata = $flash;
86
    }
87
88
    /**
89
     * @param string $key
90
     * @param null $default
91
     * @return array|null
92
     */
93 1
    public function getFlash(string $key, $default = null)
94
    {
95
//        print_r($this->flashdata);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
97 1
        return $this->flashdata[$key]['value'] ?? $default;
98
    }
99
100
    /**
101
     * destroys a session and related cookies
102
     */
103
    public function destroy()
104
    {
105
        $_SESSION = [];
106
107
        if (ini_get("session.use_cookies")) {
108
            $params = session_get_cookie_params();
109
            $time = time() - 42000;
110
            $path = $params['path'];
111
            $domain = $params['domain'];
112
            $secure = $params['secure'];
113
            $http = $params['httponly'];
114
            setcookie(session_name(), '', $time, $path, $domain, $secure, $http);
115
        }
116
117
        session_destroy();
118
    }
119
}
120