I'm running an import of some articles. But when I'm executing the job, I'd like to set the progress and show some explanation in the label. But as I like to keep my code as clean as possible, I have the logic in a Service class where I can't seem to call setProgress
. This is my current situation:
Import Job
class ImportArticlesJob extends BaseJob
{
public function execute($queue)
{
$service = new ImportService;
if (! $service->syncAllArticles($queue, $this)) {
throw new \Exception('Error while import articles');
}
return true;
}
}
Import Service
class ImportService extends Component
{
public function syncAllArticles($queue, $job)
{
try {
$data = $this->apiService->call('GetArticles', 'POST');
} catch (\Exception $e) {
}
$step = 0;
$totalSteps = count($data);
foreach ($data as $row) {
$step++;
$job->setProgress($queue, $step / $totalSteps, Craft::t('app', 'Importing articles: {step} of {total}', [
'step' => $step,
'total' => $totalSteps,
]));
// import logic goes here
}
return true;
}
}
I tried passing $this
to the service, but to no avail. I only got the object and couldn't call setProgress
since I got this error message:
Calling unknown method: ImportArticlesJob::setProgress()
Any ideas wether I can keep the logic in the services or am I obliged to move the API calls & foreach-loops to the job?