Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SessionHandler
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 destroyNative
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Middleware;
6
7use Psr\Http\Message\ResponseInterface;
8use Psr\Http\Message\ServerRequestInterface;
9use Psr\Http\Server\MiddlewareInterface;
10use Psr\Http\Server\RequestHandlerInterface;
11use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
12use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
13
14class SessionHandler implements MiddlewareInterface
15{
16    public function __construct(protected SessionStorageInterface $session)
17    {
18    }
19
20    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
21    {
22        $return = $handler->handle($request);
23
24        $cookies = $request->getCookieParams();
25        if (
26            // Is api (accessible) path
27            $request->getAttribute('route-api-accessible')
28            // Uses native PHP session
29            && $this->session instanceof NativeSessionStorage
30            // No session cookie was sent on request
31            && !isset($cookies[$this->session->getName()])
32        ) {
33            $this->destroyNative();
34        }
35
36        return $return;
37    }
38
39    /**
40     * @codeCoverageIgnore
41     */
42    protected function destroyNative(): bool
43    {
44        return session_destroy();
45    }
46}