|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TinyAuth\Utility; |
| 4 | + |
| 5 | +use ArrayAccess; |
| 6 | +use Cake\Cache\Cache; |
| 7 | +use Cake\Core\Configure; |
| 8 | +use Cake\Core\Exception\CakeException; |
| 9 | +use Cake\Core\StaticConfigTrait; |
| 10 | + |
| 11 | +/** |
| 12 | + * TinyAuth cache wrapper around session cache engine(s). |
| 13 | + */ |
| 14 | +class SessionCache { |
| 15 | + |
| 16 | + use StaticConfigTrait; |
| 17 | + |
| 18 | + protected static array $_defaultConfig = [ |
| 19 | + 'cache' => 'default', |
| 20 | + 'prefix' => 'auth_user_', |
| 21 | + ]; |
| 22 | + |
| 23 | + /** |
| 24 | + * Clears all session user info based on prefix |
| 25 | + * |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + public static function clear(): void { |
| 29 | + $config = static::prepareConfig(); |
| 30 | + static::assertValidCacheSetup($config); |
| 31 | + |
| 32 | + if (!empty($config['groups'])) { |
| 33 | + foreach ((array)$config['groups'] as $group) { |
| 34 | + Cache::clearGroup($group, $config['cache']); |
| 35 | + } |
| 36 | + |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + Cache::clear($config['cache']); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param string|int $userId |
| 45 | + * @param \ArrayAccess|array $data |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public static function write(int|string $userId, ArrayAccess|array $data): void { |
| 50 | + $config = static::prepareConfig(); |
| 51 | + |
| 52 | + Cache::write(static::key($userId), $data, $config['cache']); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param string|int $userId |
| 57 | + * |
| 58 | + * @return \ArrayAccess|array|null |
| 59 | + */ |
| 60 | + public static function read(int|string $userId): ArrayAccess|array|null { |
| 61 | + $config = static::prepareConfig(); |
| 62 | + |
| 63 | + return Cache::read(static::key($userId), $config['cache']) ?: null; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param string|int $userId |
| 68 | + * |
| 69 | + * @return bool |
| 70 | + */ |
| 71 | + public static function delete(int|string $userId): bool { |
| 72 | + $config = static::prepareConfig(); |
| 73 | + |
| 74 | + return Cache::delete(static::key($userId), $config['cache']); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param string|int $userId |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + public static function key(int|string $userId): string { |
| 82 | + $config = static::prepareConfig(); |
| 83 | + |
| 84 | + static::assertValidCacheSetup($config); |
| 85 | + |
| 86 | + return $config['prefix'] . $userId; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param array<string, mixed> $config |
| 91 | + * @throws \Cake\Core\Exception\CakeException |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + protected static function assertValidCacheSetup(array $config): void { |
| 95 | + if (!in_array($config['cache'], Cache::configured(), true)) { |
| 96 | + throw new CakeException(sprintf('Invalid or not configured TinyAuth cache `%s`', $config['cache'])); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return array<string, mixed> |
| 102 | + */ |
| 103 | + protected static function prepareConfig(): array { |
| 104 | + $defaultConfig = static::$_defaultConfig; |
| 105 | + |
| 106 | + return (array)Configure::read('TinyAuth') + $defaultConfig; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments