Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.81% |
73 / 77 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SendWeeklyPreviewCommand | |
94.81% |
73 / 77 |
|
40.00% |
2 / 5 |
14.03 | |
0.00% |
0 / 1 |
| getNotificationSubscriptionType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| autogenerateSubscriptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getNotification | |
91.67% |
22 / 24 |
|
0.00% |
0 / 1 |
5.01 | |||
| getTermineText | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
4 | |||
| getDeadlinesText | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Command\Notifications; |
| 4 | |
| 5 | use Doctrine\Common\Collections\Criteria; |
| 6 | use Doctrine\Common\Collections\Order; |
| 7 | use Olz\Entity\NotificationSubscription; |
| 8 | use Olz\Entity\Termine\Termin; |
| 9 | use Olz\Utils\WithUtilsTrait; |
| 10 | use Symfony\Component\Console\Attribute\AsCommand; |
| 11 | |
| 12 | #[AsCommand(name: 'olz:send-weekly-preview')] |
| 13 | class SendWeeklyPreviewCommand extends BaseSendNotificationsCommand { |
| 14 | use WithUtilsTrait; |
| 15 | |
| 16 | public function getNotificationSubscriptionType(): string { |
| 17 | return NotificationSubscription::TYPE_WEEKLY_PREVIEW; |
| 18 | } |
| 19 | |
| 20 | public function autogenerateSubscriptions(): void { |
| 21 | // Must be generated by user. |
| 22 | } |
| 23 | |
| 24 | /** @param array<string, mixed> $args */ |
| 25 | public function getNotification(array $args): ?Notification { |
| 26 | $current_weekday = intval($this->dateUtils()->getCurrentDateInFormat('N')); |
| 27 | $thursday = 4; |
| 28 | if ($current_weekday != $thursday) { |
| 29 | return null; |
| 30 | } |
| 31 | |
| 32 | $four_days = \DateInterval::createFromDateString('+4 days'); |
| 33 | $eleven_days = \DateInterval::createFromDateString('+11 days'); |
| 34 | $today = new \DateTime($this->dateUtils()->getIsoToday()); |
| 35 | $next_monday = (new \DateTime($this->dateUtils()->getIsoToday()))->add($four_days); |
| 36 | $end_of_timespan = (new \DateTime($this->dateUtils()->getIsoToday()))->add($eleven_days); |
| 37 | |
| 38 | $notification_text = ''; |
| 39 | $termine_text = $this->getTermineText($today, $end_of_timespan); |
| 40 | if (strlen($termine_text) > 0) { |
| 41 | $notification_text .= "\n**Termine**\n\n{$termine_text}\n"; |
| 42 | } |
| 43 | $deadlines_text = $this->getDeadlinesText($today, $end_of_timespan); |
| 44 | if (strlen($deadlines_text) > 0) { |
| 45 | $notification_text .= "\n**Meldeschlüsse**\n\n{$deadlines_text}\n"; |
| 46 | } |
| 47 | |
| 48 | if (strlen($notification_text) == 0) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | $next_monday_text = $this->dateUtils()->olzDate('t. MM', $next_monday); |
| 53 | $title = "Vorschau auf die Woche vom {$next_monday_text}"; |
| 54 | $text = "Hallo %%userFirstName%%,\n\nBis Ende nächster Woche haben wir Folgendes auf dem Programm:\n\n{$notification_text}"; |
| 55 | |
| 56 | return new Notification($title, $text, [ |
| 57 | 'notification_type' => NotificationSubscription::TYPE_WEEKLY_PREVIEW, |
| 58 | ]); |
| 59 | } |
| 60 | |
| 61 | public function getTermineText(\DateTime $today, \DateTime $end_of_timespan): string { |
| 62 | $termin_repo = $this->entityManager()->getRepository(Termin::class); |
| 63 | $criteria = Criteria::create() |
| 64 | ->where(Criteria::expr()->andX( |
| 65 | Criteria::expr()->gt('start_date', $today), |
| 66 | Criteria::expr()->lt('start_date', $end_of_timespan), |
| 67 | Criteria::expr()->eq('on_off', 1), |
| 68 | )) |
| 69 | ->orderBy(['start_date' => Order::Ascending]) |
| 70 | ->setFirstResult(0) |
| 71 | ->setMaxResults(1000) |
| 72 | ; |
| 73 | $termine = $termin_repo->matching($criteria); |
| 74 | |
| 75 | $base_href = $this->envUtils()->getBaseHref(); |
| 76 | $code_href = $this->envUtils()->getCodeHref(); |
| 77 | |
| 78 | $termine_url = "{$base_href}{$code_href}termine"; |
| 79 | $termine_text = ""; |
| 80 | foreach ($termine as $termin) { |
| 81 | $id = $termin->getId(); |
| 82 | $starts_on = $termin->getStartDate(); |
| 83 | $ends_on = $termin->getEndDate(); |
| 84 | $date = ($ends_on && $ends_on > $starts_on) |
| 85 | ? $this->dateUtils()->compactDate($starts_on).' - '.$this->dateUtils()->compactDate($ends_on) |
| 86 | : $this->dateUtils()->compactDate($starts_on); |
| 87 | $title = $termin->getTitle(); |
| 88 | $termine_text .= "- {$date}: [{$title}]({$termine_url}/{$id})\n"; |
| 89 | } |
| 90 | return $termine_text; |
| 91 | } |
| 92 | |
| 93 | public function getDeadlinesText(\DateTime $today, \DateTime $end_of_timespan): string { |
| 94 | $termin_repo = $this->entityManager()->getRepository(Termin::class); |
| 95 | |
| 96 | $base_href = $this->envUtils()->getBaseHref(); |
| 97 | $code_href = $this->envUtils()->getCodeHref(); |
| 98 | $termine_url = "{$base_href}{$code_href}termine"; |
| 99 | |
| 100 | $deadlines_text = ''; |
| 101 | |
| 102 | $criteria = Criteria::create() |
| 103 | ->where( |
| 104 | Criteria::expr()->andX( |
| 105 | Criteria::expr()->gt('deadline', $today), |
| 106 | Criteria::expr()->lt('deadline', $end_of_timespan), |
| 107 | Criteria::expr()->eq('on_off', 1), |
| 108 | ) |
| 109 | ) |
| 110 | ->orderBy(['start_date' => Order::Ascending]) |
| 111 | ->setFirstResult(0) |
| 112 | ->setMaxResults(1000) |
| 113 | ; |
| 114 | $deadlines = $termin_repo->matching($criteria); |
| 115 | foreach ($deadlines as $termin) { |
| 116 | $deadline_date = $termin->getDeadline(); |
| 117 | $date = $deadline_date ? $this->dateUtils()->compactDate($deadline_date) : ''; |
| 118 | $id = $termin->getId(); |
| 119 | $title = $termin->getTitle(); |
| 120 | $deadlines_text .= "- {$date}: Meldeschluss für '[{$title}]({$termine_url}/{$id})'\n"; |
| 121 | } |
| 122 | |
| 123 | return $deadlines_text; |
| 124 | } |
| 125 | } |