Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.11% |
123 / 135 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
DailySummaryGetter | |
91.11% |
123 / 135 |
|
25.00% |
1 / 4 |
26.47 | |
0.00% |
0 / 1 |
autogenerateSubscriptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNotification | |
91.67% |
99 / 108 |
|
0.00% |
0 / 1 |
21.26 | |||
getPrettyDateAndMaybeTime | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
getNewsCriteria | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
1 |
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\News\NewsEntry; |
8 | use Olz\Entity\NotificationSubscription; |
9 | use Olz\Entity\Termine\Termin; |
10 | use Olz\Utils\WithUtilsTrait; |
11 | |
12 | class DailySummaryGetter implements NotificationGetterInterface { |
13 | use WithUtilsTrait; |
14 | |
15 | public const CUT_OFF_TIME = '16:00:00'; |
16 | |
17 | protected \DateTime $today; |
18 | protected \DateTime $yesterday; |
19 | |
20 | public function autogenerateSubscriptions(): void { |
21 | // Must be generated by user. |
22 | } |
23 | |
24 | /** @param array<string, mixed> $args */ |
25 | public function getNotification(array $args): ?Notification { |
26 | $this->today = new \DateTime($this->dateUtils()->getIsoToday()); |
27 | $minus_one_day = \DateInterval::createFromDateString("-1 days"); |
28 | $this->yesterday = (new \DateTime($this->dateUtils()->getIsoToday()))->add($minus_one_day); |
29 | |
30 | $today_at_cut_off = new \DateTime($this->today->format('Y-m-d').' '.self::CUT_OFF_TIME); |
31 | $yesterday_at_cut_off = new \DateTime($this->yesterday->format('Y-m-d').' '.self::CUT_OFF_TIME); |
32 | $termine_criteria = Criteria::create() |
33 | ->where(Criteria::expr()->andX( |
34 | Criteria::expr()->lte('last_modified_at', $today_at_cut_off), |
35 | Criteria::expr()->gt('last_modified_at', $yesterday_at_cut_off), |
36 | Criteria::expr()->eq('newsletter', 1), |
37 | Criteria::expr()->eq('on_off', 1), |
38 | )) |
39 | ->orderBy(['start_date' => Order::Ascending, 'start_time' => Order::Ascending]) |
40 | ->setFirstResult(0) |
41 | ->setMaxResults(1000) |
42 | ; |
43 | |
44 | $notification_text = ''; |
45 | $base_href = $this->envUtils()->getBaseHref(); |
46 | $code_href = $this->envUtils()->getCodeHref(); |
47 | |
48 | if ($args['aktuell'] ?? false) { |
49 | $news_url = "{$base_href}{$code_href}news"; |
50 | $aktuell_text = ''; |
51 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
52 | $aktuell_criteria = $this->getNewsCriteria(['aktuell']); |
53 | $aktuells = $news_repo->matching($aktuell_criteria); |
54 | foreach ($aktuells as $aktuell) { |
55 | $id = $aktuell->getId(); |
56 | $pretty_datetime = $this->getPrettyDateAndMaybeTime( |
57 | $aktuell->getPublishedDate(), |
58 | $aktuell->getPublishedTime() |
59 | ); |
60 | $title = $aktuell->getTitle(); |
61 | $aktuell_text .= "- {$pretty_datetime}: [{$title}]({$news_url}/{$id})\n"; |
62 | } |
63 | if (strlen($aktuell_text) > 0) { |
64 | $notification_text .= "\n**Aktuell**\n\n{$aktuell_text}\n"; |
65 | } |
66 | } |
67 | |
68 | if ($args['blog'] ?? false) { |
69 | $news_url = "{$base_href}{$code_href}news"; |
70 | $blog_text = ''; |
71 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
72 | $blog_criteria = $this->getNewsCriteria(['kaderblog']); |
73 | $blogs = $news_repo->matching($blog_criteria); |
74 | foreach ($blogs as $blog) { |
75 | $id = $blog->getId(); |
76 | $pretty_datetime = $this->getPrettyDateAndMaybeTime( |
77 | $blog->getPublishedDate(), |
78 | $blog->getPublishedTime() |
79 | ); |
80 | $title = $blog->getTitle(); |
81 | $blog_text .= "- {$pretty_datetime}: [{$title}]({$news_url}/{$id})\n"; |
82 | } |
83 | if (strlen($blog_text) > 0) { |
84 | $notification_text .= "\n**Kaderblog**\n\n{$blog_text}\n"; |
85 | } |
86 | } |
87 | |
88 | if ($args['forum'] ?? false) { |
89 | $news_url = "{$base_href}{$code_href}news"; |
90 | $forum_text = ''; |
91 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
92 | $forum_criteria = $this->getNewsCriteria(['forum']); |
93 | $forums = $news_repo->matching($forum_criteria); |
94 | foreach ($forums as $forum) { |
95 | $id = $forum->getId(); |
96 | $pretty_datetime = $this->getPrettyDateAndMaybeTime( |
97 | $forum->getPublishedDate(), |
98 | $forum->getPublishedTime() |
99 | ); |
100 | $title = $forum->getTitle(); |
101 | if (strlen(trim($title)) > 0) { |
102 | $forum_text .= "- {$pretty_datetime}: [{$title}]({$news_url}/{$id})\n"; |
103 | } |
104 | } |
105 | if (strlen($forum_text) > 0) { |
106 | $notification_text .= "\n**Forum**\n\n{$forum_text}\n"; |
107 | } |
108 | } |
109 | |
110 | if ($args['galerie'] ?? false) { |
111 | $news_url = "{$base_href}{$code_href}news"; |
112 | $galerie_text = ''; |
113 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
114 | $galerie_criteria = $this->getNewsCriteria(['galerie', 'video']); |
115 | $galeries = $news_repo->matching($galerie_criteria); |
116 | foreach ($galeries as $galerie) { |
117 | $id = $galerie->getId(); |
118 | $pretty_datetime = $this->getPrettyDateAndMaybeTime( |
119 | $galerie->getPublishedDate(), |
120 | $galerie->getPublishedTime() |
121 | ); |
122 | $title = $galerie->getTitle(); |
123 | $galerie_text .= "- {$pretty_datetime}: [{$title}]({$news_url}/{$id})\n"; |
124 | } |
125 | if (strlen($galerie_text) > 0) { |
126 | $notification_text .= "\n**Galerien**\n\n{$galerie_text}\n"; |
127 | } |
128 | } |
129 | |
130 | if ($args['termine'] ?? false) { |
131 | $termine_url = "{$base_href}{$code_href}termine"; |
132 | $termine_text = ''; |
133 | $termin_repo = $this->entityManager()->getRepository(Termin::class); |
134 | $termine = $termin_repo->matching($termine_criteria); |
135 | foreach ($termine as $termin) { |
136 | $id = $termin->getId(); |
137 | $starts_on = $termin->getStartDate(); |
138 | $ends_on = $termin->getEndDate(); |
139 | $pretty_date = ($ends_on && $ends_on > $starts_on) |
140 | ? $this->dateUtils()->compactDate($starts_on).' - '.$this->dateUtils()->compactDate($ends_on) |
141 | : $this->dateUtils()->compactDate($starts_on); |
142 | $title = $termin->getTitle(); |
143 | if (strlen(trim($title)) > 0) { |
144 | $termine_text .= "- {$pretty_date}: [{$title}]({$termine_url}/{$id})\n"; |
145 | } |
146 | } |
147 | if (strlen($termine_text) > 0) { |
148 | $notification_text .= "\n**Aktualisierte Termine**\n\n{$termine_text}\n"; |
149 | } |
150 | } |
151 | |
152 | if (strlen($notification_text) == 0) { |
153 | return null; |
154 | } |
155 | |
156 | $title = "Tageszusammenfassung"; |
157 | $text = "Hallo %%userFirstName%%,\n\nDas lief heute auf [olzimmerberg.ch](https://olzimmerberg.ch):\n\n{$notification_text}"; |
158 | |
159 | return new Notification($title, $text, [ |
160 | 'notification_type' => NotificationSubscription::TYPE_DAILY_SUMMARY, |
161 | ]); |
162 | } |
163 | |
164 | protected function getPrettyDateAndMaybeTime(?\DateTime $date, ?\DateTime $time = null): string { |
165 | if (!$date) { |
166 | return "??"; |
167 | } |
168 | $pretty_date = $this->dateUtils()->compactDate($date); |
169 | if (!$time) { |
170 | return $pretty_date; |
171 | } |
172 | $pretty_time = $time->format('H:i'); |
173 | return "{$pretty_date} {$pretty_time}"; |
174 | } |
175 | |
176 | /** @param array<string> $formats */ |
177 | protected function getNewsCriteria(array $formats): Criteria { |
178 | return Criteria::create() |
179 | ->where(Criteria::expr()->andX( |
180 | Criteria::expr()->in('format', $formats), |
181 | Criteria::expr()->orX( |
182 | Criteria::expr()->andX( |
183 | Criteria::expr()->eq('published_date', $this->today), |
184 | Criteria::expr()->lte('published_time', new \DateTime(self::CUT_OFF_TIME)), |
185 | ), |
186 | Criteria::expr()->andX( |
187 | Criteria::expr()->eq('published_date', $this->yesterday), |
188 | Criteria::expr()->gt('published_time', new \DateTime(self::CUT_OFF_TIME)), |
189 | ), |
190 | ), |
191 | Criteria::expr()->eq('on_off', 1), |
192 | )) |
193 | ->orderBy(['published_date' => Order::Ascending, 'published_time' => Order::Ascending]) |
194 | ->setFirstResult(0) |
195 | ->setMaxResults(1000) |
196 | ; |
197 | } |
198 | } |