Files
drmaterac.pl/modules/empikmarketplace/src/Handler/CronJobsHandler.php
2025-03-21 20:24:43 +01:00

107 lines
2.7 KiB
PHP

<?php
namespace Empik\Marketplace\Handler;
use Context;
use EmpikMarketplace;
use Empik\Marketplace\Adapter\ToolsAdapter;
use Empik\Marketplace\Processor\OrderProcessor;
use Empik\Marketplace\Processor\ExportProductProcessor;
use Empik\Marketplace\Processor\ExportOfferProcessor;
use Empik\Marketplace\Exception\InvalidActionException;
class CronJobsHandler
{
const ACTION_EXPORT_PRODUCTS = 'exportProducts';
const ACTION_EXPORT_OFFERS = 'exportOffers';
const ACTION_IMPORT_ORDERS = 'importOrders';
const ACTIONS = [
self::ACTION_EXPORT_PRODUCTS,
self::ACTION_EXPORT_OFFERS,
self::ACTION_IMPORT_ORDERS,
];
/** @var Context */
protected $context;
/** @var EmpikMarketplace */
protected $module;
/** @var ToolsAdapter */
protected $tools;
/** @var ExportProductProcessor */
protected $exportProductProcessor;
/** @var ExportOfferProcessor */
protected $exportOfferProcessor;
/** @var OrderProcessor */
protected $orderProcessor;
public function __construct(
EmpikMarketplace $module,
ToolsAdapter $tools,
ExportProductProcessor $exportProductProcessor,
ExportOfferProcessor $exportOfferProcessor,
OrderProcessor $orderProcessor
) {
$this->module = $module;
$this->tools = $tools;
$this->exportProductProcessor = $exportProductProcessor;
$this->exportOfferProcessor = $exportOfferProcessor;
$this->orderProcessor = $orderProcessor;
$this->context = Context::getContext();
}
public function handle($action)
{
set_time_limit(0);
switch ($action) {
case self::ACTION_EXPORT_PRODUCTS:
$this->exportProductProcessor->process();
break;
case self::ACTION_EXPORT_OFFERS:
$this->exportOfferProcessor->process();
break;
case self::ACTION_IMPORT_ORDERS:
$this->orderProcessor->process();
break;
default:
throw new InvalidActionException();
}
}
public function getAvailableActionsUrls()
{
$urls = [];
foreach (self::ACTIONS as $action) {
$urls[$action] = $this->getActionUrl($action);
}
return $urls;
}
public function checkToken($token)
{
return $token === $this->getToken();
}
protected function getActionUrl($action)
{
return $this->context->link->getModuleLink($this->module->name, 'cron', [
'action' => $action,
'token' => $this->getToken(),
]);
}
protected function getToken()
{
return $this->tools->hash($this->module->name . '_cron');
}
}