Session::start()   B
last analyzed

Complexity

Conditions 8
Paths 27

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.013

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 2
b 0
f 0
nc 27
nop 0
dl 0
loc 24
ccs 16
cts 17
cp 0.9412
crap 8.013
rs 8.4444
1
<?php
2
/**
3
 * The Session class
4
 */
5
namespace Phile\Core;
6
7
/**
8
 * the Session class for implementing a session
9
 *
10
 * @author  Frank Nägler
11
 * @link    https://philecms.github.io
12
 * @license http://opensource.org/licenses/MIT
13
 * @package Phile\Core
14
 */
15
class Session
16
{
17
    /**
18
     * @var bool mark if session is started
19
     */
20
    public static $isStarted = false;
21
22
    /**
23
     * @var string the session id
24
     */
25
    public static $sessionId = '';
26
27
    /**
28
     * method to start the session
29
     */
30 7
    public static function start()
31
    {
32 7
        if (self::$isStarted === false) {
33 7
            session_cache_limiter('private');
34 7
            session_cache_expire(120);
35 7
            if (session_start()) {
36 7
                self::$isStarted = true;
37
            }
38 7
            if (self::$isStarted) {
39 7
                if (PHILE_CLI_MODE) {
40 7
                    $_SERVER['REMOTE_ADDR'] = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
41
                }
42 7
                if (self::get('REMOTE_ADDR') != $_SERVER['REMOTE_ADDR']) {
43 7
                    session_destroy();
44 7
                    session_start();
45 7
                    session_regenerate_id();
46 7
                    self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
47
                }
48 7
                if (self::get('REMOTE_ADDR') === null) {
49
                    self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
50
                }
51
            }
52
        }
53 7
        self::$sessionId = session_id();
54
    }
55
56
    /**
57
     * method to destroy the session
58
     */
59
    public static function destroy()
60
    {
61
        unset($_SESSION);
62
        session_destroy();
63
    }
64
65
    /**
66
     * method to save and close the session
67
     */
68
    public static function save()
69
    {
70
        session_write_close();
71
    }
72
73
    /**
74
     * method to set value into session
75
     *
76
     * @param string $key
77
     * @param mixed  $value
78
     */
79 7
    public static function set($key, $value)
80
    {
81 7
        if (!self::$isStarted) {
82 5
            self::start();
83
        }
84 7
        $_SESSION[$key] = $value;
85
    }
86
87
    /**
88
     * method to get value from session
89
     *
90
     * @param string $key
91
     * @param mixed  $default
92
     *
93
     * @return null|mixed
94
     */
95 7
    public static function get($key, $default = null)
96
    {
97 7
        if (!self::$isStarted) {
98
            self::start();
99
        }
100
101 7
        return (self::isEmpty($key)) ? $default : $_SESSION[$key];
102
    }
103
104
    /**
105
     * get the session id
106
     *
107
     * @return string
108
     */
109
    public static function getSessionId()
110
    {
111
        if (!self::$isStarted) {
112
            self::start();
113
        }
114
115
        return self::$sessionId;
116
    }
117
118
    /**
119
     * check id key is empty/set or not
120
     *
121
     * @param string $key
122
     *
123
     * @return bool
124
     */
125 7
    public static function isEmpty($key)
126
    {
127 7
        if (!self::$isStarted) {
128
            self::start();
129
        }
130
131 7
        return (!isset($_SESSION[$key]));
132
    }
133
}
134