PhpSession   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 2
dl 0
loc 107
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A name() 0 6 1
A id() 0 6 1
D __invoke() 0 41 10
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use RuntimeException;
9
10
/**
11
 * Middleware to use php session.
12
 */
13
class PhpSession
14
{
15
    use Utils\StorageTrait;
16
17
    const STORAGE_KEY = 'PHP_SESSION_STORAGE';
18
19
    /**
20
     * @var string|null
21
     */
22
    private $name;
23
24
    /**
25
     * @var string|null
26
     */
27
    private $id;
28
29
    /**
30
     * Constructor. Defines de session name.
31
     *
32
     * @param null|string $name
33
     */
34
    public function __construct($name = null)
35
    {
36
        if ($name !== null) {
37
            $this->name($name);
38
        }
39
    }
40
41
    /**
42
     * Configure the session name.
43
     *
44
     * @param string $name
45
     *
46
     * @return self
47
     */
48
    public function name($name)
49
    {
50
        $this->name = $name;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Configure the session id.
57
     *
58
     * @param string $id
59
     *
60
     * @return self
61
     */
62
    public function id($id)
63
    {
64
        $this->id = $id;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Execute the middleware.
71
     *
72
     * @param ServerRequestInterface $request
73
     * @param ResponseInterface      $response
74
     * @param callable               $next
75
     *
76
     * @return ResponseInterface
77
     */
78
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
0 ignored issues
show
Coding Style introduced by
__invoke 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...
79
    {
80
        if (session_status() === PHP_SESSION_DISABLED) {
81
            throw new RuntimeException('PHP sessions are disabled');
82
        }
83
84
        if (session_status() === PHP_SESSION_ACTIVE) {
85
            throw new RuntimeException('Failed to start the session: already started by PHP.');
86
        }
87
88
        //Session name
89
        $name = $this->name ?: session_name();
90
        session_name($name);
91
92
        //Session id
93
        $id = $this->id;
94
95
        if (empty($id)) {
96
            $cookies = $request->getCookieParams();
97
98
            if (!empty($cookies[$name])) {
99
                $id = $cookies[$name];
100
            }
101
        }
102
103
        if (!empty($id)) {
104
            session_id($id);
105
        }
106
107
        session_start();
108
109
        $request = self::startStorage($request, isset($_SESSION[self::STORAGE_KEY]) ? $_SESSION[self::STORAGE_KEY] : []);
110
        $response = $next($request, $response);
111
112
        if ((session_status() === PHP_SESSION_ACTIVE) && (session_name() === $name)) {
113
            $_SESSION[self::STORAGE_KEY] = self::stopStorage($request);
114
            session_write_close();
115
        }
116
117
        return $response;
118
    }
119
}
120