Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
16.28% |
7 / 43 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
CacheService | |
16.28% |
7 / 43 |
|
0.00% |
0 / 7 |
333.43 | |
0.00% |
0 / 1 |
connect | |
25.00% |
2 / 8 |
|
0.00% |
0 / 1 |
10.75 | |||
storeAuthentication | |
22.22% |
2 / 9 |
|
0.00% |
0 / 1 |
7.23 | |||
removeAuthentication | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
storeFile | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
getStatusFilesCache | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
getFailedLogins | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
3.19 | |||
addFailedLogin | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 |
1 | <?php /** @noinspection PhpUnhandledExceptionInspection */ |
2 | |
3 | class CacheService { |
4 | private static Redis|null $redis = null; |
5 | |
6 | private static function connect(): bool { |
7 | if (!SystemConfig::$cacheService_host) { |
8 | return false; |
9 | } |
10 | if (!isset(self::$redis)) { |
11 | try { |
12 | self::$redis = new Redis(); |
13 | self::$redis->connect(SystemConfig::$cacheService_host); |
14 | } catch (RedisException $exception) { |
15 | throw new Exception("Could not reach Cache-Service: " . $exception->getMessage()); |
16 | } |
17 | } |
18 | return true; |
19 | } |
20 | |
21 | static function storeAuthentication(PersonSession $personSession): void { |
22 | if (!self::connect()) { |
23 | return; |
24 | } |
25 | self::$redis->set( |
26 | 'group-token:' . $personSession->getLoginSession()->getGroupToken(), |
27 | $personSession->getLoginSession()->getLogin()->getWorkspaceId(), |
28 | $personSession->getLoginSession()->getLogin()->getValidTo() |
29 | ? $personSession->getLoginSession()->getLogin()->getValidTo() - TimeStamp::now() |
30 | : 24*60*60 |
31 | ); |
32 | } |
33 | |
34 | public static function removeAuthentication(PersonSession $personSession): void { |
35 | if (!self::connect()) { |
36 | return; |
37 | } |
38 | self::$redis->del('group-token:' . $personSession->getPerson()->getToken()); |
39 | } |
40 | |
41 | public static function storeFile(string $filePath): void { |
42 | if (!SystemConfig::$cacheService_includeFiles) { |
43 | return; |
44 | } |
45 | if (!self::connect()) { |
46 | return; |
47 | } |
48 | if (self::$redis->exists("file:$filePath")) { |
49 | self::$redis->expire("file:$filePath", 24*60*60); |
50 | } else { |
51 | try { |
52 | self::$redis->set("file:$filePath", file_get_contents(DATA_DIR . $filePath), 24*60*60); |
53 | } catch (RedisException $e) { |
54 | error_log('Cache exhausted: ' . $filePath); |
55 | } |
56 | } |
57 | } |
58 | |
59 | static function getStatusFilesCache(): string { |
60 | if (!SystemConfig::$cacheService_host or !SystemConfig::$cacheService_includeFiles) { |
61 | return 'off'; |
62 | } |
63 | try { |
64 | self::connect(); |
65 | } catch (RedisException $exception) { |
66 | return 'unreachable'; |
67 | } |
68 | return 'on'; |
69 | } |
70 | |
71 | public static function getFailedLogins(string $name): int { |
72 | if (!self::connect()) return 0; |
73 | $loginsFailed = self::$redis->get("login-failed:$name:"); |
74 | return (int) $loginsFailed; |
75 | } |
76 | |
77 | public static function addFailedLogin(string $name): void { |
78 | if (!self::connect()) return; |
79 | $loginsFailed = self::getFailedLogins($name); |
80 | $loginsFailed++; |
81 | $expiration = SystemConfig::$debug_fastLoginReuse ? 5 : 30*60; |
82 | self::$redis->set("login-failed:$name:", $loginsFailed, $expiration); |
83 | } |
84 | } |