setServiceLocator($serviceLocator); } /** * @param File $file * @throws \common_Exception */ public function setUpload(File $file) { $hash = $this->getHashedKey($file); $this->set($hash, []); } /** * @param File $remote * @param string $local * @throws \common_Exception */ public function addLocalCopies(File $remote, $local) { $hash = $this->getHashedKey($remote); $current = $this->getLocalCopies($remote); $current[] = $local; $this->set($hash, $current); } /** * @param $file * @return array */ public function getLocalCopies(File $file) { $result = $this->get($this->getHashedKey($file)); return is_array($result) ? $result : []; } /** * @param File $file * @return string */ private function getHashedKey(File $file) { return md5($file->getPrefix()); } /** * @param File $file */ public function removeFiles(File $file) { $this->remove($this->getHashedKey($file)); } /** * @return \common_persistence_KeyValuePersistence */ private function getKvStorage() { /** @var PersistenceManager $pm */ $pm = $this->getServiceLocator()->get(PersistenceManager::SERVICE_ID); return $pm->getPersistenceById('default_kv'); } /** * @param $key * @param $val * @throws \common_Exception */ private function set($key, $val) { $key = $this->getPrefix() . $key; $this->getKvStorage()->set($key, json_encode($val)); } /** * @param $key */ private function remove($key) { $key = $this->getPrefix() . $key; $this->getKvStorage()->del($key); } /** * @param $key * @return mixed|null */ private function get($key) { $key = $this->getPrefix() . $key; if ($this->getKvStorage()->exists($key)) { return json_decode($this->getKvStorage()->get($key), true); } return null; } /** * @return string */ private function getPrefix() { return static::class; } }