Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.44% |
38 / 39 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
DeadlineWarningGetter | |
97.44% |
38 / 39 |
|
50.00% |
1 / 2 |
7 | |
0.00% |
0 / 1 |
autogenerateSubscriptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNotification | |
100.00% |
38 / 38 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Olz\Command\SendDailyNotificationsCommand; |
4 | |
5 | use Doctrine\Common\Collections\Criteria; |
6 | use Doctrine\Common\Collections\Order; |
7 | use Olz\Entity\NotificationSubscription; |
8 | use Olz\Entity\Termine\Termin; |
9 | use Olz\Utils\WithUtilsTrait; |
10 | |
11 | class 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 | } |