Completed
Push — master ( b2e200...1332b8 )
by Oscar
04:48
created

PhpSession   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 101
rs 10

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