Completed
Push — master ( 4fdb73...356849 )
by Changwan
03:37
created

GlobalHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 82.14%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 70
ccs 23
cts 28
cp 0.8214
rs 10
wmc 13
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 7 1
A read() 0 8 2
A write() 0 8 2
A close() 0 4 1
A open() 0 4 1
B bootSession() 0 8 5
A gc() 0 4 1
1
<?php
2
namespace Wandu\Http\Session\Handler;
3
4
use SessionHandlerInterface;
5
6
class GlobalHandler implements SessionHandlerInterface
7
{
8
    /**
9
     * {@inheritdoc}
10
     */
11 2
    public function destroy($sessionId)
0 ignored issues
show
Coding Style introduced by
destroy 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...
12
    {
13 2
        $this->bootSession();
14 2
        $_SESSION = []; // destroy
15 2
        @session_destroy();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
16 2
        return true;
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 3
    public function read($sessionId)
0 ignored issues
show
Coding Style introduced by
read 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...
23
    {
24 3
        $this->bootSession();
25 3
        if (count($_SESSION)) {
26 1
            return serialize($_SESSION);
27
        }
28 3
        return '';
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    public function write($sessionId, $sessionData)
0 ignored issues
show
Coding Style introduced by
write 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...
35
    {
36 2
        $this->bootSession();
37 2
        $dataSet = unserialize($sessionData);
38 2
        foreach ($dataSet as $key => $value) {
39 1
            $_SESSION[$key] = $value;
40
        }
41 2
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function close()
47
    {
48
        return true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function gc($maxLifeTime)
55
    {
56 1
        return true;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function open($savePath, $sessionId)
63
    {
64
        return true;
65
    }
66
    
67 4
    protected function bootSession()
0 ignored issues
show
Coding Style introduced by
bootSession 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...
68
    {
69 4
        if (\PHP_SAPI === 'cli' && !isset($_SESSION)) {
70 1
            $_SESSION = [];
71 4
        } else if (\PHP_SAPI !== 'cli' && session_status() == \PHP_SESSION_NONE) {
72
            @session_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
73
        }
74 4
    }
75
}
76