Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
80 / 80
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SendWeeklyPreviewCommand
100.00% covered (success)
100.00%
80 / 80
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%
27 / 27
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\Common\Utils\WithUtilsTrait;
10use Olz\Constants\NotificationType;
11use Olz\Entity\Termine\Termin;
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()->isNull('deleted_at'),
71                Criteria::expr()->eq('parent_termin', null),
72            ))
73            ->orderBy(['start_date' => Order::Ascending])
74            ->setFirstResult(0)
75            ->setMaxResults(1000)
76        ;
77        $termine = $termin_repo->matching($criteria);
78
79        $base_href = $this->envUtils()->getBaseHref();
80        $code_href = $this->envUtils()->getCodeHref();
81
82        $termine_url = "{$base_href}{$code_href}termine";
83        $termine_text = "";
84        foreach ($termine as $termin) {
85            $id = $termin->getId();
86            $starts_on = $termin->getStartDate();
87            $ends_on = $termin->getEndDate();
88            $date = ($ends_on && $ends_on > $starts_on)
89                ? $this->dateUtils()->compactDate($starts_on).' - '.$this->dateUtils()->compactDate($ends_on)
90                : $this->dateUtils()->compactDate($starts_on);
91            $title = $this->htmlUtils()->escapeMarkdown($termin->getTitle() ?? '');
92            $termine_text .= "- {$date}: [{$title}]({$termine_url}/{$id})\n";
93        }
94        return $termine_text;
95    }
96
97    public function getDeadlinesText(\DateTime $today, \DateTime $end_of_timespan): string {
98        $termin_repo = $this->entityManager()->getRepository(Termin::class);
99
100        $base_href = $this->envUtils()->getBaseHref();
101        $code_href = $this->envUtils()->getCodeHref();
102        $termine_url = "{$base_href}{$code_href}termine";
103
104        $deadlines_text = '';
105
106        $criteria = Criteria::create()
107            ->where(
108                Criteria::expr()->andX(
109                    Criteria::expr()->gt('deadline', $today),
110                    Criteria::expr()->lt('deadline', $end_of_timespan),
111                    Criteria::expr()->isNull('deleted_at'),
112                )
113            )
114            ->orderBy(['start_date' => Order::Ascending])
115            ->setFirstResult(0)
116            ->setMaxResults(1000)
117        ;
118        $deadlines = $termin_repo->matching($criteria);
119        foreach ($deadlines as $termin) {
120            $deadline_date = $termin->getDeadline();
121            $date = $deadline_date ? $this->dateUtils()->compactDate($deadline_date) : '';
122            $id = $termin->getId();
123            $title = $this->htmlUtils()->escapeMarkdown($termin->getTitle() ?? '');
124            $deadlines_text .= "- {$date}: Meldeschluss für '[{$title}]({$termine_url}/{$id})'\n";
125        }
126
127        return $deadlines_text;
128    }
129}