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