locksDirPath = $lockPath; } else { $this->locksDirPath = sys_get_temp_dir(); } } catch ( \Exception $exception ) { PaynowLogger::error( 'Error occurred when creating locking dir.', [ 'exception' => $exception->getMessage(), 'file' => $exception->getFile(), 'line' => $exception->getLine() ] ); $this->locksDirPath = sys_get_temp_dir(); } if ( ! is_writable( $this->locksDirPath ) ) { PaynowLogger::error( 'Locking mechanism disabled, no locks path available.' ); } $this->lockEnabled = is_writable( $this->locksDirPath ); } /** * @param $externalId * @return bool */ public function checkAndCreate( $externalId ) { if ( ! $this->lockEnabled ) { return false; } $lockFilePath = $this->generateLockPath( $externalId ); $lockExists = file_exists( $lockFilePath ); if ( $lockExists && ( filemtime( $lockFilePath ) + self::LOCKED_TIME ) > time() ) { return true; } else { $this->create( $externalId, $lockExists ); return false; } } /** * @param $externalId * @return void */ public function delete( $externalId ) { if ( empty( $externalId ) ) { return; } $lockFilePath = $this->generateLockPath( $externalId ); if ( file_exists( $lockFilePath ) ) { unlink( $lockFilePath ); } } /** * @param $externalId * @param $lockExists * @return void */ private function create( $externalId, $lockExists ) { $lockPath = $this->generateLockPath( $externalId ); if ( $lockExists ) { touch( $lockPath ); } else { $fileSaved = @file_put_contents($lockPath, ''); if ( false === $fileSaved ) { PaynowLogger::error( 'Locking mechanism disabled, no locks path available.', [ 'external_id' => $externalId, 'lock_path' => $lockPath, ] ); } } } /** * @param $externalId * @return string */ private function generateLockPath( $externalId ) { return $this->locksDirPath . DIRECTORY_SEPARATOR . self::LOCKS_PREFIX . md5( $externalId ) . '.lock'; } }