<?php
 class vB_Undo_Log { use vB_Trait_NoSerialize; private vB_Db_Assertor $db; private vB_Interface_Timenow $timenowProvider; public function __construct(vB_Db_Assertor $db, vB_Interface_Timenow $timenowProvider) { $this->db = $db; $this->timenowProvider = $timenowProvider; } public function register(vB_Undo_Item $undo, int $deleteafter = 86400) : int|false { $timenow = $this->getTimeNow(); return $this->db->insert('undolog', [ 'class' => $undo->getClass(), 'userid' => $undo->getUserid(), 'data' => $undo->getData(), 'created' => $timenow, 'deleteafter' => $timenow + $deleteafter, ]); } private function getTimeNow() : int { return $this->timenowProvider->getTimeNow(); } public function deleteExpired() : void { $cutoff = $this->getTimeNow(); $this->db->delete('undolog', [ ['field'=>'deleteafter', 'value' => $cutoff, vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_LTE], ]); } public function getUndoItem(int $undoid) : ?vB_Undo_Item { $row = $this->db->getRow('undolog', [ 'undoid' => $undoid, ]); if (empty($row)) { return null; } $currentEvent = vB_Undo_Item::restoreFromData($undoid, $row['data']); return $currentEvent; } public function runUndoItem(vB_Undo_Item $item) : mixed { $result = $item->run(); $undoid = $item->getUndoid(); if ($undoid) { $this->db->delete('undolog', ['undoid' => $undoid]); } return $result; } } 