Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.95% covered (warning)
87.95%
73 / 83
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseSendNotificationsCommand
87.95% covered (warning)
87.95%
73 / 83
83.33% covered (warning)
83.33%
5 / 6
23.93
0.00% covered (danger)
0.00%
0 / 1
 getAllowedAppEnvs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNotificationSubscriptionType
n/a
0 / 0
n/a
0 / 0
0
 autogenerateSubscriptions
n/a
0 / 0
n/a
0 / 0
0
 getNotifications
n/a
0 / 0
n/a
0 / 0
0
 getNonReminderNotificationTypes
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 handle
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getNotificationSubscriptions
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 sendNotifications
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 sendNotificationToSubscription
79.59% covered (warning)
79.59%
39 / 49
0.00% covered (danger)
0.00%
0 / 1
12.03
1<?php
2
3namespace Olz\Command\Notifications\Common;
4
5use Olz\Command\Common\OlzCommand;
6use Olz\Constants\NotificationDeliveryType;
7use Olz\Constants\NotificationType;
8use Olz\Entity\NotificationSubscription;
9use Olz\Entity\TelegramLink;
10use Symfony\Component\Console\Command\Command;
11use Symfony\Component\Console\Input\InputInterface;
12use Symfony\Component\Console\Output\OutputInterface;
13use Symfony\Component\Mime\Email;
14
15abstract class BaseSendNotificationsCommand extends OlzCommand {
16    /** @return array<string> */
17    protected function getAllowedAppEnvs(): array {
18        return ['dev', 'test', 'staging', 'prod'];
19    }
20
21    abstract public function getNotificationSubscriptionType(): NotificationType;
22
23    abstract public function autogenerateSubscriptions(): void;
24
25    /**
26     * @param array<string, mixed> $args
27     *
28     * @return array<Notification>
29     */
30    abstract public function getNotifications(array $args): array;
31
32    /** @return array<NotificationType> */
33    protected function getNonReminderNotificationTypes(): array {
34        return array_values(array_filter(
35            NotificationType::cases(),
36            function ($notification_type): bool {
37                return
38                    $notification_type !== NotificationType::EMAIL_CONFIG_REMINDER
39                    && $notification_type !== NotificationType::TELEGRAM_CONFIG_REMINDER
40                    && $notification_type !== NotificationType::ROLE_REMINDER;
41            }
42        ));
43    }
44
45    protected function handle(InputInterface $input, OutputInterface $output): int {
46        $this->autogenerateSubscriptions();
47
48        $subscriptions_by_args = $this->getNotificationSubscriptions();
49        $this->sendNotifications($subscriptions_by_args);
50
51        return Command::SUCCESS;
52    }
53
54    /** @return array<string, array<NotificationSubscription>> */
55    private function getNotificationSubscriptions(): array {
56        $type = $this->getNotificationSubscriptionType();
57        $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class);
58        $subscriptions = $notification_subscription_repo->findBy(['notification_type' => $type]);
59
60        $subscriptions_by_args = [];
61        foreach ($subscriptions as $subscription) {
62            $notification_args = $subscription->getNotificationTypeArgs() ?? '';
63            $subscriptions_of_args = $subscriptions_by_args[$notification_args] ?? [];
64            $subscriptions_of_args[] = $subscription;
65            $subscriptions_by_args[$notification_args] = $subscriptions_of_args;
66        }
67        return $subscriptions_by_args;
68    }
69
70    /** @param array<string, array<NotificationSubscription>> $subscriptions_by_args */
71    private function sendNotifications(array $subscriptions_by_args): void {
72        $this->log()->info("Sending '{$this->getNotificationSubscriptionType()->value}' notifications...");
73        foreach ($subscriptions_by_args as $args_json => $subscriptions) {
74            $this->log()->info("Getting notifications for '{$args_json}'...");
75            $args = json_decode($args_json, true);
76            $notifications = $this->getNotifications($args);
77            if ($notifications === []) {
78                $this->log()->info("Nothing to send.");
79            }
80            foreach ($notifications as $notification) {
81                foreach ($subscriptions as $subscription) {
82                    $this->sendNotificationToSubscription($notification, $subscription);
83                }
84            }
85        }
86    }
87
88    private function sendNotificationToSubscription(Notification $notification, NotificationSubscription $subscription): void {
89        $user = $subscription->getUser();
90        $title = $notification->title;
91        $text = $notification->getTextForUser($user);
92        $config = $notification->config;
93        $subscription_id = $subscription->getId();
94        $delivery_type = $subscription->getDeliveryType();
95        $user_id = $user->getId();
96        if (($config['recipient_user_ids'] ?? null) !== null) {
97            $enc_recipient_user_ids = json_encode($config['recipient_user_ids']) ?: '[]';
98            if (!($config['recipient_user_ids'][$user_id] ?? false)) {
99                $this->log()->debug("Recipient user mismatch ({$user_id} not in {$enc_recipient_user_ids})...");
100                return;
101            }
102        }
103        $this->log()->info("Sending notification {$title} over {$delivery_type->value} to user ({$user_id})...");
104        switch ($delivery_type) {
105            case NotificationDeliveryType::EMAIL:
106                try {
107                    $email = (new Email())->subject("[OLZ] {$title}");
108                    $email = $this->emailUtils()->buildOlzEmail($email, $user, $text, $config);
109                    $this->emailUtils()->send($email);
110                    $this->log()->info("Email sent to user ({$user_id}): {$title}");
111                } catch (\Throwable $th) {
112                    $th_class = get_class($th);
113                    $message = $th->getMessage();
114                    $this->log()->critical("Error sending email to user ({$user_id}): [{$th_class}] {$message}", []);
115                }
116                break;
117            case NotificationDeliveryType::TELEGRAM:
118                $telegram_link_repo = $this->entityManager()->getRepository(TelegramLink::class);
119                $telegram_link = $telegram_link_repo->findOneBy(['user' => $user]);
120                if (!$telegram_link) {
121                    $this->log()->notice("User ({$user_id}) has no telegram link, but a subscription ({$subscription_id})");
122                    return;
123                }
124                $user_chat_id = $telegram_link->getTelegramChatId();
125                if (!$user_chat_id) {
126                    $this->log()->critical("User ({$user_id}) has a telegram link without chat ID, but a subscription ({$subscription_id})");
127                    return;
128                }
129                $html_title = $this->telegramUtils()->renderMarkdown($title);
130                $html_text = $this->telegramUtils()->renderMarkdown($text);
131                try {
132                    $this->telegramUtils()->callTelegramApi('sendMessage', [
133                        'chat_id' => $user_chat_id,
134                        'parse_mode' => 'HTML',
135                        'text' => "<b>{$html_title}</b>\n\n{$html_text}",
136                        'disable_web_page_preview' => true,
137                    ]);
138                    $this->log()->info("Telegram sent to user ({$user_id}): {$title}");
139                } catch (\Throwable $th) {
140                    $th_class = get_class($th);
141                    $message = $th->getMessage();
142                    $this->log()->notice("Error sending telegram to user ({$user_id}): [{$th_class}] {$message}", []);
143                }
144                break;
145            default:
146                $this->log()->critical("Unknown delivery type '{$delivery_type->value}'");
147                break;
148        }
149    }
150}