Session   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 166
rs 10
c 0
b 0
f 0
wmc 22
lcom 0
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
B start() 0 35 11
A open() 0 3 1
A set() 0 3 1
A save() 0 5 2
A exists() 0 3 1
A get() 0 7 2
A remove() 0 9 2
A destroy() 0 5 1
A close() 0 3 1
1
<?php
2
3
namespace Asymptix\web;
4
5
use Asymptix\helpers\Naming;
6
7
/**
8
 * Session functionality.
9
 *
10
 * @category Asymptix PHP Framework
11
 * @author Dmytro Zarezenko <[email protected]>
12
 * @copyright (c) 2015 - 2016, Dmytro Zarezenko
13
 *
14
 * @git https://github.com/Asymptix/Framework
15
 * @license http://opensource.org/licenses/MIT
16
 */
17
class Session {
18
19
    /**
20
     * Start new or resume existing session.
21
     *
22
     * @param string $name [optional] The session name references the name of the
23
     *           session, which is used in cookies and URLs (e.g. PHPSESSID).
24
     *           It should contain only alphanumeric characters; it should be short
25
     *           and descriptive (i.e. for users with enabled cookie warnings).
26
     *           If name is specified, the name of the current session is changed
27
     *           to its value.
28
     *
29
     *           The session name can't consist of digits only, at least one letter
30
     *           must be present. Otherwise a new session id is generated every time.
31
     * @param array $iniSettings [optional] List of parameters for the ini_set() function.
32
     *
33
     * @param bool $useCookie [optional] Use cookie or not. All next arguments
34
     *           works only if this parameter is TRUE.
35
     * @param int $lifetime [optional] Lifetime of the session cookie, defined in seconds.
36
     * @param string $path [optional] Path on the domain where the cookie will work.
37
     *           Use a single slash ('/') for all paths on the domain.
38
     * @param string $domain [optional] Cookie domain, for example 'www.asymptix.com'.
39
     *           To make cookies visible on all subdomains then the domain must
40
     *           be prefixed with a dot like '.asymptix.com'.
41
     * @param bool $secure [optional] If TRUE cookie will only be sent over secure connections.
42
     * @param bool $httponly [optional] If set to TRUE then PHP will attempt to
43
     *           send the httponly flag when setting the session cookie.
44
     *
45
     * @return mixed This function returns TRUE if a session was successfully
46
     *          started, otherwise FALSE. If session was resumed returns session
47
     *          status: PHP_SESSION_DISABLED if sessions are disabled.
48
     *                  PHP_SESSION_NONE if sessions are enabled, but none exists.
49
     *                  PHP_SESSION_ACTIVE if sessions are enabled, and one exists.
50
     */
51
    public static function start($name = "", array $iniSettings = [], $useCookie = false,
0 ignored issues
show
Coding Style introduced by
start uses the super-global variable $_COOKIE 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
start 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...
52
            $lifetime = null, $path = null, $domain = null, $secure = null, $httponly = null)
53
    {
54
        if (!empty($iniSettings)) {
55
            foreach ($iniSettings as $key => $value) {
56
                ini_set($key, $value);
57
            }
58
        }
59
60
        if ($useCookie) {
61
            session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
62
        }
63
        if (!empty($name)) {
64
            session_name($name);
65
        }
66
67
        $status = session_status();
68
        if ($status == PHP_SESSION_NONE) {
69
            if (ini_get('session.use_cookies') && isset($_COOKIE['PHPSESSID'])) {
70
                $sessionId = $_COOKIE['PHPSESSID'];
71
            } elseif (!ini_get('session.use_only_cookies') && isset($_GET['PHPSESSID'])) {
72
                $sessionId = $_GET['PHPSESSID'];
73
            } else {
74
                return session_start();
75
            }
76
77
            if (!preg_match('/^[a-zA-Z0-9,-]{22,40}$/', $sessionId)) {
78
                return false;
79
            }
80
81
            return session_start();
82
        }
83
84
        return $status;
85
    }
86
87
    /**
88
     * Start new or resume existing session.
89
     *
90
     * @return mixed This function returns TRUE if a session was successfully
91
     *          started, otherwise FALSE. If session was resumed returns session
92
     *          status: PHP_SESSION_DISABLED if sessions are disabled.
93
     *                  PHP_SESSION_NONE if sessions are enabled, but none exists.
94
     *                  PHP_SESSION_ACTIVE if sessions are enabled, and one exists.
95
     */
96
    public static function open() {
97
        return self::start();
98
    }
99
100
    /**
101
     * Sets session variable value.
102
     *
103
     * @param string $fieldName
104
     * @param mixed $fieldValue
105
     */
106
    public static function set($fieldName, $fieldValue = true) {
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...
107
        Naming::setValueWithComplexName($_SESSION, $fieldName, $fieldValue);
108
    }
109
110
    /**
111
     * Save data to the session.
112
     *
113
     * @param array $data
114
     */
115
    public static function save($data = []) {
0 ignored issues
show
Coding Style introduced by
save 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...
116
        foreach ($data as $key => $value) {
117
            $_SESSION[$key] = $value;
118
        }
119
    }
120
121
    /**
122
     * Verify if session variable exists.
123
     *
124
     * @param string $fieldName
125
     * @return bool
126
     */
127
    public static function exists($fieldName) {
0 ignored issues
show
Coding Style introduced by
exists 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...
128
        return isset($_SESSION[$fieldName]);
129
    }
130
131
    /**
132
     * Returns value of the session variable exists or null otherwise.
133
     *
134
     * @param string $fieldName Variable name.
135
     * @return mixed Value of the session variable or null if varible with this
136
     *           name is not exists in teh session.
137
     */
138
    public static function get($fieldName) {
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...
139
        if (self::exists($fieldName)) {
140
            return $_SESSION[$fieldName];
141
        }
142
143
        return null;
144
    }
145
146
    /**
147
     * Removes varibale from session.
148
     *
149
     * @param string $fieldName Variable name.
150
     * @return bool True if variable removed or false if it doesn't exist.
151
     */
152
    public static function remove($fieldName) {
0 ignored issues
show
Coding Style introduced by
remove 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
        if (self::exists($fieldName)) {
154
            unset($_SESSION[$fieldName]);
155
156
            return true;
157
        }
158
159
        return false;
160
    }
161
162
    /**
163
     * Destroys all data registered to a session.
164
     *
165
     * @return bool True on success or false on failure.
166
     */
167
    public static function destroy() {
168
        session_unset();
169
170
        return session_destroy();
171
    }
172
173
    /**
174
     * Destroys all data registered to a session.
175
     *
176
     * @return bool True on success or false on failure.
177
     */
178
    public static function close() {
179
        return self::destroy();
180
    }
181
182
}
183