SessionMiddleware::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 5
c 3
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 2
crap 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