ILIAS  trunk Revision v11.0_alpha-1851-ga8564da6fed
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
AutoresponderServiceImpl.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Mail\Autoresponder;
22 
23 use DateInterval;
24 use ilMailOptions;
26 use DateTimeZone;
28 
30 {
32  private $mail_action;
34 
36  protected array $auto_responder_data = [];
37 
38  public function __construct(
39  int $global_idle_time_interval,
40  private bool $auto_responder_status,
41  private readonly AutoresponderRepository $auto_responder_repository,
42  private readonly ClockInterface $clock,
43  ?callable $mail_action = null
44  ) {
45  $this->mail_action = $mail_action;
46 
47  $this->idle_time_interval = new DateInterval('P' . $global_idle_time_interval . 'D');
48  }
49 
50  public function isAutoresponderEnabled(): bool
51  {
52  return $this->auto_responder_status;
53  }
54 
55  public function enableAutoresponder(): void
56  {
57  $this->auto_responder_status = true;
58  }
59 
60  public function disableAutoresponder(): void
61  {
62  $this->auto_responder_status = false;
63  }
64 
65  public function handleAutoresponderMails(int $auto_responder_receiver_usr_id): void
66  {
67  if ($this->auto_responder_data === []) {
68  return;
69  }
70 
71  foreach ($this->auto_responder_data as $auto_responder_sender_usr_id => $mail_options) {
72  if ($this->auto_responder_repository->exists(
73  $auto_responder_sender_usr_id,
74  $auto_responder_receiver_usr_id
75  )) {
76  $auto_responder = $this->auto_responder_repository->findBySenderIdAndReceiverId(
77  $auto_responder_sender_usr_id,
78  $auto_responder_receiver_usr_id
79  );
80  } else {
81  $auto_responder = new AutoresponderDto(
82  $auto_responder_sender_usr_id,
83  $auto_responder_receiver_usr_id,
84  $this->normalizeDateTimezone($this->clock->now())
85  ->sub($this->idle_time_interval)
86  ->modify('-1 second')
87  );
88  }
89 
90  if ($this->shouldSendAutoresponder($auto_responder)) {
91  $auto_responder = $auto_responder->withSentTime($this->clock->now());
92 
93  if ($this->mail_action !== null) {
95  $auto_responder_sender_usr_id,
96  $mail_options,
97  $auto_responder->getSentTime()->add($this->idle_time_interval)
98  );
99  } else {
100  $mail = new AutoresponderNotification(
101  $mail_options,
102  $auto_responder_receiver_usr_id,
103  $auto_responder->getSentTime()->add($this->idle_time_interval)
104  );
105  $mail->send();
106  }
107 
108  $this->auto_responder_repository->store($auto_responder);
109  }
110  }
111  }
112 
114  {
115  return $date_time->setTimezone(new DateTimeZone('UTC'));
116  }
117 
118  private function shouldSendAutoresponder(AutoresponderDto $auto_responder): bool
119  {
120  // Normalize timezones
121  $last_send_time_with_added_interval = $this
122  ->normalizeDateTimezone($auto_responder->getSentTime())
123  ->add($this->idle_time_interval);
124 
125  $now = $this->normalizeDateTimezone($this->clock->now());
126 
127  // Don't compare the objects because of microseconds
128  return $last_send_time_with_added_interval->format('Y-m-d H:i:s') <= $now->format('Y-m-d H:i:s');
129  }
130 
132  int $sender_id,
133  ilMailOptions $mail_receiver_options,
134  ilMailOptions $mail_sender_options
135  ): void {
136  if ($this->auto_responder_status && $mail_receiver_options->isAbsent()) {
137  $this->auto_responder_data[$sender_id] = $mail_receiver_options;
138  }
139  }
140 
141  public function emptyAutoresponderData(): void
142  {
143  $this->auto_responder_data = [];
144  }
145 }
enqueueAutoresponderIfEnabled(int $sender_id, ilMailOptions $mail_receiver_options, ilMailOptions $mail_sender_options)
add()
description: > Example for rendring an add glyph.
Definition: add.php:41
__construct(int $global_idle_time_interval, private bool $auto_responder_status, private readonly AutoresponderRepository $auto_responder_repository, private readonly ClockInterface $clock, ?callable $mail_action=null)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
sub()
description: > Example for rendering a submenu.
Definition: sub.php:32
withSentTime(DateTimeImmutable $sent_time)
handleAutoresponderMails(int $auto_responder_receiver_usr_id)