entityManager = $entityManager; $this->factory = $factory; $this->language = $language; } public function run(JobData $data): void { $id = $data->getTargetId(); if ($id === null) { throw new Error("ID not passed to the mass action job."); } /** @var ExportEntity|null $entity */ $entity = $this->entityManager->getEntity(ExportEntity::ENTITY_TYPE, $id); if ($entity === null) { throw new Error("Export '{$id}' not found."); } /** @var User|null $user */ $user = $this->entityManager->getEntity(User::ENTITY_TYPE, $entity->getCreatedBy()->getId()); if (!$user) { throw new Error("Export entity '{$id}', user not found."); } try { $export = $this->factory->createForUser($user); $this->setRunning($entity); $result = $export ->setParams($entity->getParams()) ->run(); } catch (Throwable $e) { $this->setFailed($entity); throw new Error("Export job error: " . $e->getMessage()); } $this->setSuccess($entity, $result); $this->entityManager->refreshEntity($entity); if ($entity->notifyOnFinish()) { $this->notifyFinish($entity); } } private function notifyFinish(ExportEntity $entity): void { /** @var Notification $notification */ $notification = $this->entityManager->getNewEntity(Notification::ENTITY_TYPE); $url = '?entryPoint=download&id=' . $entity->getAttachmentId(); $message = str_replace( '{url}', $url, $this->language->translateLabel('exportProcessed', 'messages', 'Export') ); $notification ->setType(Notification::TYPE_MESSAGE) ->setMessage($message) ->setUserId($entity->getCreatedBy()->getId()); $this->entityManager->saveEntity($notification); } private function setFailed(ExportEntity $entity): void { $entity->setStatus(ExportEntity::STATUS_FAILED); $this->entityManager->saveEntity($entity); } private function setRunning(ExportEntity $entity): void { $entity->setStatus(ExportEntity::STATUS_RUNNING); $this->entityManager->saveEntity($entity); } private function setSuccess(ExportEntity $entity, Result $result): void { $entity ->setStatus(ExportEntity::STATUS_SUCCESS) ->setAttachmentId($result->getAttachmentId()); $this->entityManager->saveEntity($entity); } }