Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.87% covered (success)
94.87%
37 / 39
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeadlineWarningGetter
94.87% covered (success)
94.87%
37 / 39
0.00% covered (danger)
0.00%
0 / 2
7.01
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
97.37% covered (success)
97.37%
37 / 38
0.00% covered (danger)
0.00%
0 / 1
6
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 DeadlineWarningGetter 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        $days_arg = intval($args['days']);
21        if ($days_arg <= 0 || $days_arg > 7) {
22            return null;
23        }
24        $given_days = \DateInterval::createFromDateString("+{$days_arg} days");
25        $in_given_days = (new \DateTime($this->dateUtils()->getIsoToday()))->add($given_days);
26        $in_given_days_start = new \DateTime($in_given_days->format('Y-m-d').' 00:00:00');
27        $in_given_days_end = new \DateTime($in_given_days->format('Y-m-d').' 23:59:59');
28
29        $termin_repo = $this->entityManager()->getRepository(Termin::class);
30
31        $base_href = $this->envUtils()->getBaseHref();
32        $code_href = $this->envUtils()->getCodeHref();
33        $termine_url = "{$base_href}{$code_href}termine";
34
35        $deadlines_text = '';
36
37        $criteria = Criteria::create()
38            ->where(
39                Criteria::expr()->andX(
40                    Criteria::expr()->gte('deadline', $in_given_days_start),
41                    Criteria::expr()->lte('deadline', $in_given_days_end),
42                    Criteria::expr()->eq('on_off', 1),
43                )
44            )
45            ->orderBy(['start_date' => Order::Ascending])
46            ->setFirstResult(0)
47            ->setMaxResults(1000)
48        ;
49        $deadlines = $termin_repo->matching($criteria);
50        foreach ($deadlines as $termin) {
51            $deadline_date = $termin->getDeadline();
52            $date = $deadline_date ? $this->dateUtils()->compactDate($deadline_date) : '';
53            $id = $termin->getId();
54            $title = $termin->getTitle();
55            $deadlines_text .= "{$date}: Meldeschluss für '[{$title}]({$termine_url}/{$id})'\n";
56        }
57
58        if (strlen($deadlines_text) == 0) {
59            return null;
60        }
61
62        $title = "Meldeschlusswarnung";
63        $text = "Hallo %%userFirstName%%,\n\nFolgende Meldeschlüsse stehen bevor:\n\n{$deadlines_text}";
64
65        return new Notification($title, $text, [
66            'notification_type' => NotificationSubscription::TYPE_DEADLINE_WARNING,
67        ]);
68    }
69}