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