<?php
/**
 * Mirasvit
 *
 * This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
 * Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
 * If you wish to customize this module for your needs.
 * Please refer to http://www.magentocommerce.com for more information.
 *
 * @category  Mirasvit
 * @package   mirasvit/module-cache-warmer
 * @version   1.11.13
 * @copyright Copyright (C) 2026 Mirasvit (https://mirasvit.com/)
 */



namespace Mirasvit\CacheWarmer\Helper;


use Magento\Config\Model\ResourceModel\Config as ConfigWriter;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory as ConfigCollectionFactory;
use Mirasvit\CacheWarmer\Model\Config;

class LockManager
{
    const PATH_PREFIX = "mst_lock/";

    const MIN_LOCK_TIMEOUT = 1800;

    private $scopeConfig;

    private $configWriter;

    private $configCollectionFactory;

    private $config;

    public function __construct(
        ScopeConfigInterface $scopeConfig,
        ConfigWriter $configWriter,
        ConfigCollectionFactory $factory,
        Config $config
    ) {
        $this->scopeConfig             = $scopeConfig;
        $this->configWriter            = $configWriter;
        $this->configCollectionFactory = $factory;
        $this->config                  = $config;
    }

    /**
     * @param string $name
     *
     * @return bool
     */
    public function lock($name)
    {
        $lockValue = $this->getLockValue($name);

        if ($lockValue > 0 && !$this->isStale($lockValue)) {
            return false;
        }

        $lockPath = self::PATH_PREFIX . $name;
        $this->configWriter->saveConfig($lockPath, time(), ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 0);

        return true;
    }

    /**
     * @param string $name
     *
     * @return bool
     */
    public function unlock($name)
    {
        if (!$this->isLocked($name)) {
            return false;
        }

        $lockPath = self::PATH_PREFIX . $name;
        $this->configWriter->saveConfig($lockPath, 0, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 0);

        return true;
    }

    /**
     * @param string $name
     *
     * @return bool
     */
    public function isLocked($name)
    {
        $lockValue = $this->getLockValue($name);

        return $lockValue > 0 && !$this->isStale($lockValue);
    }

    /**
     * @param string $name
     *
     * @return int
     */
    private function getLockValue($name)
    {
        $lockPath = self::PATH_PREFIX . $name;

        // using collection to avoid read lock status from cache
        $collection = $this->configCollectionFactory->create();
        $config = $collection->addScopeFilter('default', 0, $lockPath)->getFirstItem();

        return (int)$config->getData('value');
    }

    /**
     * @param int $lockValue
     *
     * @return bool
     */
    private function isStale($lockValue)
    {
        $timeout = max($this->config->getJobRunThreshold() * 3, self::MIN_LOCK_TIMEOUT);

        return (time() - $lockValue) > $timeout;
    }
}
