Passed
Push — master ( 8242d2...71bfa3 )
by Arnold
05:32
created

SessionMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
declare(strict_types=1);
4
5
namespace Jasny\Session;
6
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
9
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
10
use Psr\Http\Message\ResponseInterface as Response;
11
12
/**
13
 * Middleware to start a session.
14
 * The session can be mocked by setting the 'session' attribute of the server request.
15
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
16
class SessionMiddleware implements MiddlewareInterface
17
{
18
    protected SessionInterface $defaultSession;
19
20
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $session should have a doc-comment as per coding-style.
Loading history...
21
     * Class constructor.
22
     */
23
    public function __construct(?SessionInterface $session = null)
24
    {
25
        $this->defaultSession = $session ?? new GlobalSession();
26
    }
27
28
    /**
29
     * Process an incoming server request (PSR-15).
30
     *
31
     * @param ServerRequest  $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
32
     * @param RequestHandler $handler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
33
     * @return Response
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
34
     */
35
    public function process(ServerRequest $request, RequestHandler $handler): Response
36
    {
37
        return $this->run($request, \Closure::fromCallable([$handler, 'handle']));
38
    }
39
40
    /**
41
     * Get a callback that can be used as double pass middleware.
42
     *
43
     * @return callable(ServerRequest,Response,callable):Response
44
     */
45
    public function asDoublePass(): callable
46
    {
47
        return function (ServerRequest $request, Response $response, callable $next): Response {
48
            return $this->run($request, fn($request) => $next($request, $response));
49
        };
50
    }
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
53
     * @param ServerRequest $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
54
     * @param callable      $handle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
55
     * @return Response
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
56
     */
57
    protected function run(ServerRequest $request, callable $handle): Response
58
    {
59
        $session = $request->getAttribute('session');
60
61
        if ($session !== null && !$session instanceof SessionInterface) {
62
            trigger_error("'session' attribute of server request isn't a session object", E_USER_WARNING);
63
            return $handle($request);
64
        }
65
66
        if ($session === null) {
67
            $session = $this->defaultSession;
68
            $request = $request->withAttribute('session', $session);
69
        }
70
71
        $session->start();
72
73
        try {
74
            return $handle($request);
75
        } finally {
76
            $session->stop();
77
        }
78
    }
79
}
80