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
Push — master ( 1b449a...2ed547 )
by Cees-Jan
01:50
created

SessionMiddleware::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1.0036

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 13
cp 0.8462
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 2
crap 1.0036
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 Throwable;
11
use function React\Promise\resolve;
12
13
final class SessionMiddleware
14
{
15
    const ATTRIBUTE_NAME = 'wyrihaximus.react.http.middleware.session';
16
17
    /**
18
     * @var string
19
     */
20
    private $cookieName;
21
22
    /**
23
     * @var CacheInterface
24
     */
25
    private $cache;
26
27
    /**
28
     * @param string         $cookieName
29
     * @param CacheInterface $cache
30
     */
31 1
    public function __construct(string $cookieName, CacheInterface $cache)
32
    {
33 1
        $this->cookieName = $cookieName;
34 1
        $this->cache = $cache;
35 1
    }
36
37 1
    public function __invoke(ServerRequestInterface $request, callable $next)
38
    {
39 1
        $id = $this->getId($request);
40
41
        return $this->cache->get($id)->otherwise(function () {
42
            return resolve([]);
43
        })->then(function ($sessionData) use ($next, $request, $id) {
44 1
            $session = new Session($sessionData);
45 1
            $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...
46
47 1
            return resolve($next($request))->then(function (ResponseInterface $response) use ($id, $session) {
48 1
                $this->cache->set($id, $session->getContents());
49 1
                $cookie = new SetCookie($this->cookieName, $id);
50 1
                $response = $cookie->addToResponse($response);
51
52 1
                return $response;
53 1
            });
54 1
        });
55
    }
56
57 1
    private function getId(ServerRequestInterface $request): string
58
    {
59 1
        $cookies = RequestCookies::createFromRequest($request);
60
61
        try {
62 1
            if ($cookies->has($this->cookieName)) {
63 1
                return $cookies->get($this->cookieName)->getValue();
64
            }
65
        } catch (Throwable $et) {
66
            // Do nothing, only a not found will be thrown so generating our own id now
67
        }
68
69
        return bin2hex(random_bytes(128));
70
    }
71
}
72