SessionMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 4
b 0
f 0
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A process() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Session;
14
15
use Koded\Stdlib\Interfaces\ConfigurationFactory;
16
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
17
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
18
19
20
class SessionMiddleware implements MiddlewareInterface
21
{
22
    public const SESSION_STARTED = 'sessionStarted';
23
24 2
    public function __construct(ConfigurationFactory $settings)
25
    {
26 2
        session_start(session_register_custom_handler($settings)->sessionParameters());
27 2
    }
28
29 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
30
    {
31 2
        $request  = $request->withAttribute(self::SESSION_STARTED, PHP_SESSION_ACTIVE === session_status());
32 2
        $response = $handler->handle($request);
33
34 2
        if (500 !== $response->getStatusCode()) {
35 1
            session_write_close();
36
        }
37
38 2
        return $response;
39
    }
40
}
41