*/ class LockService extends ConfigurableService { const SERVICE_ID = 'generis/LockService'; const OPTION_PERSISTENCE_CLASS = 'persistence_class'; const OPTION_PERSISTENCE_OPTIONS = 'persistence_options'; /** @var Factory */ private $factory; /** @var StoreInterface */ private $store; /** * @return Factory * @throws \common_exception_FileReadFailedException * @throws \common_exception_InconsistentData * @throws \common_exception_NotImplemented */ public function getLockFactory() { if ($this->factory === null) { $this->factory = new Factory(new RetryTillSaveStore($this->getStore())); } return $this->factory; } /** * @return StoreInterface * @throws \common_exception_FileReadFailedException * @throws \common_exception_InconsistentData * @throws \common_exception_NotImplemented */ private function getStore() { if ($this->store === null) { $persistenceClass = $this->getOption(self::OPTION_PERSISTENCE_CLASS); $persistenceOptions = $this->getOption(self::OPTION_PERSISTENCE_OPTIONS); switch ($persistenceClass) { case FlockStore::class: $this->store = $this->getFlockStore($persistenceOptions); break; case NoLockStorage::class: $this->store = $this->getNoLockStore(); break; case RedisStore::class: $this->store = $this->getRedisStore($persistenceOptions); break; default: throw new \common_exception_NotImplemented('configured storage is not supported'); } } return $this->store; } /** * Install store. Should be called after registration of lock service */ public function install() { } /** * @param $persistenceId * @return RedisStore * @throws \common_exception_InconsistentData */ private function getRedisStore($persistenceId) { $persistenceManager = $this->getServiceLocator()->get(\common_persistence_Manager::SERVICE_ID); $persistence = $persistenceManager->getPersistenceById($persistenceId); if (!$persistence->getDriver() instanceof \common_persistence_PhpRedisDriver) { throw new \common_exception_InconsistentData('Not redis persistence id configured for RedisStore'); } return new RedisStore($persistence->getDriver()->getConnection()); } /** * @param $filePath * @return FlockStore * @throws \common_exception_FileReadFailedException */ private function getFlockStore($filePath) { if (is_dir($filePath) && is_writable($filePath)) { return new FlockStore($filePath); } throw new \common_exception_FileReadFailedException('Lock store path is not writable'); } /** * @return NoLockStorage */ private function getNoLockStore() { return new NoLockStorage(); } }