Passed
Push — development ( 032ead...866be2 )
by Mirco
01:47
created

SessionDataNative::debug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/***************************************************************************
3
 *  For license information see doc/license.txt
4
 *
5
 *  Unicode Reminder メモ
6
 *
7
 *  Session data handling with build-in php session
8
 ***************************************************************************/
9
10
namespace Oc\Session;
11
12
/**
13
 * Class SessionDataNative
14
 * Not for productive usage!! Implementation not finished yet
15
 */
16
class SessionDataNative implements SessionDataInterface
17
{
18
    public $changed = false;
19
    public $values = array();
20
    public $session_initialized = false;
21
22
    /**
23
     * SessionDataNative constructor.
24
     */
25
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
26
    {
27
        if (isset($_REQUEST['SESSION']) && $_REQUEST['SESSION'] !== '') {
28
            $this->initSession();
29
        }
30
    }
31
32
    /**
33
     *
34
     */
35
    private function initSession()
0 ignored issues
show
Coding Style introduced by
initSession uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
initSession uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
initSession uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
initSession uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
36
    {
37
        global $opt;
38
39
        if ($this->session_initialized !== true) {
40
            session_name('SESSION');
41
            session_set_cookie_params(
42
                $opt['session']['expire']['cookie'],
43
                $opt['session']['path'],
44
                $opt['session']['domain']
45
            );
46
            session_start();
47
48
            if ($opt['session']['check_referer']) {
49
                if (isset($_SERVER['REFERER'])) {
50
                    // TODO fix the following if statement, seems corrupted
51
                    if (strtolower(
52
                            substr(
53
                                'http' + strstr($_SERVER['REFERER'], '://'),
54
                                0,
55
                                strlen($opt['page']['absolute_http_url'])
56
                            )
57
                        ) != strtolower($opt['page']['absolute_http_url'])
58
                    ) {
59
                        $this->createNewSession();
60
                    }
61
                }
62
            }
63
64
            if ((isset($_GET['SESSION']) || isset($_POST['SESSION'])) && count($_SESSION) > 0) {
65
                // compare and set timestamp
66
                if (isset($_SESSION['lastcall'])) {
67
                    if (abs(time() - $_SESSION['lastcall']) > $opt['session']['expire']['url']) {
68
                        $this->createNewSession();
69
                    }
70
                }
71
72
                $_SESSION['lastcall'] = time();
73
            }
74
75
            $this->session_initialized = true;
76
        }
77
    }
78
79
    /**
80
     *
81
     */
82
    private function createNewSession()
0 ignored issues
show
Coding Style introduced by
createNewSession uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
83
    {
84
        session_regenerate_id();
85
        $locale = isset($_SESSION['locale']) ? $_SESSION['locale'] : '';
86
        foreach ($_SESSION as $k => $v) {
87
            unset($_SESSION[$k]);
88
        }
89
        if ($locale != '') {
90
            $_SESSION['locale'] = $locale;
91
        }
92
    }
93
94
    /**
95
     * @param $name
96
     * @param $value
97
     * @param null $default
98
     */
99
    public function set($name, $value, $default = null)
0 ignored issues
show
Coding Style introduced by
set uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
100
    {
101
        if (!isset($_SESSION[$name]) || $_SESSION[$name] != $value) {
102
            if ($value == $default) {
103
                if (isset($_SESSION[$name])) {
104
                    unset($_SESSION[$name]);
105
                    $this->changed = true;
106
                }
107
            } else {
108
                $this->init_session();
0 ignored issues
show
Bug introduced by
The method init_session() does not seem to exist on object<Oc\Session\SessionDataNative>.

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...
109
                $_SESSION[$name] = $value;
110
                $this->changed = true;
111
            }
112
        }
113
    }
114
115
    /**
116
     * @param $name
117
     * @param null $default
118
     *
119
     * @return null
120
     */
121
    public function get($name, $default = null)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
122
    {
123
        return isset($_SESSION[$name]) ? $_SESSION[$name] : $default;
124
    }
125
126
    /**
127
     * @param $name
128
     *
129
     * @return bool
130
     */
131
    public function is_set($name)
0 ignored issues
show
Coding Style introduced by
is_set uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
132
    {
133
        return isset($_SESSION[$name]);
134
    }
135
136
    /**
137
     * @param $name
138
     */
139
    public function un_set($name)
0 ignored issues
show
Coding Style introduced by
un_set uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
140
    {
141
        if (isset($_SESSION[$name])) {
142
            unset($_SESSION[$name]);
143
            $this->changed = true;
144
        }
145
    }
146
147
    public function header()
148
    {
149
        // is automatically sent
150
    }
151
152
    public function debug()
0 ignored issues
show
Coding Style introduced by
debug uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
153
    {
154
        print_r($_SESSION);
155
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method debug() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
156
    }
157
158
    public function close()
0 ignored issues
show
Coding Style introduced by
close uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
159
    {
160
        if ($this->session_initialized === true) {
161
            if (count($_SESSION) === 0) {
162
                try {
163
                    session_destroy();
164
                } catch (\Exception $e) {
165
                    // @todo implement logging
166
                }
167
            } else {
168
                session_write_close();
169
            }
170
        }
171
    }
172
}
173