Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.94% |
59 / 72 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
EmailConfigurationReminderGetter | |
81.94% |
59 / 72 |
|
0.00% |
0 / 3 |
22.35 | |
0.00% |
0 / 1 |
autogenerateSubscriptions | |
80.00% |
20 / 25 |
|
0.00% |
0 / 1 |
10.80 | |||
getEmailConfigReminderState | |
80.65% |
25 / 31 |
|
0.00% |
0 / 1 |
7.36 | |||
getNotification | |
87.50% |
14 / 16 |
|
0.00% |
0 / 1 |
3.02 |
1 | <?php |
2 | |
3 | namespace Olz\Command\SendDailyNotificationsCommand; |
4 | |
5 | use Olz\Entity\NotificationSubscription; |
6 | use Olz\Entity\Users\User; |
7 | use Olz\Utils\WithUtilsTrait; |
8 | |
9 | class EmailConfigurationReminderGetter implements NotificationGetterInterface { |
10 | use NotificationGetterTrait; |
11 | use WithUtilsTrait; |
12 | |
13 | public const DAY_OF_MONTH = 22; |
14 | |
15 | public function autogenerateSubscriptions(): void { |
16 | $email_notifications_state = $this->getEmailConfigReminderState(); |
17 | |
18 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
19 | $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class); |
20 | $user_repo = $this->entityManager()->getRepository(User::class); |
21 | foreach ($email_notifications_state as $user_id => $state) { |
22 | $reminder_id = $state['reminder_id'] ?? false; |
23 | $needs_reminder = $state['needs_reminder'] ?? false; |
24 | $user = $user_repo->findOneBy(['id' => $user_id]); |
25 | if (!$user) { |
26 | $this->log()->warning("No user (ID:{$user_id}) for telegram notification"); |
27 | } |
28 | if ($needs_reminder && !$reminder_id && $user) { |
29 | $this->log()->info("Generating email configuration reminder subscription for '{$user}'..."); |
30 | $subscription = new NotificationSubscription(); |
31 | $subscription->setUser($user); |
32 | $subscription->setDeliveryType(NotificationSubscription::DELIVERY_EMAIL); |
33 | $subscription->setNotificationType(NotificationSubscription::TYPE_EMAIL_CONFIG_REMINDER); |
34 | $subscription->setNotificationTypeArgs(json_encode(['cancelled' => false]) ?: '{}'); |
35 | $subscription->setCreatedAt($now_datetime); |
36 | $this->entityManager()->persist($subscription); |
37 | } |
38 | if ($reminder_id && !$needs_reminder) { |
39 | $this->log()->info("Removing email configuration reminder subscription ({$reminder_id}) for '{$user}'..."); |
40 | $subscription = $notification_subscription_repo->findOneBy(['id' => $reminder_id]); |
41 | if ($subscription) { |
42 | $this->entityManager()->remove($subscription); |
43 | } |
44 | } |
45 | } |
46 | $this->entityManager()->flush(); |
47 | } |
48 | |
49 | /** @return array<int, array{reminder_id?: int, needs_reminder?: bool}> */ |
50 | protected function getEmailConfigReminderState(): array { |
51 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
52 | $minus_one_month = \DateInterval::createFromDateString("-1 months"); |
53 | $one_month_ago = $now_datetime->add($minus_one_month); |
54 | $email_notifications_state = []; |
55 | |
56 | // Find users with existing email config reminder notification subscriptions. |
57 | $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class); |
58 | $email_notification_subscriptions = $notification_subscription_repo->findBy([ |
59 | 'notification_type' => NotificationSubscription::TYPE_EMAIL_CONFIG_REMINDER, |
60 | ]); |
61 | foreach ($email_notification_subscriptions as $subscription) { |
62 | $user_id = $subscription->getUser()->getId() ?: 0; |
63 | $user_state = $email_notifications_state[$user_id] ?? []; |
64 | $subscription_id = $subscription->getId(); |
65 | $this->generalUtils()->checkNotNull($subscription_id, "No subscription ID"); |
66 | $user_state['reminder_id'] = $subscription_id; |
67 | $email_notifications_state[$user_id] = $user_state; |
68 | } |
69 | |
70 | // Find users who should have email config reminder notification subscriptions. |
71 | $user_repo = $this->entityManager()->getRepository(User::class); |
72 | $users_with_email = $user_repo->getUsersWithLogin(); |
73 | $non_config_reminder_notification_types = $this->getNonReminderNotificationTypes(); |
74 | foreach ($users_with_email as $user_with_email) { |
75 | $joined_recently = ($user_with_email->getCreatedAt()->getTimestamp() > $one_month_ago->getTimestamp()); |
76 | $subscription = $notification_subscription_repo->findOneBy([ |
77 | 'user' => $user_with_email, |
78 | 'delivery_type' => NotificationSubscription::DELIVERY_EMAIL, |
79 | 'notification_type' => $non_config_reminder_notification_types, |
80 | ]); |
81 | if (!$subscription && $joined_recently) { |
82 | $user_id = $user_with_email->getId() ?: 0; |
83 | $user_state = $email_notifications_state[$user_id] ?? []; |
84 | $user_state['needs_reminder'] = true; |
85 | $email_notifications_state[$user_id] = $user_state; |
86 | } |
87 | } |
88 | |
89 | return $email_notifications_state; |
90 | } |
91 | |
92 | // --- |
93 | |
94 | /** @param array<string, mixed> $args */ |
95 | public function getNotification(array $args): ?Notification { |
96 | if ($args['cancelled'] ?? false) { |
97 | return null; |
98 | } |
99 | $day_of_month = intval($this->dateUtils()->getCurrentDateInFormat('j')); |
100 | if ($day_of_month !== self::DAY_OF_MONTH) { |
101 | return null; |
102 | } |
103 | |
104 | $base_href = $this->envUtils()->getBaseHref(); |
105 | $code_href = $this->envUtils()->getCodeHref(); |
106 | $newsletter_url = "{$base_href}{$code_href}apps/newsletter"; |
107 | |
108 | $title = "Kein Newsletter abonniert"; |
109 | $text = <<<ZZZZZZZZZZ |
110 | Hallo %%userFirstName%%, |
111 | |
112 | Leider hast du bisher keinerlei OLZ-Newsletter-Benachrichtigungen abonniert. |
113 | |
114 | |
115 | **Du möchtest eigentlich OLZ-Newsletter-Benachrichtigungen erhalten?** |
116 | |
117 | In diesem Fall musst du dich auf der Website [*einloggen*]({$newsletter_url}#login-dialog), und im ["Newsletter"-App]({$newsletter_url}) (ist auch unter "Service" zu finden) bei "E-Mail Newsletter" die gewünschten Benachrichtigungen auswählen. |
118 | |
119 | Falls du dein Passwort vergessen hast, kannst du es im Login-Dialog bei "Passwort vergessen?" zurücksetzen. Du bist mit der E-Mail Adresse `%%userEmail%%` registriert. |
120 | |
121 | |
122 | **Du möchtest auch weiterhin keine OLZ-Newsletter-Benachrichtigungen erhalten?** |
123 | |
124 | Dann ignoriere dieses E-Mail. Wenn du es nicht deaktivierst, wird dir dieses E-Mail nächsten Monat allerdings erneut zugesendet. Um dich abzumelden, klicke unten auf "Keine solchen E-Mails mehr". |
125 | |
126 | |
127 | ZZZZZZZZZZ; |
128 | |
129 | return new Notification($title, $text, [ |
130 | 'notification_type' => NotificationSubscription::TYPE_EMAIL_CONFIG_REMINDER, |
131 | ]); |
132 | } |
133 | } |