ILIAS  release_8 Revision v8.24
class.ilMassMailTaskProcessor.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
30{
36 private int $anonymousUserId;
37
38 public function __construct(
41 ilLanguage $language = null,
42 ilLogger $logger = null,
43 Container $dic = null,
46 ) {
47 if (null === $dic) {
48 global $DIC;
49 $dic = $DIC;
50 }
51
52 if (null === $taskManager) {
53 $taskManager = $dic->backgroundTasks()->taskManager();
54 }
55 $this->taskManager = $taskManager;
56
57 if (null === $taskFactory) {
58 $taskFactory = $dic->backgroundTasks()->taskFactory();
59 }
60 $this->taskFactory = $taskFactory;
61
62 if (null === $language) {
63 $language = $dic->language();
64 }
65 $this->language = $language;
66
67 if (null === $logger) {
69 }
70 $this->logger = $logger;
71
72 if (null === $objectJsonService) {
74 }
75 $this->objectJsonService = $objectJsonService;
76
77 $this->anonymousUserId = $anonymousUserId;
78 }
79
88 public function run(
89 array $mailValueObjects,
90 int $userId,
91 string $contextId,
92 array $contextParameters,
93 int $mailsPerTask = 100
94 ): void {
95 $objectsServiceSize = count($mailValueObjects);
96
97 if ($objectsServiceSize <= 0) {
98 throw new ilException('First parameter must contain at least 1 array element');
99 }
100
101 if ($mailsPerTask <= 0) {
102 throw new ilException(
103 sprintf(
104 'The mails per task MUST be a positive integer, "%s" given',
105 $mailsPerTask
106 )
107 );
108 }
109
110 foreach ($mailValueObjects as $mailValueObject) {
111 if (!($mailValueObject instanceof ilMailValueObject)) {
112 throw new ilException('Array MUST contain ilMailValueObjects ONLY');
113 }
114 }
115
116 $lastTask = null;
117 $taskCounter = 0;
118
119 $remainingObjects = [];
120 foreach ($mailValueObjects as $mailValueObject) {
121 $taskCounter++;
122
123 $remainingObjects[] = $mailValueObject;
124 if ($taskCounter === $mailsPerTask) {
125 $interaction = $this->createInteraction($userId, $contextId, $contextParameters, $remainingObjects);
126
127 $this->runTask($interaction, $userId);
128
129 $taskCounter = 0;
130 $remainingObjects = [];
131 }
132 }
133
134 if ([] !== $remainingObjects) {
135 $interaction = $this->createInteraction($userId, $contextId, $contextParameters, $remainingObjects);
136
137 $this->runTask($interaction, $userId);
138 }
139 }
140
141 private function runTask(\ILIAS\BackgroundTasks\Task $task, int $userId): void
142 {
143 $bucket = new BasicBucket();
144 $bucket->setUserId($userId);
145
146 $bucket->setTask($task);
147 $bucket->setTitle($this->language->txt('mail_bg_task_title'));
148
149 $this->logger->info('Delegated delivery to background task');
150 $this->taskManager->run($bucket);
151 }
152
153 private function createInteraction(
154 int $userId,
155 string $contextId,
156 array $contextParameters,
157 $remainingObjects
158 ): ILIAS\BackgroundTasks\Task {
159 $jsonString = $this->objectJsonService->convertToJson($remainingObjects);
160
161 $task = $this->taskFactory->createTask(ilMassMailDeliveryJob::class, [
162 $userId,
163 $jsonString,
164 $contextId,
165 serialize($contextParameters),
166 ]);
167
168 // Important: Don't return the task (e.g. as an early return for anonymous user id) https://mantis.ilias.de/view.php?id=33618
169
170 $parameters = [$task, $userId];
171
172 return $this->taskFactory->createTask(
173 ilMailDeliveryJobUserInteraction::class,
174 $parameters
175 );
176 }
177}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
__construct(TaskManager $taskManager=null, TaskFactory $taskFactory=null, ilLanguage $language=null, ilLogger $logger=null, Container $dic=null, ilMailValueObjectJsonService $objectJsonService=null, int $anonymousUserId=ANONYMOUS_USER_ID)
createInteraction(int $userId, string $contextId, array $contextParameters, $remainingObjects)
runTask(\ILIAS\BackgroundTasks\Task $task, int $userId)
ilMailValueObjectJsonService $objectJsonService
run(array $mailValueObjects, int $userId, string $contextId, array $contextParameters, int $mailsPerTask=100)
const ANONYMOUS_USER_ID
Definition: constants.php:27
global $DIC
Definition: feed.php:28
Class ChatMainBarProvider \MainMenu\Provider.
$dic
Definition: result.php:32