summaryrefslogtreecommitdiff
path: root/src/Mail/EngelsystemMailer.php
blob: 87915d673d527b10cfee696c1f45b31e852b5e67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

namespace Engelsystem\Mail;

use Engelsystem\Helpers\Translation\Translator;
use Engelsystem\Models\User\User;
use Engelsystem\Renderer\Renderer;
use Swift_Mailer as SwiftMailer;

class EngelsystemMailer extends Mailer
{
    /** @var Renderer|null */
    protected $view;

    /** @var Translator|null */
    protected $translation;

    /** @var string */
    protected $subjectPrefix = null;

    /**
     * @param SwiftMailer $mailer
     * @param Renderer    $view
     * @param Translator  $translation
     */
    public function __construct(SwiftMailer $mailer, Renderer $view = null, Translator $translation = null)
    {
        parent::__construct($mailer);

        $this->translation = $translation;
        $this->view = $view;
    }

    /**
     * @param string|string[]|User $to
     * @param string               $subject
     * @param string               $template
     * @param array                $data
     * @param string|null          $locale
     * @return int
     */
    public function sendViewTranslated(
        $to,
        string $subject,
        string $template,
        array $data = [],
        ?string $locale = null
    ): int {
        if ($to instanceof User) {
            $locale = $locale ?: $to->settings->language;
            $to = $to->contact->email ? $to->contact->email : $to->email;
        }

        $activeLocale = null;
        if (
            $locale
            && $this->translation
            && isset($this->translation->getLocales()[$locale])
        ) {
            $activeLocale = $this->translation->getLocale();
            $this->translation->setLocale($locale);
        }

        $subject = $this->translation ? $this->translation->translate($subject) : $subject;
        $sentMails = $this->sendView($to, $subject, $template, $data);

        if ($activeLocale) {
            $this->translation->setLocale($activeLocale);
        }

        return $sentMails;
    }

    /**
     * Send a template
     *
     * @param string|string[] $to
     * @param string          $subject
     * @param string          $template
     * @param array           $data
     * @return int
     */
    public function sendView($to, string $subject, string $template, array $data = []): int
    {
        $body = $this->view->render($template, $data);

        return $this->send($to, $subject, $body);
    }

    /**
     * Send the mail
     *
     * @param string|string[] $to
     * @param string          $subject
     * @param string          $body
     * @return int
     */
    public function send($to, string $subject, string $body): int
    {
        if ($this->subjectPrefix) {
            $subject = sprintf('[%s] %s', $this->subjectPrefix, $subject);
        }

        return parent::send($to, $subject, $body);
    }

    /**
     * @return string
     */
    public function getSubjectPrefix(): string
    {
        return $this->subjectPrefix;
    }

    /**
     * @param string $subjectPrefix
     */
    public function setSubjectPrefix(string $subjectPrefix)
    {
        $this->subjectPrefix = $subjectPrefix;
    }
}