Completed
Push — master ( a4831f...a4416a )
by ARCANEDEV
07:50
created

PhpSession::status()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanedev\LaravelTracker\Support;
2
3
use Illuminate\Support\Arr;
4
5
/**
6
 * Class     PhpSession
7
 *
8
 * @package  Arcanedev\LaravelTracker\Support
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class PhpSession
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Constants
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    const DEFAULT_NAMESPACE = 'arcanedev/phpsession';
18
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Properties
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    private $namespace;
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Constructor
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * PhpSession constructor.
31
     * @param null $namespace
32
     */
33
    public function __construct($namespace = null)
34
    {
35
        $this->startSession();
36
        $this->setNamespace($namespace);
37
    }
38
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Getters & Setters
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    /**
44
     * Set the session namespace.
45
     *
46
     * @param  string  $namespace
47
     */
48
    public function setNamespace($namespace)
49
    {
50
        if ($namespace) {
51
            $this->namespace = $namespace;
52
        }
53
    }
54
55
    /**
56
     * Get the session status.
57
     *
58
     * @return int
59
     */
60
    public function status()
61
    {
62
        return session_status();
63
    }
64
65
    /**
66
     * Get the session id.
67
     *
68
     * @return string
69
     */
70
    public function getId()
71
    {
72
        return session_id();
73
    }
74
75
    /**
76
     * Start the session.
77
     */
78
    private function startSession()
79
    {
80
        if ( ! $this->isStarted())
81
            session_start();
82
    }
83
84
    /**
85
     * Get the session data.
86
     *
87
     * @param  string       $key
88
     * @param  string|null  $namespace
89
     *
90
     * @return mixed
91
     */
92
    public function get($key, $namespace = null)
93
    {
94
        return Arr::get($this->getNamespaceData($namespace), $key);
95
    }
96
97
    /**
98
     * Check if session has data.
99
     *
100
     * @param  string       $key
101
     * @param  string|null  $namespace
102
     *
103
     * @return bool
104
     */
105
    public function has($key, $namespace = null)
106
    {
107
        return Arr::has($this->getNamespaceData($namespace), $key);
108
    }
109
110
    /**
111
     * Add data to the session.
112
     *
113
     * @param  string       $key
114
     * @param  mixed        $value
115
     * @param  string|null  $namespace
116
     */
117
    public function put($key, $value, $namespace = null)
118
    {
119
        $this->setNamespaceData(
120
            $namespace,
121
            Arr::add($this->getNamespaceData($namespace), $key, $value)
122
        );
123
    }
124
125
    /**
126
     * Make the namespace for the session.
127
     *
128
     * @param  string|null  $namespace
129
     *
130
     * @return string
131
     */
132
    private function makeNamespace($namespace = null)
133
    {
134
        $namespace = $namespace ?: $this->namespace;
135
136
        return $namespace ?: self::DEFAULT_NAMESPACE;
137
    }
138
139
    /**
140
     * Get the session data by a namespace.
141
     *
142
     * @param  string  $namespace
143
     *
144
     * @return array
145
     */
146
    private function getNamespaceData($namespace)
0 ignored issues
show
Coding Style introduced by
getNamespaceData 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...
147
    {
148
        return Arr::get($_SESSION, $this->makeNamespace($namespace), []);
149
    }
150
151
    /**
152
     * Set the session data by a namespace.
153
     *
154
     * @param  string  $namespace
155
     * @param  mixed   $value
156
     */
157
    private function setNamespaceData($namespace, $value)
0 ignored issues
show
Coding Style introduced by
setNamespaceData 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...
158
    {
159
        $_SESSION[$this->makeNamespace($namespace)] = $value;
160
    }
161
162
    /**
163
     * Regenerate the session.
164
     *
165
     * @param  bool  $destroy
166
     *
167
     * @return string
168
     */
169
    public function regenerate($destroy = true)
170
    {
171
        session_regenerate_id($destroy);
172
173
        return $this->getId();
174
    }
175
176
    /* ------------------------------------------------------------------------------------------------
177
     |  Check Functions
178
     | ------------------------------------------------------------------------------------------------
179
     */
180
    /**
181
     * Check if the session is started.
182
     *
183
     * @return bool
184
     */
185
    private function isStarted()
186
    {
187
        return $this->status() === PHP_SESSION_ACTIVE;
188
    }
189
}
190