Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.67% |
74 / 75 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SendTelegramConfigurationReminderCommand | |
98.67% |
74 / 75 |
|
75.00% |
3 / 4 |
21 | |
0.00% |
0 / 1 |
| getNotificationSubscriptionType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| autogenerateSubscriptions | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
10 | |||
| getTelegramConfigReminderState | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
7 | |||
| getNotifications | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Command\Notifications; |
| 4 | |
| 5 | use Olz\Command\Notifications\Common\BaseSendNotificationsCommand; |
| 6 | use Olz\Command\Notifications\Common\Notification; |
| 7 | use Olz\Constants\NotificationDeliveryType; |
| 8 | use Olz\Constants\NotificationType; |
| 9 | use Olz\Entity\NotificationSubscription; |
| 10 | use Olz\Entity\TelegramLink; |
| 11 | use Olz\Entity\Users\User; |
| 12 | use Olz\Utils\WithUtilsTrait; |
| 13 | use Symfony\Component\Console\Attribute\AsCommand; |
| 14 | |
| 15 | #[AsCommand(name: 'olz:send-telegram-config-reminder')] |
| 16 | class SendTelegramConfigurationReminderCommand extends BaseSendNotificationsCommand { |
| 17 | use WithUtilsTrait; |
| 18 | |
| 19 | public const DAY_OF_MONTH = 22; |
| 20 | |
| 21 | public function getNotificationSubscriptionType(): NotificationType { |
| 22 | return NotificationType::TELEGRAM_CONFIG_REMINDER; |
| 23 | } |
| 24 | |
| 25 | public function autogenerateSubscriptions(): void { |
| 26 | $this->log()->info("Generating telegram configuration reminder subscriptions..."); |
| 27 | $telegram_notifications_state = $this->getTelegramConfigReminderState(); |
| 28 | |
| 29 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
| 30 | $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class); |
| 31 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 32 | foreach ($telegram_notifications_state as $user_id => $state) { |
| 33 | $reminder_id = $state['reminder_id'] ?? false; |
| 34 | $needs_reminder = $state['needs_reminder'] ?? false; |
| 35 | $user = $user_repo->findOneBy(['id' => $user_id]); |
| 36 | if (!$user) { |
| 37 | $this->log()->warning("No user (ID:{$user_id}) for telegram reminder"); |
| 38 | } |
| 39 | if ($needs_reminder && !$reminder_id && $user) { |
| 40 | $this->log()->info("Generating telegram configuration reminder subscription for '{$user}'..."); |
| 41 | $subscription = new NotificationSubscription(); |
| 42 | $subscription->setUser($user); |
| 43 | $subscription->setDeliveryType(NotificationDeliveryType::TELEGRAM); |
| 44 | $subscription->setNotificationType(NotificationType::TELEGRAM_CONFIG_REMINDER); |
| 45 | $subscription->setNotificationTypeArgs(json_encode(['cancelled' => false]) ?: '{}'); |
| 46 | $subscription->setCreatedAt($now_datetime); |
| 47 | $this->entityManager()->persist($subscription); |
| 48 | } |
| 49 | if ($reminder_id && !$needs_reminder) { |
| 50 | $this->log()->info("Removing telegram configuration reminder subscription ({$reminder_id}) for '{$user}'..."); |
| 51 | $subscription = $notification_subscription_repo->findOneBy(['id' => $reminder_id]); |
| 52 | if ($subscription) { |
| 53 | $this->entityManager()->remove($subscription); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | $this->entityManager()->flush(); |
| 58 | } |
| 59 | |
| 60 | /** @return array<int, array{reminder_id?: int, needs_reminder?: bool}> */ |
| 61 | protected function getTelegramConfigReminderState(): array { |
| 62 | $telegram_notifications_state = []; |
| 63 | |
| 64 | // Find users with existing telegram config reminder notification subscriptions. |
| 65 | $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class); |
| 66 | $telegram_notification_subscriptions = $notification_subscription_repo->findBy([ |
| 67 | 'notification_type' => NotificationType::TELEGRAM_CONFIG_REMINDER, |
| 68 | ]); |
| 69 | foreach ($telegram_notification_subscriptions as $subscription) { |
| 70 | $user_id = $subscription->getUser()->getId() ?: 0; |
| 71 | $user_state = $telegram_notifications_state[$user_id] ?? []; |
| 72 | $subscription_id = $subscription->getId(); |
| 73 | $this->generalUtils()->checkNotNull($subscription_id, "No subscription ID"); |
| 74 | $user_state['reminder_id'] = $subscription_id; |
| 75 | $telegram_notifications_state[$user_id] = $user_state; |
| 76 | } |
| 77 | |
| 78 | // Find users who should have telegram config reminder notification subscriptions. |
| 79 | $telegram_link_repo = $this->entityManager()->getRepository(TelegramLink::class); |
| 80 | $telegram_links = $telegram_link_repo->getActivatedTelegramLinks(); |
| 81 | $non_config_reminder_notification_types = $this->getNonReminderNotificationTypes(); |
| 82 | foreach ($telegram_links as $telegram_link) { |
| 83 | $user = $telegram_link->getUser(); |
| 84 | if (!$user) { |
| 85 | continue; |
| 86 | } |
| 87 | $subscription = $notification_subscription_repo->findOneBy([ |
| 88 | 'user' => $user, |
| 89 | 'delivery_type' => NotificationDeliveryType::TELEGRAM, |
| 90 | 'notification_type' => $non_config_reminder_notification_types, |
| 91 | ]); |
| 92 | if (!$subscription) { |
| 93 | $user_id = $user->getId() ?: 0; |
| 94 | $user_state = $telegram_notifications_state[$user_id] ?? []; |
| 95 | $user_state['needs_reminder'] = true; |
| 96 | $telegram_notifications_state[$user_id] = $user_state; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $telegram_notifications_state; |
| 101 | } |
| 102 | |
| 103 | // --- |
| 104 | |
| 105 | public function getNotifications(array $args): array { |
| 106 | if ($args['cancelled'] ?? false) { |
| 107 | return []; |
| 108 | } |
| 109 | $day_of_month = intval($this->dateUtils()->getCurrentDateInFormat('j')); |
| 110 | if ($day_of_month !== self::DAY_OF_MONTH) { |
| 111 | return []; |
| 112 | } |
| 113 | |
| 114 | $base_href = $this->envUtils()->getBaseHref(); |
| 115 | $code_href = $this->envUtils()->getCodeHref(); |
| 116 | $newsletter_url = "{$base_href}{$code_href}apps/newsletter"; |
| 117 | |
| 118 | $title = "Keine Push-Nachrichten abonniert"; |
| 119 | $text = <<<ZZZZZZZZZZ |
| 120 | Hallo %%userFirstName%%, |
| 121 | |
| 122 | Du hast bisher keinerlei Push-Nachrichten für Telegram abonniert. |
| 123 | |
| 124 | |
| 125 | **Du möchtest eigentlich Push-Nachrichten erhalten?** |
| 126 | |
| 127 | In diesem Fall musst du dich auf der Website *einloggen*, und im ["Newsletter"-App]({$newsletter_url}) (ist auch unter "Service" zu finden) bei "Nachrichten-Push" die gewünschten Benachrichtigungen auswählen. |
| 128 | |
| 129 | |
| 130 | **Du möchtest gar keine Push-Nachrichten erhalten?** |
| 131 | |
| 132 | Dann lösche einfach diesen Chat. |
| 133 | |
| 134 | |
| 135 | ZZZZZZZZZZ; |
| 136 | |
| 137 | return [ |
| 138 | new Notification($title, $text, [ |
| 139 | 'notification_type' => NotificationType::TELEGRAM_CONFIG_REMINDER, |
| 140 | ]), |
| 141 | ]; |
| 142 | } |
| 143 | } |