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

Session   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A flashManagement() 0 13 3
A incrementFlashStorage() 0 15 4
A set() 0 7 1
A setFlash() 0 10 1
A getFlash() 0 6 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
    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