ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMassMailTaskProcessor.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3
8
13{
17 private $taskManager;
18
22 private $taskFactory;
23
27 private $language;
28
32 private $logger;
33
38
43
53 public function __construct(
56 ilLanguage $language = null,
57 ilLogger $logger = null,
58 Container $dic = null,
61 ) {
62 if (null === $dic) {
63 global $DIC;
64 $dic = $DIC;
65 }
66
67 if (null === $taskManager) {
68 $taskManager = $dic->backgroundTasks()->taskManager();
69 }
70 $this->taskManager = $taskManager;
71
72 if (null === $taskFactory) {
73 $taskFactory = $dic->backgroundTasks()->taskFactory();
74 }
75 $this->taskFactory = $taskFactory;
76
77 if (null === $language) {
78 $language = $dic->language();
79 }
80 $this->language = $language;
81
82 if (null === $logger) {
84 }
85 $this->logger = $logger;
86
87 if (null === $objectJsonService) {
89 }
90 $this->objectJsonService = $objectJsonService;
91
92 $this->anonymousUserId = $anonymousUserId;
93 }
94
103 public function run(
104 array $mailValueObjects,
105 int $userId,
106 string $contextId,
107 array $contextParameters,
108 int $mailsPerTask = 100
109 ) {
110 $objectsServiceSize = sizeof($mailValueObjects);
111
112 if ($objectsServiceSize <= 0) {
113 throw new ilException('First parameter must contain at least 1 array element');
114 }
115
116 if ($mailsPerTask <= 0) {
117 throw new ilException(sprintf('The mails per task MUST be a positive integer, "%s" given', $mailsPerTask));
118 }
119
120 foreach ($mailValueObjects as $mailValueObject) {
121 if (false === ($mailValueObject instanceof ilMailValueObject)) {
122 throw new ilException('Array MUST contain ilMailValueObjects ONLY');
123 }
124 }
125
126 $lastTask = null;
127 $taskCounter = 0;
128
129 $remainingObjects = array();
130 foreach ($mailValueObjects as $mailValueObject) {
131 $taskCounter++;
132
133 $remainingObjects[] = $mailValueObject;
134 if ($taskCounter === $mailsPerTask) {
135 $interaction = $this->createInteraction($userId, $contextId, $contextParameters, $remainingObjects);
136
137 $this->runTask($interaction, $userId);
138
139 $taskCounter = 0;
140 $remainingObjects = array();
141 }
142 }
143
144 if (array() !== $remainingObjects) {
145 $interaction = $this->createInteraction($userId, $contextId, $contextParameters, $remainingObjects);
146
147 $this->runTask($interaction, $userId);
148 }
149 }
150
155 private function runTask(\ILIAS\BackgroundTasks\Task $task, int $userId)
156 {
157 $bucket = new BasicBucket();
158 $bucket->setUserId($userId);
159
160 $bucket->setTask($task);
161 $bucket->setTitle($this->language->txt('mail_bg_task_title'));
162
163 $this->logger->info('Delegated delivery to background task');
164 $this->taskManager->run($bucket);
165 }
166
174 private function createInteraction(
175 int $userId,
176 string $contextId,
177 array $contextParameters,
178 $remainingObjects
179 ) : \ILIAS\BackgroundTasks\Task {
180 $jsonString = $this->objectJsonService->convertToJson($remainingObjects);
181
182 $task = $this->taskFactory->createTask(\ilMassMailDeliveryJob::class, [
183 (int) $userId,
184 (string) $jsonString,
185 (string) $contextId,
186 (string) serialize($contextParameters),
187 ]);
188
189 // Important: Don't return the task (e.g. as an early return for anonymous user id) https://mantis.ilias.de/view.php?id=33618
190
191 $parameters = [$task, (int) $userId];
192
193 $interaction = $this->taskFactory->createTask(
194 \ilMailDeliveryJobUserInteraction::class,
195 $parameters
196 );
197
198 return $interaction;
199 }
200}
An exception for terminatinating execution or to throw for unit testing.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:19
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
static getLogger($a_component_id)
Get component logger.
Component logger with individual log levels by component id.
createInteraction(int $userId, string $contextId, array $contextParameters, $remainingObjects)
runTask(\ILIAS\BackgroundTasks\Task $task, int $userId)
__construct(TaskManager $taskManager=null, TaskFactory $taskFactory=null, ilLanguage $language=null, ilLogger $logger=null, Container $dic=null, ilMailValueObjectJsonService $objectJsonService=null, string $anonymousUserId=ANONYMOUS_USER_ID)
run(array $mailValueObjects, int $userId, string $contextId, array $contextParameters, int $mailsPerTask=100)
const ANONYMOUS_USER_ID
Definition: constants.php:25
global $DIC
Definition: goto.php:24
language()
Definition: language.php:2
Class ChatMainBarProvider \MainMenu\Provider.
$dic
Definition: result.php:13