Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
79 / 79
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SendWeeklyPreviewCommand
100.00% covered (success)
100.00%
79 / 79
100.00% covered (success)
100.00%
5 / 5
14
100.00% covered (success)
100.00%
1 / 1
 getNotificationSubscriptionType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 autogenerateSubscriptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNotifications
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
5
 getTermineText
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
4
 getDeadlinesText
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Olz\Command\Notifications;
4
5use Doctrine\Common\Collections\Criteria;
6use Doctrine\Common\Collections\Order;
7use Olz\Command\Notifications\Common\BaseSendNotificationsCommand;
8use Olz\Command\Notifications\Common\Notification;
9use Olz\Constants\NotificationType;
10use Olz\Entity\Termine\Termin;
11use Olz\Utils\WithUtilsTrait;
12use Symfony\Component\Console\Attribute\AsCommand;
13
14#[AsCommand(name: 'olz:send-weekly-preview')]
15class SendWeeklyPreviewCommand extends BaseSendNotificationsCommand {
16    use WithUtilsTrait;
17
18    public function getNotificationSubscriptionType(): NotificationType {
19        return NotificationType::WEEKLY_PREVIEW;
20    }
21
22    public function autogenerateSubscriptions(): void {
23        // Must be generated by user.
24    }
25
26    public function getNotifications(array $args): array {
27        $current_weekday = intval($this->dateUtils()->getCurrentDateInFormat('N'));
28        $thursday = 4;
29        if ($current_weekday != $thursday) {
30            return [];
31        }
32
33        $four_days = \DateInterval::createFromDateString('+4 days');
34        $eleven_days = \DateInterval::createFromDateString('+11 days');
35        $today = new \DateTime($this->dateUtils()->getIsoToday());
36        $next_monday = (new \DateTime($this->dateUtils()->getIsoToday()))->add($four_days);
37        $end_of_timespan = (new \DateTime($this->dateUtils()->getIsoToday()))->add($eleven_days);
38
39        $notification_text = '';
40        $termine_text = $this->getTermineText($today, $end_of_timespan);
41        if (strlen($termine_text) > 0) {
42            $notification_text .= "\n**Termine**\n\n{$termine_text}\n";
43        }
44        $deadlines_text = $this->getDeadlinesText($today, $end_of_timespan);
45        if (strlen($deadlines_text) > 0) {
46            $notification_text .= "\n**Meldeschlüsse**\n\n{$deadlines_text}\n";
47        }
48
49        if (strlen($notification_text) == 0) {
50            return [];
51        }
52
53        $next_monday_text = $this->dateUtils()->olzDate('t. MM', $next_monday);
54        $title = "Vorschau auf die Woche vom {$next_monday_text}";
55        $text = "Hallo %%userFirstName%%,\n\nBis Ende nächster Woche haben wir Folgendes auf dem Programm:\n\n{$notification_text}";
56
57        return [
58            new Notification($title, $text, [
59                'notification_type' => NotificationType::WEEKLY_PREVIEW,
60            ]),
61        ];
62    }
63
64    public function getTermineText(\DateTime $today, \DateTime $end_of_timespan): string {
65        $termin_repo = $this->entityManager()->getRepository(Termin::class);
66        $criteria = Criteria::create()
67            ->where(Criteria::expr()->andX(
68                Criteria::expr()->gt('start_date', $today),
69                Criteria::expr()->lt('start_date', $end_of_timespan),
70                Criteria::expr()->eq('on_off', 1),
71            ))
72            ->orderBy(['start_date' => Order::Ascending])
73            ->setFirstResult(0)
74            ->setMaxResults(1000)
75        ;
76        $termine = $termin_repo->matching($criteria);
77
78        $base_href = $this->envUtils()->getBaseHref();
79        $code_href = $this->envUtils()->getCodeHref();
80
81        $termine_url = "{$base_href}{$code_href}termine";
82        $termine_text = "";
83        foreach ($termine as $termin) {
84            $id = $termin->getId();
85            $starts_on = $termin->getStartDate();
86            $ends_on = $termin->getEndDate();
87            $date = ($ends_on && $ends_on > $starts_on)
88                ? $this->dateUtils()->compactDate($starts_on).' - '.$this->dateUtils()->compactDate($ends_on)
89                : $this->dateUtils()->compactDate($starts_on);
90            $title = $this->htmlUtils()->escapeMarkdown($termin->getTitle() ?? '');
91            $termine_text .= "- {$date}: [{$title}]({$termine_url}/{$id})\n";
92        }
93        return $termine_text;
94    }
95
96    public function getDeadlinesText(\DateTime $today, \DateTime $end_of_timespan): string {
97        $termin_repo = $this->entityManager()->getRepository(Termin::class);
98
99        $base_href = $this->envUtils()->getBaseHref();
100        $code_href = $this->envUtils()->getCodeHref();
101        $termine_url = "{$base_href}{$code_href}termine";
102
103        $deadlines_text = '';
104
105        $criteria = Criteria::create()
106            ->where(
107                Criteria::expr()->andX(
108                    Criteria::expr()->gt('deadline', $today),
109                    Criteria::expr()->lt('deadline', $end_of_timespan),
110                    Criteria::expr()->eq('on_off', 1),
111                )
112            )
113            ->orderBy(['start_date' => Order::Ascending])
114            ->setFirstResult(0)
115            ->setMaxResults(1000)
116        ;
117        $deadlines = $termin_repo->matching($criteria);
118        foreach ($deadlines as $termin) {
119            $deadline_date = $termin->getDeadline();
120            $date = $deadline_date ? $this->dateUtils()->compactDate($deadline_date) : '';
121            $id = $termin->getId();
122            $title = $this->htmlUtils()->escapeMarkdown($termin->getTitle() ?? '');
123            $deadlines_text .= "- {$date}: Meldeschluss für '[{$title}]({$termine_url}/{$id})'\n";
124        }
125
126        return $deadlines_text;
127    }
128}