<?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\Controller\Adminhtml;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Mirasvit\CacheWarmer\Api\Repository\JobRepositoryInterface;
use Mirasvit\CacheWarmer\Api\Service\JobServiceInterface;
use Mirasvit\CacheWarmer\Model\Config;
use Mirasvit\CacheWarmer\Model\ResourceModel\Job\CollectionFactory;

abstract class AbstractJob extends Action
{
    protected $jobRepository;

    protected $jobService;

    protected $collectionFactory;

    protected $context;

    protected $resultFactory;
    
    protected $config;

    public function __construct(
        JobRepositoryInterface $jobRepository,
        JobServiceInterface $jobService,
        CollectionFactory $collectionFactory,
        Config $config,
        Context $context
    ) {
        $this->jobRepository     = $jobRepository;
        $this->jobService        = $jobService;
        $this->collectionFactory = $collectionFactory;

        $this->config        = $config;
        $this->context       = $context;
        $this->resultFactory = $context->getResultFactory();

        parent::__construct($context);
    }

    /**
     * @param mixed $resultPage
     * @return mixed
     */
    protected function initPage($resultPage)
    {
        if (!$this->config->isEnabled()) {
            $this->messageManager->addWarningMessage(__('The Cache Warmer extension is disabled in configurations.'));
        }
        
        $resultPage->setActiveMenu('Mirasvit_CacheWarmer::cache_warmer_job');
        $resultPage->getConfig()->getTitle()->prepend(__('Page Cache Warmer'));
        $resultPage->getConfig()->getTitle()->prepend(__('Jobs'));

        return $resultPage;
    }

    /**
     * @return bool
     */
    protected function _isAllowed()
    {
        return $this->context->getAuthorization()->isAllowed('Mirasvit_CacheWarmer::cache_warmer_job');
    }
}
