Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
News
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 created
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 updated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sendMail
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Events\Listener;
6
7use Engelsystem\Mail\EngelsystemMailer;
8use Engelsystem\Models\News as NewsModel;
9use Engelsystem\Models\User\Settings as UserSettings;
10use Illuminate\Database\Eloquent\Collection;
11use Psr\Log\LoggerInterface;
12
13class News
14{
15    public function __construct(
16        protected LoggerInterface $log,
17        protected EngelsystemMailer $mailer,
18        protected UserSettings $settings
19    ) {
20    }
21
22    public function created(NewsModel $news, bool $sendNotification = true): void
23    {
24        $this->sendMail($news, 'notification.news.new', 'emails/news-new', $sendNotification);
25    }
26
27    public function updated(NewsModel $news, bool $sendNotification = true): void
28    {
29        $this->sendMail($news, 'notification.news.updated', 'emails/news-updated', $sendNotification);
30    }
31
32    protected function sendMail(NewsModel $news, string $subject, string $template, bool $sendNotification = true): void
33    {
34        if (!$sendNotification) {
35            return;
36        }
37
38        /** @var UserSettings[]|Collection $recipients */
39        $recipients = $this->settings
40            ->with('user.personalData')
41            ->where('email_news', true)
42            ->get();
43
44        foreach ($recipients as $recipient) {
45            $this->mailer->sendViewTranslated(
46                $recipient->user,
47                $subject,
48                $template,
49                ['title' => $news->title, 'news' => $news, 'username' => $recipient->user->displayName]
50            );
51        }
52    }
53}