231 lines
6.4 KiB
PHP
231 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace KupShop\PohodaBundle\Utils;
|
|
|
|
use KupShop\AdminBundle\Util\ActivityLog;
|
|
use Query\Operator;
|
|
|
|
trait PohodaConnectorHelper
|
|
{
|
|
use \DatabaseCommunication;
|
|
|
|
protected \ArrayObject $mConfigs;
|
|
protected \ArrayIterator $iterator;
|
|
|
|
/**
|
|
* @var Configuration
|
|
*/
|
|
public $configuration;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $syncTime;
|
|
protected $skipSaveLastSyncTime = false;
|
|
|
|
protected bool $mServerQuickInstance = true;
|
|
|
|
public function reloadConfig()
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
$pohodaSettings = $dbcfg->loadValue('pohoda') ?? [];
|
|
|
|
$this->mConfigs = new \ArrayObject(
|
|
array_filter(
|
|
$pohodaSettings['mservers'] ?? [],
|
|
fn ($key) => in_array($key, array_flip($this->configuration->get('mservers'))),
|
|
ARRAY_FILTER_USE_KEY
|
|
)
|
|
);
|
|
|
|
// Fallback kdyz neni nastaveny instance pro pomale operace
|
|
foreach ($this->mConfigs as &$config) {
|
|
$config['url_long'] = ($config['url_long'] ?? false) ?: $config['url'];
|
|
$config['server_name_long'] = ($config['server_name_long'] ?? false) ?: ($config['server_name'] ?? '');
|
|
}
|
|
|
|
$this->iterator = $this->mConfigs->getIterator();
|
|
}
|
|
|
|
private function getIterator(): \ArrayIterator
|
|
{
|
|
return $this->iterator;
|
|
}
|
|
|
|
public function getCurrent()
|
|
{
|
|
return $this->getIterator()->current();
|
|
}
|
|
|
|
public function getCurrentKey()
|
|
{
|
|
return $this->getIterator()->key();
|
|
}
|
|
|
|
public function getMServerCount()
|
|
{
|
|
return count($this->configuration->get('mservers'));
|
|
}
|
|
|
|
public function getDefaultKey(): int
|
|
{
|
|
return $this->configuration->getDefaultIndex();
|
|
}
|
|
|
|
public function getDefault()
|
|
{
|
|
if ($this->mConfigs->offsetExists($this->getDefaultKey() ?? false)) {
|
|
return $this->mConfigs->offsetGet($this->getDefaultKey());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function setActiveConfigById($id)
|
|
{
|
|
if ($this->mConfigs->offsetGet($id) ?? false) {
|
|
$this->getIterator()->seek($id);
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function setActiveConfigByOrderId($orderId)
|
|
{
|
|
$mServer = sqlQueryBuilder()->select("JSON_VALUE(note_admin, '$.mServerIndex')")
|
|
->from('orders')
|
|
->where(Operator::equals(['id' => $orderId]))
|
|
->execute()
|
|
->fetchOne();
|
|
|
|
return $this->setActiveConfigById($mServer);
|
|
}
|
|
|
|
public function prepareSyncTime()
|
|
{
|
|
$this->syncTime = (new \DateTime())->format('d-m-Y H:i:s');
|
|
}
|
|
|
|
/**
|
|
* @param null $count
|
|
* @param string $appendText
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function setLastSyncTime($type, $count = null, $appendText = '')
|
|
{
|
|
if ($this->skipSaveLastSyncTime) {
|
|
$this->skipSaveLastSyncTime = false;
|
|
|
|
return;
|
|
}
|
|
|
|
$dbcfg = \Settings::getDefault();
|
|
$pohoda = $dbcfg->loadValue('pohoda');
|
|
$pohoda['mservers'][$this->getCurrentKey()][$type]['last_sync'] = $this->syncTime ?: (new \DateTime())->format('d-m-Y H:i:s');
|
|
|
|
if ($count) {
|
|
$pohoda['mservers'][$this->getCurrentKey()][$type]['count'] = $count;
|
|
}
|
|
|
|
if (empty($dbcfg->pohoda)) {
|
|
$dbcfg->saveValue('pohoda', $pohoda);
|
|
} else {
|
|
$dbcfg->updateValue('pohoda', $pohoda);
|
|
}
|
|
if (!empty($appendText)) {
|
|
$appendText = "- {$appendText}";
|
|
}
|
|
$this->addActivityLog(ActivityLog::SEVERITY_SUCCESS, translate($type, 'pohodaSettingsTab', false, true)."{$appendText}", []);
|
|
}
|
|
|
|
public function getLastSyncTime($type)
|
|
{
|
|
$datetime = new \DateTime();
|
|
if (!empty($this->getCurrent()[$type]['last_sync'])) {
|
|
$datetime->setTimestamp(strtotime($this->getCurrent()[$type]['last_sync']));
|
|
$datetime = $datetime->modify('-5 minutes');
|
|
} else {
|
|
$datetime = $datetime->modify('-2 hours');
|
|
}
|
|
|
|
return $this->prepareDateTime($datetime);
|
|
}
|
|
|
|
public function checkLastSuccessfulSynchronizationTime(): void
|
|
{
|
|
$pohoda = \Settings::getDefault()->loadValue('pohoda');
|
|
$lastSync = $pohoda['mservers'][$this->getCurrentKey()]['lastSuccessfulSynchronizationTime'] ?? null;
|
|
|
|
if ($lastSync && (strtotime($lastSync) < strtotime('-30 minutes'))) {
|
|
$this->addActivityLog(
|
|
severity: ActivityLog::SEVERITY_WARNING,
|
|
msg: 'Od poslední úspěšné synchronizace uplynulo více než 30 minut',
|
|
data: [
|
|
'mserver' => $this->getCurrentKey(),
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
public function setLastSuccessfulSynchronizationTime(): void
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
$pohoda = $dbcfg->loadValue('pohoda');
|
|
$pohoda['mservers'][$this->getCurrentKey()]['lastSuccessfulSynchronizationTime'] = (new \DateTime())->format('d-m-Y H:i:s');
|
|
|
|
if (empty($dbcfg->pohoda)) {
|
|
$dbcfg->saveValue('pohoda', $pohoda);
|
|
} else {
|
|
$dbcfg->updateValue('pohoda', $pohoda);
|
|
}
|
|
}
|
|
|
|
public function skipSaveLastSyncTime(bool $skipSaveLastSyncTime)
|
|
{
|
|
$this->skipSaveLastSyncTime = $skipSaveLastSyncTime;
|
|
}
|
|
|
|
/**
|
|
* @required
|
|
*/
|
|
public function setConfiguration(Configuration $configuration): void
|
|
{
|
|
$this->configuration = $configuration;
|
|
$this->reloadConfig();
|
|
}
|
|
|
|
protected function saveLastSuccessConnectionTime()
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
|
|
$pohoda = $dbcfg->loadValue('pohoda');
|
|
$pohoda['mservers'][$this->getCurrentKey()]['lastSuccessConnectionTime'] = (new \DateTime())->format('d-m-Y H:i:s');
|
|
|
|
if (empty($dbcfg->pohoda)) {
|
|
$dbcfg->saveValue('pohoda', $pohoda);
|
|
} else {
|
|
$dbcfg->updateValue('pohoda', $pohoda);
|
|
}
|
|
}
|
|
|
|
public function isMServerQuickInstance(): bool
|
|
{
|
|
return $this->mServerQuickInstance;
|
|
}
|
|
|
|
public function setMServerQuickInstance(bool $mServerQuickInstance): void
|
|
{
|
|
$this->mServerQuickInstance = $mServerQuickInstance;
|
|
}
|
|
|
|
public function getMServerName()
|
|
{
|
|
$config = $this->getCurrent();
|
|
|
|
return $this->isMServerQuickInstance() ? $config['server_name'] : $config['server_name_long'];
|
|
}
|
|
}
|