Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.50% |
37 / 40 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SendDeadlineWarningCommand | |
92.50% |
37 / 40 |
|
0.00% |
0 / 3 |
8.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 | |
97.37% |
37 / 38 |
|
0.00% |
0 / 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\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-deadline-warning')] |
| 13 | class SendDeadlineWarningCommand extends BaseSendNotificationsCommand { |
| 14 | use WithUtilsTrait; |
| 15 | |
| 16 | public function getNotificationSubscriptionType(): string { |
| 17 | return NotificationSubscription::TYPE_DEADLINE_WARNING; |
| 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 | $days_arg = intval($args['days']); |
| 27 | if ($days_arg <= 0 || $days_arg > 7) { |
| 28 | return null; |
| 29 | } |
| 30 | $given_days = \DateInterval::createFromDateString("+{$days_arg} days"); |
| 31 | $in_given_days = (new \DateTime($this->dateUtils()->getIsoToday()))->add($given_days); |
| 32 | $in_given_days_start = new \DateTime($in_given_days->format('Y-m-d').' 00:00:00'); |
| 33 | $in_given_days_end = new \DateTime($in_given_days->format('Y-m-d').' 23:59:59'); |
| 34 | |
| 35 | $termin_repo = $this->entityManager()->getRepository(Termin::class); |
| 36 | |
| 37 | $base_href = $this->envUtils()->getBaseHref(); |
| 38 | $code_href = $this->envUtils()->getCodeHref(); |
| 39 | $termine_url = "{$base_href}{$code_href}termine"; |
| 40 | |
| 41 | $deadlines_text = ''; |
| 42 | |
| 43 | $criteria = Criteria::create() |
| 44 | ->where( |
| 45 | Criteria::expr()->andX( |
| 46 | Criteria::expr()->gte('deadline', $in_given_days_start), |
| 47 | Criteria::expr()->lte('deadline', $in_given_days_end), |
| 48 | Criteria::expr()->eq('on_off', 1), |
| 49 | ) |
| 50 | ) |
| 51 | ->orderBy(['start_date' => Order::Ascending]) |
| 52 | ->setFirstResult(0) |
| 53 | ->setMaxResults(1000) |
| 54 | ; |
| 55 | $deadlines = $termin_repo->matching($criteria); |
| 56 | foreach ($deadlines as $termin) { |
| 57 | $deadline_date = $termin->getDeadline(); |
| 58 | $date = $deadline_date ? $this->dateUtils()->compactDate($deadline_date) : ''; |
| 59 | $id = $termin->getId(); |
| 60 | $title = $termin->getTitle(); |
| 61 | $deadlines_text .= "- {$date}: Meldeschluss für '[{$title}]({$termine_url}/{$id})'\n"; |
| 62 | } |
| 63 | |
| 64 | if (strlen($deadlines_text) == 0) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | $title = "Meldeschlusswarnung"; |
| 69 | $text = "Hallo %%userFirstName%%,\n\nFolgende Meldeschlüsse stehen bevor:\n\n{$deadlines_text}"; |
| 70 | |
| 71 | return new Notification($title, $text, [ |
| 72 | 'notification_type' => NotificationSubscription::TYPE_DEADLINE_WARNING, |
| 73 | ]); |
| 74 | } |
| 75 | } |