dude920228 /
php-cache
| 1 | <?php |
||
| 2 | |||
| 3 | namespace PhpCache\CacheServer; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Description of ActionHandler. |
||
| 7 | * |
||
| 8 | * @author dude920228 |
||
| 9 | */ |
||
| 10 | class ActionHandler |
||
| 11 | { |
||
| 12 | public function __invoke( |
||
| 13 | CacheServer $server, |
||
| 14 | array $data, |
||
| 15 | $connection |
||
| 16 | ): ?bool { |
||
| 17 | $action = $data['action']; |
||
| 18 | $functionName = 'handle'.ucfirst($action); |
||
| 19 | if (!method_exists($this, $functionName)) { |
||
| 20 | return false; |
||
| 21 | } |
||
| 22 | |||
| 23 | return call_user_func_array([$this, $functionName], [ |
||
| 24 | $server, |
||
| 25 | $data, |
||
| 26 | $connection, |
||
| 27 | ]); |
||
| 28 | } |
||
| 29 | |||
| 30 | private function handleSet( |
||
| 31 | CacheServer $server, |
||
| 32 | array $data, |
||
| 33 | $connection |
||
| 34 | ):bool { |
||
| 35 | $package = $data['message']; |
||
| 36 | if ($server->getEventListener()) { |
||
| 37 | $package = $server->getEventListener()->onSet($data['key'], $package); |
||
| 38 | } |
||
| 39 | $success = $server->getBucket()->store($data['key'], $package); |
||
| 40 | |||
| 41 | return $success; |
||
| 42 | } |
||
| 43 | |||
| 44 | private function handleGet( |
||
| 45 | CacheServer $server, |
||
| 46 | array $data, |
||
| 47 | $connection |
||
| 48 | ): bool { |
||
| 49 | $key = $data['key']; |
||
| 50 | $package = $server->getBucket()->get($key); |
||
| 51 | if (!$package) { |
||
| 52 | return false; |
||
| 53 | } |
||
| 54 | if ($server->getEventListener()) { |
||
| 55 | $package = $server->getEventListener()->onGet($key, $package); |
||
| 56 | } |
||
| 57 | $dataToSend = serialize($package); |
||
| 58 | $server->getIOHandler()->writeToSocket($connection, $dataToSend); |
||
| 59 | |||
| 60 | return true; |
||
| 61 | } |
||
| 62 | |||
| 63 | private function handleDelete( |
||
| 64 | CacheServer $server, |
||
| 65 | array $data, |
||
| 66 | $connection |
||
| 67 | ): bool { |
||
| 68 | $key = $data['key']; |
||
| 69 | $package = $server->getBucket()->get($key); |
||
| 70 | if (!$package) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | if ($server->getEventListener()) { |
||
| 74 | $package = $server->getEventListener()->onDelete($key, $package); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 75 | } |
||
| 76 | $success = $server->getBucket()->delete($key); |
||
| 77 | |||
| 78 | return $success; |
||
| 79 | } |
||
| 80 | |||
| 81 | private function handleGetEntries( |
||
| 82 | CacheServer $server, |
||
| 83 | array $data, |
||
| 84 | $connection |
||
| 85 | ): bool { |
||
| 86 | $entries = $server->getBucket()->getEntries(); |
||
| 87 | $entriesFormatted = []; |
||
| 88 | foreach ($entries as $key => $value) { |
||
| 89 | $entriesFormatted[$key] = gzuncompress($value['content']); |
||
| 90 | } |
||
| 91 | $dataToSend = serialize($entriesFormatted); |
||
| 92 | $server->getIOHandler()->writeToSocket($connection, $dataToSend); |
||
| 93 | |||
| 94 | return true; |
||
| 95 | } |
||
| 96 | |||
| 97 | private function handleQuit( |
||
| 98 | CacheServer $server, |
||
| 99 | array $data, |
||
| 100 | $connection |
||
| 101 | ): void { |
||
| 102 | $server->close(); |
||
| 103 | } |
||
| 104 | } |
||
| 105 |