ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Notifier.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ReportDto;
26 use ilDBStatement;
27 use ilDBConstants;
29 use ilDBInterface;
30 
31 class Notifier
32 {
34  private const MAIL_DELIVERY_PING_THRESHOLD = 25;
35 
36  private readonly ilDBInterface $db;
37  private readonly ClockInterface $clock;
39 
40  public function __construct(
41  private readonly ilMailCronOrphanedMails $job,
42  private readonly NotificationsCollector $collector,
43  private readonly int $mail_expiration_days,
44  private readonly int $mail_expiration_warning_days,
45  ?ilDBInterface $db = null,
46  ?ClockInterface $clock = null
47  ) {
48  global $DIC;
49 
50  $this->db = $db ?? $DIC->database();
51  $this->clock = $clock ?? (new Factory())->clock()->system();
52 
53  $this->mark_as_notified_stmt = $this->db->prepareManip(
54  'INSERT INTO mail_cron_orphaned (mail_id, folder_id, ts_do_delete) VALUES (?, ?, ?)',
56  );
57  }
58 
59  private function markAsNotified(ReportDto $collection_obj): void
60  {
61  $notify_days_before = 1;
62  if ($this->mail_expiration_days > $this->mail_expiration_warning_days) {
63  $notify_days_before = $this->mail_expiration_days - $this->mail_expiration_warning_days;
64  }
65 
66  $deletion_datetime = $this->clock->now()
67  ->modify('+ ' . $notify_days_before . ' days')
68  ->setTime(0, 0);
69 
70  $i = 0;
71  foreach ($collection_obj->getFolderObjects() as $folder_obj) {
72  $folder_id = $folder_obj->getFolderId();
73 
74  foreach ($folder_obj->getOrphanedMailObjects() as $mail_obj) {
75  $mail_id = $mail_obj->getMailId();
76 
77  if ($i > 0 && $i % self::NOTIFICATION_MARKER_PING_THRESHOLD === 0) {
78  $this->job->ping();
79  }
80 
81  $this->db->execute(
82  $this->mark_as_notified_stmt,
83  [$mail_id, $folder_id, $deletion_datetime->getTimestamp()]
84  );
85  $i++;
86  }
87  }
88  }
89 
90  private function sendMail(ReportDto $collection_obj): void
91  {
92  $mail = new MailNotification();
93  $mail->setRecipients([$collection_obj->getUserId()]);
94  $mail->setAdditionalInformation(['mail_folders' => $collection_obj->getFolderObjects()]);
95  $mail->send();
96  }
97 
98  public function send(): void
99  {
100  $i = 0;
101  foreach ($this->collector->getCollection() as $collection_obj) {
102  if ($i > 0 && $i % self::MAIL_DELIVERY_PING_THRESHOLD === 0) {
103  $this->job->ping();
104  }
105 
106  $this->sendMail($collection_obj);
107  $this->markAsNotified($collection_obj);
108  ++$i;
109  }
110  }
111 }
getFolderObjects()
Definition: ReportDto.php:50
markAsNotified(ReportDto $collection_obj)
Definition: Notifier.php:59
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct(private readonly ilMailCronOrphanedMails $job, private readonly NotificationsCollector $collector, private readonly int $mail_expiration_days, private readonly int $mail_expiration_warning_days, ?ilDBInterface $db=null, ?ClockInterface $clock=null)
Definition: Notifier.php:40
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22
Builds data types.
Definition: Factory.php:35
readonly ilDBStatement $mark_as_notified_stmt
Definition: Notifier.php:38