Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.80% covered (success)
98.80%
82 / 83
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MonthlyPreviewGetter
98.80% covered (success)
98.80%
82 / 83
75.00% covered (warning)
75.00%
3 / 4
15
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%
31 / 31
100.00% covered (success)
100.00%
1 / 1
7
 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 MonthlyPreviewGetter 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        $saturday = 6;
22        if ($current_weekday != $saturday) {
23            return null;
24        }
25        $day_of_month = intval($this->dateUtils()->getCurrentDateInFormat('j'));
26        $total_days_of_month = intval($this->dateUtils()->getCurrentDateInFormat('t'));
27        if ($day_of_month <= $total_days_of_month - 14) {
28            return null; // not yet this month
29        }
30        if ($day_of_month > $total_days_of_month - 7) {
31            return null; // not anymore this month
32        }
33
34        $one_month = \DateInterval::createFromDateString('+1 months');
35        $two_months = \DateInterval::createFromDateString('+2 months');
36        $today = new \DateTime($this->dateUtils()->getIsoToday());
37        $next_month = (new \DateTime($this->dateUtils()->getIsoToday()))->add($one_month);
38        $in_two_months = (new \DateTime($this->dateUtils()->getIsoToday()))->add($two_months);
39        $end_of_timespan = new \DateTime($in_two_months->format('Y-m-01'));
40
41        $notification_text = '';
42        $termine_text = $this->getTermineText($today, $end_of_timespan);
43        if (strlen($termine_text) > 0) {
44            $notification_text .= "\n**Termine**\n\n{$termine_text}\n";
45        }
46        $deadlines_text = $this->getDeadlinesText($today, $end_of_timespan);
47        if (strlen($deadlines_text) > 0) {
48            $notification_text .= "\n**Meldeschlüsse**\n\n{$deadlines_text}\n";
49        }
50
51        if (strlen($notification_text) == 0) {
52            return null;
53        }
54
55        $month_name = $this->dateUtils()->olzDate('MM', $next_month);
56        $title = "Monatsvorschau {$month_name}";
57        $text = "Hallo %%userFirstName%%,\n\nIm {$month_name} haben wir Folgendes auf dem Programm:\n\n{$notification_text}";
58
59        return new Notification($title, $text, [
60            'notification_type' => NotificationSubscription::TYPE_MONTHLY_PREVIEW,
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 = $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 = $termin->getTitle();
123            $deadlines_text .= "{$date}: Meldeschluss für '[{$title}]({$termine_url}/{$id})'\n";
124        }
125
126        return $deadlines_text;
127    }
128}