From 4306b184c4471825f916d895b047ed03fdf58985 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 16 Jan 2017 12:31:08 +0100 Subject: History mechanism Use case: rest API service * saved by default in data/history * same format as datastore.php * traced events: * save/edit/delete link * change settings or plugins settings * rename tag --- application/History.php | 183 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 application/History.php (limited to 'application/History.php') diff --git a/application/History.php b/application/History.php new file mode 100644 index 00000000..c06067df --- /dev/null +++ b/application/History.php @@ -0,0 +1,183 @@ +historyFilePath = $historyFilePath; + if ($retentionTime !== null) { + $this->retentionTime = $retentionTime; + } + $this->check(); + $this->read(); + } + + /** + * Add Event: new link. + * + * @param array $link Link data. + */ + public function addLink($link) + { + $this->addEvent(self::CREATED, $link['id']); + } + + /** + * Add Event: update existing link. + * + * @param array $link Link data. + */ + public function updateLink($link) + { + $this->addEvent(self::UPDATED, $link['id']); + } + + /** + * Add Event: delete existing link. + * + * @param array $link Link data. + */ + public function deleteLink($link) + { + $this->addEvent(self::DELETED, $link['id']); + } + + /** + * Add Event: settings updated. + */ + public function updateSettings() + { + $this->addEvent(self::SETTINGS); + } + + /** + * Save a new event and write it in the history file. + * + * @param string $status Event key, should be defined as constant. + * @param mixed $id Event item identifier (e.g. link ID). + */ + protected function addEvent($status, $id = null) + { + $item = [ + 'event' => $status, + 'datetime' => (new DateTime())->format(DateTime::ATOM), + 'id' => $id !== null ? $id : '', + ]; + $this->history = array_merge([$item], $this->history); + $this->write(); + } + + /** + * Check that the history file is writable. + * Create the file if it doesn't exist. + * + * @throws Exception if it isn't writable. + */ + protected function check() + { + if (! is_file($this->historyFilePath)) { + FileUtils::writeFlatDB($this->historyFilePath, []); + } + + if (! is_writable($this->historyFilePath)) { + throw new Exception('History file isn\'t readable or writable'); + } + } + + /** + * Read JSON history file. + */ + protected function read() + { + $this->history = FileUtils::readFlatDB($this->historyFilePath, []); + if ($this->history === false) { + throw new Exception('Could not parse history file'); + } + } + + /** + * Write JSON history file and delete old entries. + */ + protected function write() + { + $comparaison = new DateTime('-'. $this->retentionTime . ' seconds'); + foreach ($this->history as $key => $value) { + if (DateTime::createFromFormat(DateTime::ATOM, $value['datetime']) < $comparaison) { + unset($this->history[$key]); + } + } + FileUtils::writeFlatDB($this->historyFilePath, array_values($this->history)); + } + + /** + * Get the History. + * + * @return array + */ + public function getHistory() + { + return $this->history; + } +} -- cgit v1.2.3