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