Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
EngelsystemMailer
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 sendViewTranslated
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
9
 sendView
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 send
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getSubjectPrefix
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSubjectPrefix
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Mail;
6
7use Engelsystem\Helpers\Translation\Translator;
8use Engelsystem\Models\User\User;
9use Engelsystem\Renderer\Renderer;
10use Psr\Log\LoggerInterface;
11use Symfony\Component\Mailer\MailerInterface;
12
13class EngelsystemMailer extends Mailer
14{
15    protected ?Renderer $view = null;
16
17    protected ?Translator $translation = null;
18
19    protected ?string $subjectPrefix = null;
20
21    /**
22     * @param Renderer|null   $view
23     * @param Translator|null $translation
24     */
25    public function __construct(
26        LoggerInterface $log,
27        MailerInterface $mailer,
28        Renderer $view = null,
29        Translator $translation = null
30    ) {
31        parent::__construct($log, $mailer);
32
33        $this->translation = $translation;
34        $this->view = $view;
35    }
36
37    /**
38     * @param string|string[]|User $to
39     */
40    public function sendViewTranslated(
41        string|array|User $to,
42        string $subject,
43        string $template,
44        array $data = [],
45        ?string $locale = null
46    ): bool {
47        if ($to instanceof User) {
48            $locale = $locale ?: $to->settings->language;
49            $to = $to->contact->email ?: $to->email;
50        }
51
52        $activeLocale = null;
53        if (
54            $locale
55            && $this->translation
56            && isset($this->translation->getLocales()[$locale])
57        ) {
58            $activeLocale = $this->translation->getLocale();
59            $this->translation->setLocale($locale);
60        }
61
62        $subject = $this->translation ? $this->translation->translate($subject, $data) : $subject;
63        $status = $this->sendView($to, $subject, $template, $data);
64
65        if ($activeLocale) {
66            $this->translation->setLocale($activeLocale);
67        }
68
69        return $status;
70    }
71
72    /**
73     * Send a template
74     *
75     * @param string|string[] $to
76     */
77    public function sendView(string|array $to, string $subject, string $template, array $data = []): bool
78    {
79        $body = $this->view->render($template, $data);
80
81        return $this->send($to, $subject, $body);
82    }
83
84    /**
85     * Send the mail
86     *
87     * @param string|string[] $to
88     */
89    public function send(string|array $to, string $subject, string $body): bool
90    {
91        if ($this->subjectPrefix) {
92            $subject = sprintf('[%s] %s', $this->subjectPrefix, trim($subject));
93        }
94
95        return parent::send($to, $subject, $body);
96    }
97
98    public function getSubjectPrefix(): string
99    {
100        return $this->subjectPrefix;
101    }
102
103    public function setSubjectPrefix(string $subjectPrefix): void
104    {
105        $this->subjectPrefix = $subjectPrefix;
106    }
107}