Files
kupshop/bundles/External/PompoBundle/DRS/Synchronizer/AbstractSynchronizer.php
2025-08-02 16:30:27 +02:00

95 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace External\PompoBundle\DRS\Synchronizer;
use External\PompoBundle\DRS\Util\DRSApi;
use External\PompoBundle\Util\PompoLogger;
abstract class AbstractSynchronizer implements SynchronizerInterface
{
protected static $type;
protected $mode = self::MODE_NORMAL;
protected bool $logging = false;
protected DRSApi $drsApi;
protected PompoLogger $logger;
public function __construct(DRSApi $drsApi, PompoLogger $logger)
{
$this->drsApi = $drsApi;
$this->logger = $logger;
}
public static function getType(): string
{
return static::$type;
}
public function setMode(int $mode): void
{
$this->mode = $mode;
}
public function process(): void
{
foreach ($this->getItems() as $item) {
$this->logItem($item);
$this->processItem($item);
}
}
abstract protected function processItem(array $item): void;
protected function getItems(): iterable
{
return [];
}
protected function getLastTimestamp(): ?int
{
if ($this->mode === self::MODE_FULL) {
return null;
}
$dbcfg = \Settings::getDefault();
$drs = $dbcfg->loadValue('drs');
return $drs[static::getType()]['timestamp'] ?? null;
}
protected function setLastTimestamp(int $timestamp): void
{
$dbcfg = \Settings::getDefault();
$drs = $dbcfg->loadValue('drs') ?? [];
$drs[static::getType()]['timestamp'] = $timestamp;
$drs[static::getType()]['updated'] = time();
$dbcfg->saveValue('drs', $drs, false);
}
private function logItem(array $item): void
{
if (isDevelopment()) {
return;
}
if (!$this->logging) {
return;
}
$this->logger->log(
sprintf('[pompo] Processing item of type "%s"', static::getType()),
[
'type' => static::getType(),
'data' => $item,
]
);
}
}