Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 107 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
UpdateNotificationSubscriptionsEndpoint | |
0.00% |
0 / 107 |
|
0.00% |
0 / 1 |
600 | |
0.00% |
0 / 1 |
handle | |
0.00% |
0 / 107 |
|
0.00% |
0 / 1 |
600 |
1 | <?php |
2 | |
3 | namespace Olz\Apps\Newsletter\Endpoints; |
4 | |
5 | use Olz\Api\OlzTypedEndpoint; |
6 | use Olz\Entity\NotificationSubscription; |
7 | |
8 | /** |
9 | * @extends OlzTypedEndpoint< |
10 | * array{ |
11 | * deliveryType: 'email'|'telegram', |
12 | * monthlyPreview: bool, |
13 | * weeklyPreview: bool, |
14 | * deadlineWarning: bool, |
15 | * deadlineWarningDays: '1'|'2'|'3'|'7', |
16 | * dailySummary: bool, |
17 | * dailySummaryAktuell: bool, |
18 | * dailySummaryBlog: bool, |
19 | * dailySummaryForum: bool, |
20 | * dailySummaryGalerie: bool, |
21 | * dailySummaryTermine: bool, |
22 | * weeklySummary: bool, |
23 | * weeklySummaryAktuell: bool, |
24 | * weeklySummaryBlog: bool, |
25 | * weeklySummaryForum: bool, |
26 | * weeklySummaryGalerie: bool, |
27 | * weeklySummaryTermine: bool, |
28 | * }, |
29 | * array{status: 'OK'|'ERROR'} |
30 | * > |
31 | */ |
32 | class UpdateNotificationSubscriptionsEndpoint extends OlzTypedEndpoint { |
33 | protected function handle(mixed $input): mixed { |
34 | $user = $this->authUtils()->getCurrentUser(); |
35 | $this->generalUtils()->checkNotNull($user, "Not logged in"); |
36 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
37 | |
38 | $delivery_type = $input['deliveryType']; |
39 | $has_monthly_preview = $input['monthlyPreview']; |
40 | $has_weekly_preview = $input['weeklyPreview']; |
41 | $has_deadline_warning = $input['deadlineWarning']; |
42 | $deadline_warning_days = $input['deadlineWarningDays']; |
43 | $has_daily_summary = $input['dailySummary']; |
44 | $daily_summary_aktuell = $input['dailySummaryAktuell']; |
45 | $daily_summary_blog = $input['dailySummaryBlog']; |
46 | $daily_summary_forum = $input['dailySummaryForum']; |
47 | $daily_summary_galerie = $input['dailySummaryGalerie']; |
48 | $daily_summary_termine = $input['dailySummaryTermine']; |
49 | $has_weekly_summary = $input['weeklySummary']; |
50 | $weekly_summary_aktuell = $input['weeklySummaryAktuell']; |
51 | $weekly_summary_blog = $input['weeklySummaryBlog']; |
52 | $weekly_summary_forum = $input['weeklySummaryForum']; |
53 | $weekly_summary_galerie = $input['weeklySummaryGalerie']; |
54 | $weekly_summary_termine = $input['weeklySummaryTermine']; |
55 | |
56 | $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class); |
57 | |
58 | $existing_subscriptions = $notification_subscription_repo->findBy([ |
59 | 'user' => $user, |
60 | 'delivery_type' => $delivery_type, |
61 | ]); |
62 | foreach ($existing_subscriptions as $subscription) { |
63 | $this->entityManager()->remove($subscription); |
64 | } |
65 | |
66 | // TYPE_DAILY_SUMMARY |
67 | if ($has_daily_summary) { |
68 | $args = []; |
69 | if ($daily_summary_aktuell) { |
70 | $args['aktuell'] = true; |
71 | } |
72 | if ($daily_summary_blog) { |
73 | $args['blog'] = true; |
74 | } |
75 | if ($daily_summary_forum) { |
76 | $args['forum'] = true; |
77 | } |
78 | if ($daily_summary_galerie) { |
79 | $args['galerie'] = true; |
80 | } |
81 | if ($daily_summary_termine) { |
82 | $args['termine'] = true; |
83 | } |
84 | $subscription = new NotificationSubscription(); |
85 | $subscription->setDeliveryType($delivery_type); |
86 | $subscription->setUser($user); |
87 | $subscription->setNotificationType(NotificationSubscription::TYPE_DAILY_SUMMARY); |
88 | $subscription->setNotificationTypeArgs(json_encode($args) ?: '{}'); |
89 | $subscription->setCreatedAt($now_datetime); |
90 | $this->entityManager()->persist($subscription); |
91 | } |
92 | |
93 | // TYPE_DEADLINE_WARNING |
94 | if ($has_deadline_warning) { |
95 | $args = [ |
96 | 'days' => intval($deadline_warning_days), |
97 | ]; |
98 | $subscription = new NotificationSubscription(); |
99 | $subscription->setDeliveryType($delivery_type); |
100 | $subscription->setUser($user); |
101 | $subscription->setNotificationType(NotificationSubscription::TYPE_DEADLINE_WARNING); |
102 | $subscription->setNotificationTypeArgs(json_encode($args) ?: '{}'); |
103 | $subscription->setCreatedAt($now_datetime); |
104 | $this->entityManager()->persist($subscription); |
105 | } |
106 | |
107 | // TYPE_MONTHLY_PREVIEW |
108 | if ($has_monthly_preview) { |
109 | $args = []; |
110 | $subscription = new NotificationSubscription(); |
111 | $subscription->setDeliveryType($delivery_type); |
112 | $subscription->setUser($user); |
113 | $subscription->setNotificationType(NotificationSubscription::TYPE_MONTHLY_PREVIEW); |
114 | $subscription->setNotificationTypeArgs(json_encode($args) ?: '{}'); |
115 | $subscription->setCreatedAt($now_datetime); |
116 | $this->entityManager()->persist($subscription); |
117 | } |
118 | |
119 | // TYPE_WEEKLY_PREVIEW |
120 | if ($has_weekly_preview) { |
121 | $args = []; |
122 | $subscription = new NotificationSubscription(); |
123 | $subscription->setDeliveryType($delivery_type); |
124 | $subscription->setUser($user); |
125 | $subscription->setNotificationType(NotificationSubscription::TYPE_WEEKLY_PREVIEW); |
126 | $subscription->setNotificationTypeArgs(json_encode($args) ?: '{}'); |
127 | $subscription->setCreatedAt($now_datetime); |
128 | $this->entityManager()->persist($subscription); |
129 | } |
130 | |
131 | // TYPE_WEEKLY_SUMMARY |
132 | if ($has_weekly_summary) { |
133 | $args = []; |
134 | if ($weekly_summary_aktuell) { |
135 | $args['aktuell'] = true; |
136 | } |
137 | if ($weekly_summary_blog) { |
138 | $args['blog'] = true; |
139 | } |
140 | if ($weekly_summary_forum) { |
141 | $args['forum'] = true; |
142 | } |
143 | if ($weekly_summary_galerie) { |
144 | $args['galerie'] = true; |
145 | } |
146 | if ($weekly_summary_termine) { |
147 | $args['termine'] = true; |
148 | } |
149 | $subscription = new NotificationSubscription(); |
150 | $subscription->setDeliveryType($delivery_type); |
151 | $subscription->setUser($user); |
152 | $subscription->setNotificationType(NotificationSubscription::TYPE_WEEKLY_SUMMARY); |
153 | $subscription->setNotificationTypeArgs(json_encode($args) ?: '{}'); |
154 | $subscription->setCreatedAt($now_datetime); |
155 | $this->entityManager()->persist($subscription); |
156 | } |
157 | |
158 | // The user actively chose this, so even if they unselected all |
159 | // notifications, we should not send config reminders. |
160 | $notification_type = |
161 | $delivery_type === NotificationSubscription::DELIVERY_TELEGRAM |
162 | ? NotificationSubscription::TYPE_TELEGRAM_CONFIG_REMINDER |
163 | : NotificationSubscription::TYPE_EMAIL_CONFIG_REMINDER; |
164 | $subscription = new NotificationSubscription(); |
165 | $subscription->setDeliveryType($delivery_type); |
166 | $subscription->setUser($user); |
167 | $subscription->setNotificationType($notification_type); |
168 | $subscription->setNotificationTypeArgs(json_encode(['cancelled' => true]) ?: '{}'); |
169 | $subscription->setCreatedAt($now_datetime); |
170 | $this->entityManager()->persist($subscription); |
171 | |
172 | $this->entityManager()->flush(); |
173 | |
174 | return ['status' => 'OK']; |
175 | } |
176 | } |