66 lines
2.2 KiB
PHP
66 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace External\VarioBundle\Uploaders;
|
|
|
|
use External\VarioBundle\Exception\SynchronizerException;
|
|
use External\VarioBundle\Synchronizers\BaseSynchronizer;
|
|
use KupShop\AdminBundle\Util\ActivityLog;
|
|
|
|
class BaseUploader extends BaseSynchronizer
|
|
{
|
|
/** @var string */
|
|
protected static $type = '';
|
|
|
|
public function sync(): void
|
|
{
|
|
$syncType = str_replace('Upload', '', static::getType());
|
|
$this->writeLine(sprintf('Starting synchronization of type \'%s\' to Vario', $syncType));
|
|
|
|
foreach ($this->getData() as $data) {
|
|
$synchronizeData = $this->prepareData($data);
|
|
if ($synchronizeData && $varioId = $this->synchronizeData($synchronizeData)) {
|
|
$this->afterInsertUpdateAction($varioId, $data, $synchronizeData);
|
|
}
|
|
}
|
|
|
|
$this->writeLine(sprintf('Ended synchronization of type \'%s\' to Vario', $syncType));
|
|
}
|
|
|
|
public function synchronizeData(object $data): ?string
|
|
{
|
|
try {
|
|
addActivityLog(ActivityLog::SEVERITY_NOTICE, ActivityLog::TYPE_SYNC, '[Vario] Založení uživatele', [json_encode($data)]);
|
|
if ($varioId = $this->client->getClient()->{self::getType()}($data)) {
|
|
addActivityLog(ActivityLog::SEVERITY_NOTICE, ActivityLog::TYPE_SYNC, '[Vario] Založen uživatel', [json_encode($data), $varioId]);
|
|
|
|
return $this->helper->trimVarioId($varioId);
|
|
}
|
|
} catch (\Exception $e) {
|
|
addActivityLog(ActivityLog::SEVERITY_ERROR, ActivityLog::TYPE_SYNC, '[Vario] Chyba založení uživatele', [$e->getMessage()]);
|
|
|
|
if (isDevelopment()) {
|
|
throw $e;
|
|
}
|
|
throw new SynchronizerException($e->getMessage());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected function afterInsertUpdateAction(string $varioId = '', ?array $data = null, ?object $synchronizeData = null)
|
|
{
|
|
}
|
|
|
|
protected function getData(): iterable
|
|
{
|
|
return [];
|
|
}
|
|
|
|
protected function prepareData(array $data): ?object
|
|
{
|
|
throw new SynchronizerException(
|
|
sprintf('Class %s should overwrite prepareData() method.', get_class($this))
|
|
);
|
|
}
|
|
}
|