183 lines
5.4 KiB
PHP
183 lines
5.4 KiB
PHP
<?php
|
|
|
|
class stTaskSchedulerImportTask extends stTask
|
|
{
|
|
/**
|
|
*
|
|
* @var stTaskSchedulerImportInterface
|
|
*/
|
|
private $import = null;
|
|
|
|
public function initialize()
|
|
{
|
|
$configuration = $this->getImport()->getConfiguration();
|
|
|
|
if (!$configuration->getUserOption('enabled'))
|
|
{
|
|
$this->getLogger()->info('Import został pominięty (powód: Wyłączony w konfiguracji)');
|
|
return false;
|
|
}
|
|
|
|
$preloadedDataTask = stTaskScheluder::getTask('st_task_scheduler_import_preloaded_data');
|
|
|
|
if ($preloadedDataTask->getStatus() == stTask::STATUS_RUNNING)
|
|
{
|
|
$this->getLogger()->info('Import został pominięty (powód: Oczekuję na zakończenie zadania *%task%*)', [
|
|
'%task%' => $preloadedDataTask->getName(),
|
|
]);
|
|
|
|
return false;
|
|
}
|
|
|
|
if (!$this->getParameter('import_file') && !$this->downloadFile())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!$this->getImport()->open($this->getParameter('import_file')))
|
|
{
|
|
$this->getLogger()->error('Wystąpił problem podczas otwierania pliku importu `%file%`', array('%file%' => $this->getParameter('import_file')));
|
|
|
|
return false;
|
|
}
|
|
|
|
if (!$this->getImport()->isValid())
|
|
{
|
|
$this->getLogger()->error('Plik `%file%` ma nieprawidłowy format', array('%file%' => $this->getImport()->getConfiguration()->getUserOption('url')));
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function count(): int
|
|
{
|
|
return $this->getImport()->count();
|
|
}
|
|
|
|
public function execute(int $offset): int
|
|
{
|
|
if (!$this->isCLI())
|
|
{
|
|
$this->getImport()->setOffset($offset);
|
|
}
|
|
|
|
if (!$this->getImport()->execute())
|
|
{
|
|
$count = $this->doCount();
|
|
|
|
return $count;
|
|
}
|
|
|
|
$offset = $this->getImport()->getOffset();
|
|
|
|
return $offset;
|
|
}
|
|
|
|
public function started()
|
|
{
|
|
if ($this->getImport()->getConfiguration()->getParent())
|
|
{
|
|
$parentTask = TaskPeer::retrieveByTaskId($this->getImport()->getConfiguration()->getParent()->getId());
|
|
$parentTask->doStart();
|
|
}
|
|
|
|
$this->getImport()->started();
|
|
|
|
stTaskSchedulerImportProductPeer::doResetProductsAvailability($this->getImport());
|
|
}
|
|
|
|
public function finished()
|
|
{
|
|
stTaskSchedulerImportProductPeer::doDeactivateNotAvailableProducts($this->getImport());
|
|
|
|
if ($this->getImport()->getConfiguration()->getParent())
|
|
{
|
|
$parentTask = TaskPeer::retrieveByTaskId($this->getImport()->getConfiguration()->getParent()->getId());
|
|
$parentTask->doFinish();
|
|
}
|
|
|
|
$this->getImport()->finished();
|
|
}
|
|
|
|
protected function getImport(): stTaskSchedulerImportInterface
|
|
{
|
|
if (null === $this->import)
|
|
{
|
|
$factory = new stTaskSchedulerImportFactory();
|
|
$this->import = $factory->createImport($this);
|
|
}
|
|
|
|
return $this->import;
|
|
}
|
|
|
|
/**
|
|
* Zwraca ścieżke do pobranego pliku importu
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function getImportFilepath(): string
|
|
{
|
|
if (!is_dir(sfConfig::get('sf_data_dir') . '/task-scheduler-import'))
|
|
{
|
|
mkdir(sfConfig::get('sf_data_dir') . '/task-scheduler-import');
|
|
}
|
|
|
|
return sfConfig::get('sf_data_dir') . '/task-scheduler-import/' . $this->getId() . '.' . $this->getImport()->getFiletypeExtension();
|
|
}
|
|
|
|
protected function downloadFile(): bool
|
|
{
|
|
$downloader = $this->getImport()->createFileDownloader();
|
|
$fileUrl = $this->getImport()->getConfigurationParameter('url');
|
|
|
|
$authentication = $this->getImport()->getConfiguration()->getAuthentication();
|
|
|
|
if ('basic' == $authentication || 'ftp' == $authentication)
|
|
{
|
|
$auth = $this->getImport()->getConfigurationParameter('auth');
|
|
if ($auth)
|
|
{
|
|
$downloader->setOption('basic_auth', $auth['user'] . ':' . $auth['password']);
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
$this->getLogger()->info('Pobieranie pliku importu <%url%>', array('%url%' => $fileUrl));
|
|
|
|
$downloader->download($fileUrl, function(stFileDownloaderFile $file) use ($fileUrl) {
|
|
|
|
if ($file->isArchive())
|
|
{
|
|
$files = $file->extract();
|
|
$file = $files[0];
|
|
}
|
|
|
|
if ($file->move($this->getImportFilepath()))
|
|
{
|
|
$this->getLogger()->info('Plik importu <%url%> pobrany pomyślnie', array('%url%' => $fileUrl));
|
|
}
|
|
else
|
|
{
|
|
$this->getLogger()->error('Wystąpił problem podczas zapisu pliku importu **%file%**', array('%file%' => $this->getImportFilepath()));
|
|
return false;
|
|
}
|
|
|
|
$this->setParameter('import_file', $file->getFilePath());
|
|
}, ['encode' => false]);
|
|
}
|
|
catch (stFileDownloaderException $e)
|
|
{
|
|
$this->getLogger()->error('Wystąpił błąd `%error%` podczas pobierania pliku importu <%url%>', array(
|
|
'%url%' => $fileUrl,
|
|
'%error%' => $e->getMessage()
|
|
));
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |