• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rich-id / email-template-bundle / #15

pending completion
#15

push

web-flow
<a href="https://github.com/rich-id/email-template-bundle/commit/<a class=hub.com/rich-id/email-template-bundle/commit/<a class="double-link" href="https://git"><a class=hub.com/rich-id/email-template-bundle/commit/<a class="double-link" href="https://git"><a class=hub.com/rich-id/email-template-bundle/commit/<a class="double-link" href="https://git"><a class=hub.com/rich-id/email-template-bundle/commit/<a class="double-link" href="https://git"><a class=hub.com/rich-id/email-template-bundle/commit/c75b9938ea649e91fc02e8535830ceb768b72432">c75b9938e">&lt;a href=&quot;https://github.com/rich-id/email-template-bundle/commit/</a><a class="double-link" href="https://github.com/rich-id/email-template-bundle/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/rich-id/email-template-bundle/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/rich-id/email-template-bundle/commit/&lt;a class=&quot;double-link&quot; href=&quot;https://git">&lt;a class=</a>hub.com/rich-id/email-template-bundle/commit/c75b9938ea649e91fc02e8535830ceb768b72432">c75b9938e</a><a href="https://github.com/rich-id/email-template-bundle/commit/c75b9938ea649e91fc02e8535830ceb768b72432">&lt;a href=&quot;https://github.com/rich-id/email-template-bundle/commit/c75b9938ea649e91fc02e8535830ceb768b72432&quot;&gt;&amp;quot;&amp;gt;&amp;amp;lt;a href=&amp;amp;quot;https://github.com/rich-id/email-template-bundle/commit/&amp;lt;/a&amp;gt;&amp;lt;a class=&amp;quot;double-link&amp;quot; href=&amp;quot;https://github.com/rich-id/email-template-bundle/commit/&amp;amp;lt;a class=&amp;amp;quot;double-link&amp;amp;quot; href=&amp;amp;quot;https://git&amp;quot;&amp;gt;&amp;amp;lt;a class=&amp;lt;/a&amp;gt;hub.com/rich-id/email-template-bundle/commit/c75b9938ea649e91fc02e8535830ceb768b72432&amp;quot;&amp;gt;c75b9938e&amp;lt;/a&amp;gt;&amp;lt;a href=&amp;quot;https://github.com/rich-id/email-template-bundle/commit/c75b9938ea649e91fc02e8535830ceb768b72432&amp;quot;&amp;gt;&amp;amp;lt;a href=&amp;amp;quot;https://github.com/rich-id/email-template-bundle/commit/c75b9938ea649e91fc02e8535830ceb7&amp;lt;/a&amp;gt;68b72432&amp;quot;&amp;gt</a>;&amp;quot;&amp;gt;Merge &amp;lt;/a&amp;gt;&amp;lt;a class=&amp;quot;double-link&amp;quot; href=&amp;quot;https://github.com/rich-id/email-template-bundle/commit/&amp;lt;a class=&amp;quot;double-link&amp;quot; href=&amp;quot;https://github.c... (continued)

2 of 2 new or added lines in 1 file covered. (100.0%)

105 of 117 relevant lines covered (89.74%)

4.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.16
/src/Domain/Email/AbstractEmail.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace RichId\EmailTemplateBundle\Domain\Email;
6

7
use RichId\EmailTemplateBundle\Domain\Attachment\Attachment;
8
use RichId\EmailTemplateBundle\Domain\Constant;
9
use RichId\EmailTemplateBundle\Domain\Email\Trait\EmailDataTrait;
10
use RichId\EmailTemplateBundle\Domain\Exception\InvalidEmailServiceException;
11
use RichId\EmailTemplateBundle\Domain\Fetcher\EmailTemplateFetcher;
12
use RichId\EmailTemplateBundle\Domain\Model\EmailModelInterface;
13
use RichId\EmailTemplateBundle\Domain\Port\TemplatingInterface;
14
use RichId\EmailTemplateBundle\Domain\Port\TranslatorInterface;
15
use Symfony\Component\Mime\Email;
16
use Symfony\Contracts\Service\Attribute\Required;
17

18
/** @template T of EmailModelInterface */
19
abstract class AbstractEmail
20
{
21
    public const TEMPLATES = [Constant::DEFAULT_TEMPLATE];
22
    protected const BODY_TYPE = self::BODY_TYPE_TRANSLATION;
23

24
    protected const BODY_TYPE_TRANSLATION = 'translation';
25
    protected const BODY_TYPE_TWIG = 'twig';
26

27
    protected const TRANSLATION_DOMAIN = 'emails';
28
    protected const TEMPLATING_FOLDER = 'emails';
29

30
    #[Required]
31
    public TemplatingInterface $templating;
32

33
    #[Required]
34
    public TranslatorInterface $translator;
35

36
    #[Required]
37
    public EmailTemplateFetcher $emailTemplateFetcher;
38

39
    /** @var ?T */
40
    protected ?EmailModelInterface $data = null;
41

42
    /**
43
     * @param ?T $data
44
     *
45
     * @return AbstractEmail<T>
46
     */
47
    public function setData(?EmailModelInterface $data): self
48
    {
49
        $this->data = $data;
12✔
50

51
        return $this;
12✔
52
    }
53

54
    abstract public function getEmailSlug(): string;
55

56
    /** @return string[] */
57
    abstract protected function getTo(): array;
58

59
    protected function assertValidParameters(): void
60
    {
61
    }
62

63
    public function getName(): string
64
    {
65
        return $this->translator->trans(
×
66
            \sprintf('%s.name', $this->getEmailSlug()),
×
67
            [],
×
68
            static::TRANSLATION_DOMAIN
69
        );
70
    }
71

72
    /** @return string[] */
73
    protected function getCc(): array
74
    {
75
        return [];
1✔
76
    }
77

78
    /** @return string[] */
79
    protected function getBcc(): array
80
    {
81
        return [];
1✔
82
    }
83

84
    /** @return string[] */
85
    protected function getFrom(): array
86
    {
87
        return [];
1✔
88
    }
89

90
    /** @return Attachment[] */
91
    protected function getAttachments(): array
92
    {
93
        return [];
2✔
94
    }
95

96
    protected function skippedIf(): bool
97
    {
98
        return false;
8✔
99
    }
100

101
    protected function getSubject(): string
102
    {
103
        return $this->translator->trans(
6✔
104
            \sprintf('%s.%s.%s', $this->getEmailSlug(), $this->getTemplateSlug(), 'subject'),
6✔
105
            $this->customSubjectParameters(),
6✔
106
            static::TRANSLATION_DOMAIN
107
        );
108
    }
109

110
    /** @return array<string, mixed> */
111
    protected function customSubjectParameters(): array
112
    {
113
        return [];
6✔
114
    }
115

116
    protected function getBody(): string
117
    {
118
        if (static::BODY_TYPE === static::BODY_TYPE_TRANSLATION) {
6✔
119
            return $this->getTranslationBody();
4✔
120
        }
121

122
        if (static::BODY_TYPE === static::BODY_TYPE_TWIG) {
2✔
123
            return $this->getTwigBody();
1✔
124
        }
125

126
        throw new \InvalidArgumentException(\sprintf('Invalid argument for BODY_TYPE: %s given.', static::BODY_TYPE));
1✔
127
    }
128

129
    protected function getTranslationBody(): string
130
    {
131
        return $this->translator->trans(
4✔
132
            \sprintf('%s.%s.%s', $this->getEmailSlug(), $this->getTemplateSlug(), 'body'),
4✔
133
            $this->customBodyParameters(),
4✔
134
            static::TRANSLATION_DOMAIN
135
        );
136
    }
137

138
    protected function getTwigBody(): string
139
    {
140
        return $this->templating->render(
1✔
141
            \sprintf('%s/%s/%s.html.twig', static::TEMPLATING_FOLDER, $this->getEmailSlug(), $this->getTemplateSlug()),
1✔
142
            $this->customBodyParameters()
1✔
143
        );
144
    }
145

146
    /** @return array<string, mixed> */
147
    protected function customBodyParameters(): array
148
    {
149
        return [];
1✔
150
    }
151

152
    final protected function getEmail(): ?Email
153
    {
154
        $template = $this->getTemplateSlug();
12✔
155

156
        if (!$this->supportTemplate($template)) {
12✔
157
            throw new InvalidEmailServiceException($this::class, $template);
×
158
        }
159

160
        $this->assertValidParameters();
12✔
161

162
        if ($this->skippedIf()) {
9✔
163
            return null;
1✔
164
        }
165

166
        $to = $this->getTo();
8✔
167

168
        if (empty($to)) {
8✔
169
            return null;
2✔
170
        }
171

172
        $email = new Email();
6✔
173
        $email->subject($this->getSubject())
6✔
174
            ->html($this->getBody())
6✔
175
            ->to(...\array_unique($to));
5✔
176

177
        if (!empty($this->getCc())) {
5✔
178
            $email->cc(...\array_unique($this->getCc()));
4✔
179
        }
180

181
        if (!empty($this->getBcc())) {
5✔
182
            $email->bcc(...\array_unique($this->getBcc()));
4✔
183
        }
184

185
        if (!empty($this->getFrom())) {
5✔
186
            $email->from(...\array_unique($this->getFrom()));
4✔
187
        }
188

189
        foreach ($this->getAttachments() as $attachment) {
5✔
190
            if ($attachment instanceof Attachment) {
3✔
191
                $email->attach($attachment->getData(), $attachment->getFilename(), $attachment->getContentType());
3✔
192
            }
193
        }
194

195
        return $email;
5✔
196
    }
197

198
    final public function supportTemplate(string $template): bool
199
    {
200
        return \in_array($template, static::TEMPLATES);
12✔
201
    }
202

203
    final protected function getTemplateSlug(): string
204
    {
205
        return ($this->emailTemplateFetcher)($this->getEmailSlug());
12✔
206
    }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc