Completed
Push — master ( b002ee...8b7e77 )
by Alexander
02:11
created

SessionMiddleware   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 97.06%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 33
c 1
b 0
f 1
dl 0
loc 66
rs 10
ccs 33
cts 34
cp 0.9706
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getSidFromRequest() 0 4 1
B commitSession() 0 39 7
A process() 0 10 2
1
<?php
2
namespace Yiisoft\Yii\Web\Session;
3
4
use Psr\Http\Message\ResponseInterface;
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Server\MiddlewareInterface;
7
use Psr\Http\Server\RequestHandlerInterface;
8
use Yiisoft\Yii\Web\Cookie;
9
10
class SessionMiddleware implements MiddlewareInterface
11
{
12
    private $session;
13
14 4
    public function __construct(SessionInterface $session)
15
    {
16 4
        $this->session = $session;
17
    }
18
19 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
20
    {
21
        try {
22 4
            $response = $handler->handle($request);
23 1
        } catch (\Throwable $e) {
24 1
            $this->session->discard();
25 1
            throw $e;
26
        }
27
28 3
        return $this->commitSession($request, $response);
29
    }
30
31 3
    private function commitSession(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
32
    {
33 3
        if (!$this->session->isActive()) {
34 1
            return $response;
35
        }
36
37 2
        $this->session->close();
38
39 2
        $currentSid = $this->session->getID();
40
41
        // SID changed, neeed to send new cookie
42 2
        if ($this->getSidFromRequest($request) !== $currentSid) {
43 2
            $cookieParameters = $this->session->getCookieParameters();
44
45 2
            $cookieDomain = $cookieParameters['domain'];
46 2
            if (empty($cookieDomain)) {
47 1
                $cookieDomain = $request->getUri()->getHost();
48
            }
49
50 2
            $useSecureCookie = $cookieParameters['secure'];
51 2
            if ($useSecureCookie && $request->getUri()->getScheme() !== 'https') {
52 1
                throw new SessionException('"cookie_secure" is on but connection is not secure. Either set Session "cookie_secure" option to "0" or make connection secure');
53
            }
54
55 1
            $sessionCookie = (new Cookie($this->session->getName(), $currentSid))
0 ignored issues
show
Bug introduced by
It seems like $currentSid can also be of type null; however, parameter $value of Yiisoft\Yii\Web\Cookie::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            $sessionCookie = (new Cookie($this->session->getName(), /** @scrutinizer ignore-type */ $currentSid))
Loading history...
56 1
                ->path($cookieParameters['path'])
57 1
                ->domain($cookieDomain)
58 1
                ->httpOnly($cookieParameters['httponly'])
59 1
                ->secure($useSecureCookie)
60 1
                ->sameSite($cookieParameters['samesite'] ?? Cookie::SAME_SITE_LAX);
61
62 1
            if ($cookieParameters['lifetime'] > 0) {
63 1
                $sessionCookie = $sessionCookie->validFor(new \DateInterval('PT' . $cookieParameters['lifetime'] . 'S'));
64
            }
65
66 1
            return $sessionCookie->addToResponse($response);
67
        }
68
69
        return $response;
70
    }
71
72 2
    private function getSidFromRequest(ServerRequestInterface $request): ?string
73
    {
74 2
        $cookies = $request->getCookieParams();
75 2
        return $cookies[$this->session->getName()] ?? null;
76
    }
77
}
78