GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#2)
by Cees-Jan
03:11 queued 01:16
created

SessionMiddleware::fetchSessionFromRequest()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 9
cts 11
cp 0.8182
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 11
nc 6
nop 1
crap 3.054
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use HansOtt\PSR7Cookies\RequestCookies;
6
use HansOtt\PSR7Cookies\SetCookie;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use React\Cache\CacheInterface;
10
use React\Promise\PromiseInterface;
11
use Throwable;
12
use WyriHaximus\React\Http\Middleware\SessionId\RandomBytes;
13
use function React\Promise\resolve;
14
15
final class SessionMiddleware
16
{
17
    const ATTRIBUTE_NAME = 'wyrihaximus.react.http.middleware.session';
18
19
    /**
20
     * @var string
21
     */
22
    private $cookieName = '';
23
24
    /**
25
     * @var CacheInterface
26
     */
27
    private $cache;
28
29
    /**
30
     * @var array
31
     */
32
    private $cookieParams = [];
33
34
    /**
35
     * @var SessionIdInterface
36
     */
37
    private $sessionId;
38
39
    /**
40
     * @param string         $cookieName
41
     * @param CacheInterface $cache
42
     * @param array          $cookieParams
43
     */
44 12
    public function __construct(
45
        string $cookieName,
46
        CacheInterface $cache,
47
        array $cookieParams = [],
48
        SessionIdInterface $sessionId = null
49
    ) {
50 12
        $this->cookieName = $cookieName;
51 12
        $this->cache = $cache;
52 12
        $this->cookieParams = $cookieParams;
53
54 12
        if ($sessionId === null) {
55 12
            $sessionId = new RandomBytes();
56
        }
57 12
        $this->sessionId = $sessionId;
58 12
    }
59
60
    public function __invoke(ServerRequestInterface $request, callable $next)
61
    {
62 12
        return $this->fetchSessionFromRequest($request)->then(function (Session $session) use ($next, $request) {
63 12
            $requestSessionId = $session->getId();
64 12
            $request = $request->withAttribute(self::ATTRIBUTE_NAME, $session);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $request, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
65
66 12
            return resolve(
67 12
                $next($request)
68 12
            )->then(function (ResponseInterface $response) use ($session, $requestSessionId) {
69 12
                $this->updateCache($session->getId(), $requestSessionId, $session->getContents());
70
71 12
                $cookie = new SetCookie($this->cookieName, $session->getId(), ...$this->cookieParams);
0 ignored issues
show
Documentation introduced by
$this->cookieParams is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72 12
                $response = $cookie->addToResponse($response);
73
74 12
                return $response;
75 12
            });
76 12
        });
77
    }
78
79 12
    private function fetchSessionFromRequest(ServerRequestInterface $request): PromiseInterface
80
    {
81 12
        $id = '';
82 12
        $cookies = RequestCookies::createFromRequest($request);
83
84
        try {
85 12
            if (!$cookies->has($this->cookieName)) {
86 10
                return resolve(new Session($id, [], $this->sessionId));
87
            }
88
89 2
            $id = $cookies->get($this->cookieName)->getValue();
90
91 2
            return $this->fetchSessionDataFromCache($id)->then(function (array $sessionData) use ($id) {
92 2
                return new Session($id, $sessionData, $this->sessionId);
93 2
            });
94
        } catch (Throwable $et) {
95
            // Do nothing, only a not found will be thrown so generating our own id now
96
        }
97
98
        return resolve(new Session($id, [], $this->sessionId));
99
    }
100
101
    private function fetchSessionDataFromCache(string $id): PromiseInterface
102
    {
103 2
        return $this->cache->get($id)->otherwise(function () {
104 2
            return resolve([]);
105 2
        });
106
    }
107
108 12
    private function updateCache(string $currentSessionId, string $requestSessionId, array $contents)
109
    {
110 12
        if ($currentSessionId !== '') {
111 2
            return $this->cache->set($currentSessionId, $contents);
112
        }
113
114 10
        if ($currentSessionId === '' && $requestSessionId !== '') {
115 1
            return $this->cache->remove($requestSessionId);
116
        }
117 9
    }
118
}
119