Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
42 / 42 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| SendDeadlineWarningCommand | |
100.00% |
42 / 42 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
| getNotificationSubscriptionType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| autogenerateSubscriptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNotifications | |
100.00% |
40 / 40 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Command\Notifications; |
| 4 | |
| 5 | use Doctrine\Common\Collections\Criteria; |
| 6 | use Doctrine\Common\Collections\Order; |
| 7 | use Olz\Command\Notifications\Common\BaseSendNotificationsCommand; |
| 8 | use Olz\Command\Notifications\Common\Notification; |
| 9 | use Olz\Constants\NotificationType; |
| 10 | use Olz\Entity\Termine\Termin; |
| 11 | use Olz\Utils\WithUtilsTrait; |
| 12 | use Symfony\Component\Console\Attribute\AsCommand; |
| 13 | |
| 14 | #[AsCommand(name: 'olz:send-deadline-warning')] |
| 15 | class SendDeadlineWarningCommand extends BaseSendNotificationsCommand { |
| 16 | use WithUtilsTrait; |
| 17 | |
| 18 | public function getNotificationSubscriptionType(): NotificationType { |
| 19 | return NotificationType::DEADLINE_WARNING; |
| 20 | } |
| 21 | |
| 22 | public function autogenerateSubscriptions(): void { |
| 23 | // Must be generated by user. |
| 24 | } |
| 25 | |
| 26 | public function getNotifications(array $args): array { |
| 27 | $days_arg = intval($args['days']); |
| 28 | if ($days_arg <= 0 || $days_arg > 7) { |
| 29 | return []; |
| 30 | } |
| 31 | $given_days = \DateInterval::createFromDateString("+{$days_arg} days"); |
| 32 | $in_given_days = (new \DateTime($this->dateUtils()->getIsoToday()))->add($given_days); |
| 33 | $in_given_days_start = new \DateTime($in_given_days->format('Y-m-d').' 00:00:00'); |
| 34 | $in_given_days_end = new \DateTime($in_given_days->format('Y-m-d').' 23:59:59'); |
| 35 | |
| 36 | $termin_repo = $this->entityManager()->getRepository(Termin::class); |
| 37 | |
| 38 | $base_href = $this->envUtils()->getBaseHref(); |
| 39 | $code_href = $this->envUtils()->getCodeHref(); |
| 40 | $termine_url = "{$base_href}{$code_href}termine"; |
| 41 | |
| 42 | $deadlines_text = ''; |
| 43 | |
| 44 | $criteria = Criteria::create() |
| 45 | ->where( |
| 46 | Criteria::expr()->andX( |
| 47 | Criteria::expr()->gte('deadline', $in_given_days_start), |
| 48 | Criteria::expr()->lte('deadline', $in_given_days_end), |
| 49 | Criteria::expr()->eq('on_off', 1), |
| 50 | ) |
| 51 | ) |
| 52 | ->orderBy(['start_date' => Order::Ascending]) |
| 53 | ->setFirstResult(0) |
| 54 | ->setMaxResults(1000) |
| 55 | ; |
| 56 | $deadlines = $termin_repo->matching($criteria); |
| 57 | foreach ($deadlines as $termin) { |
| 58 | $deadline_date = $termin->getDeadline(); |
| 59 | $date = $deadline_date ? $this->dateUtils()->compactDate($deadline_date) : ''; |
| 60 | $id = $termin->getId(); |
| 61 | $title = $this->htmlUtils()->escapeMarkdown($termin->getTitle() ?? ''); |
| 62 | $deadlines_text .= "- {$date}: Meldeschluss für '[{$title}]({$termine_url}/{$id})'\n"; |
| 63 | } |
| 64 | |
| 65 | if (strlen($deadlines_text) == 0) { |
| 66 | return []; |
| 67 | } |
| 68 | |
| 69 | $title = "Meldeschlusswarnung"; |
| 70 | $text = "Hallo %%userFirstName%%,\n\nFolgende Meldeschlüsse stehen bevor:\n\n{$deadlines_text}"; |
| 71 | |
| 72 | return [ |
| 73 | new Notification($title, $text, [ |
| 74 | 'notification_type' => NotificationType::DEADLINE_WARNING, |
| 75 | ]), |
| 76 | ]; |
| 77 | } |
| 78 | } |