<?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\Service\Config;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Psr\Log\LoggerInterface;

class HolePunchConfig
{
    const FROM_CACHE           = 'm_from_cache';
    const FIND_DATA            = 'm_find_data_by_pattern_data';
    const CMS_BLOCK_EXCLUDE    = 'm_cms_block_exclude';
    const WIDGET_BLOCK_EXCLUDE = 'm_widget_block_exclude';
    /**
     * @var SerializerInterface
     */
    private $serializer;
    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * HolePunchConfig constructor.
     * @param ScopeConfigInterface $scopeConfig
     * @param SerializerInterface $serializer
     * @param LoggerInterface $logger
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        SerializerInterface $serializer,
        LoggerInterface $logger
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->serializer  = $serializer;
        $this->logger      = $logger;
    }

    /**
     * @param int|null $store
     * @return array
     */
    public function getTemplates($store = null)
    {
        $conf = $this->scopeConfig->getValue(
            'cache_warmer/fpc_hole_punch/hole_punch_templates',
            ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
            $store
        );

        if ($conf === null || $conf === false || $conf === '' || $conf === '[]') {
            return [];
        }

        try {
            $conf = $this->serializer->unserialize((string)$conf);
        } catch (\InvalidArgumentException $e) {
            $this->logger->warning('CacheWarmer: failed to unserialize hole_punch_templates config', [
                'error' => $e->getMessage(),
            ]);
            $conf = [];
        }

        if (is_object($conf)) {
            $conf = (array)$conf;
            foreach ($conf as $key => $value) {
                if (is_object($value)) {
                    $conf[$key] = (array)$value;
                }
            }
        }

        if (is_array($conf)) {
            foreach ($conf as $confKey => $confData) {
                if (!$confData['template'] || !$confData['block']) {
                    unset($conf[$confKey]);
                }
            }
        }

        return $conf;
    }
}
