These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 WyriHaximus\React\Http\Middleware\SessionId\RandomBytes; |
||
12 | use function React\Promise\resolve; |
||
13 | |||
14 | final class SessionMiddleware |
||
15 | { |
||
16 | const ATTRIBUTE_NAME = 'wyrihaximus.react.http.middleware.session'; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $cookieName = ''; |
||
22 | |||
23 | /** |
||
24 | * @var CacheInterface |
||
25 | */ |
||
26 | private $cache; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $cookieParams = []; |
||
32 | |||
33 | /** |
||
34 | * @var SessionIdInterface |
||
35 | */ |
||
36 | private $sessionId; |
||
37 | |||
38 | /** |
||
39 | * @param string $cookieName |
||
40 | * @param CacheInterface $cache |
||
41 | * @param array $cookieParams |
||
42 | */ |
||
43 | 9 | public function __construct( |
|
44 | string $cookieName, |
||
45 | CacheInterface $cache, |
||
46 | array $cookieParams = [], |
||
47 | SessionIdInterface $sessionId = null |
||
48 | ) { |
||
49 | 9 | $this->cookieName = $cookieName; |
|
50 | 9 | $this->cache = $cache; |
|
51 | 9 | $this->cookieParams = $cookieParams; |
|
52 | |||
53 | 9 | if ($sessionId === null) { |
|
54 | 9 | $sessionId = new RandomBytes(); |
|
55 | } |
||
56 | 9 | $this->sessionId = $sessionId; |
|
57 | 9 | } |
|
58 | |||
59 | 9 | public function __invoke(ServerRequestInterface $request, callable $next) |
|
60 | { |
||
61 | 9 | $id = $this->getId($request); |
|
62 | |||
63 | 8 | return $this->cache->get($id)->otherwise(function () { |
|
64 | 8 | return resolve([]); |
|
65 | })->then(function ($sessionData) use ($next, $request, $id) { |
||
66 | 9 | $session = new Session($sessionData); |
|
67 | 9 | $request = $request->withAttribute(self::ATTRIBUTE_NAME, $session); |
|
68 | |||
69 | 9 | return resolve($next($request))->then(function (ResponseInterface $response) use ($id, $session) { |
|
70 | 9 | $this->cache->set($id, $session->getContents()); |
|
71 | 9 | $cookie = new SetCookie($this->cookieName, $id, ...$this->cookieParams); |
|
0 ignored issues
–
show
|
|||
72 | 9 | $response = $cookie->addToResponse($response); |
|
73 | |||
74 | 9 | return $response; |
|
75 | 9 | }); |
|
76 | 9 | }); |
|
77 | } |
||
78 | |||
79 | 9 | private function getId(ServerRequestInterface $request): string |
|
80 | { |
||
81 | 9 | $cookies = RequestCookies::createFromRequest($request); |
|
82 | |||
83 | try { |
||
84 | 9 | if ($cookies->has($this->cookieName)) { |
|
85 | 9 | return $cookies->get($this->cookieName)->getValue(); |
|
86 | } |
||
87 | } catch (Throwable $et) { |
||
88 | // Do nothing, only a not found will be thrown so generating our own id now |
||
89 | } |
||
90 | |||
91 | 8 | return $this->sessionId->generate(); |
|
92 | } |
||
93 | } |
||
94 |
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: