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