1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Koded package. |
||
5 | * |
||
6 | * (c) Mihail Binev <[email protected]> |
||
7 | * |
||
8 | * Please view the LICENSE distributed with this source code |
||
9 | * for the full copyright and license information. |
||
10 | * |
||
11 | */ |
||
12 | |||
13 | namespace Koded\Session\Handler; |
||
14 | |||
15 | use Koded\Session\SessionConfiguration; |
||
16 | use SessionHandlerInterface; |
||
17 | use function Koded\Caching\simple_cache_factory; |
||
18 | |||
19 | |||
20 | final class RedisHandler implements SessionHandlerInterface |
||
21 | { |
||
22 | /** @var int */ |
||
23 | protected $ttl; |
||
24 | |||
25 | /** @var \Redis */ |
||
26 | private $client; |
||
27 | |||
28 | 21 | public function __construct(SessionConfiguration $settings) |
|
29 | { |
||
30 | 21 | $this->ttl = (int)$settings->get('gc_maxlifetime', ini_get('session.gc_maxlifetime')); |
|
31 | 21 | $this->client = simple_cache_factory('redis', $this->configuration($settings))->client(); |
|
0 ignored issues
–
show
|
|||
32 | 21 | } |
|
33 | |||
34 | 21 | public function close(): bool |
|
35 | { |
||
36 | 21 | return true; |
|
37 | } |
||
38 | |||
39 | 1 | public function destroy($sessionId): bool |
|
40 | { |
||
41 | 1 | return $this->client->del($sessionId) > 0; |
|
42 | } |
||
43 | |||
44 | /** |
||
45 | * @codeCoverageIgnore |
||
46 | */ |
||
47 | public function gc($maxLifetime): bool |
||
48 | { |
||
49 | return true; |
||
50 | } |
||
51 | |||
52 | 21 | public function read($sessionId): string |
|
53 | { |
||
54 | 21 | return $this->client->get($sessionId) ?: ''; |
|
55 | } |
||
56 | |||
57 | 21 | public function write($sessionId, $sessionData): bool |
|
58 | { |
||
59 | 21 | return $this->client->setex($sessionId, $this->ttl, $sessionData); |
|
60 | } |
||
61 | |||
62 | 21 | public function open($savePath, $sessionId): bool |
|
63 | { |
||
64 | 21 | return true; |
|
65 | } |
||
66 | |||
67 | 21 | private function configuration(SessionConfiguration $settings): array |
|
68 | { |
||
69 | return [ |
||
70 | 21 | 'prefix' => (string)$settings->get('prefix', 'session:'), |
|
71 | 21 | 'host' => (string)$settings->get('host'), |
|
72 | 21 | 'port' => (int)$settings->get('port', 6379), |
|
73 | 21 | 'timeout' => (float)$settings->get('timeout', 0.0), |
|
74 | 21 | 'retry' => (int)$settings->get('retry', 0), |
|
75 | 21 | 'db' => (int)$settings->get('db', 0), |
|
76 | ]; |
||
77 | } |
||
78 | } |
||
79 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.