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