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

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

11 Mar 2024 03:53PM UTC coverage: 89.362% (-0.4%) from 89.764%
#25

push

Matthias Devlamynck
Update dependencies

126 of 141 relevant lines covered (89.36%)

5.27 hits per line

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

88.89
/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\Exception\InvalidEmailServiceException;
10
use RichId\EmailTemplateBundle\Domain\Fetcher\EmailTemplateFetcher;
11
use RichId\EmailTemplateBundle\Domain\Model\EmailModelInterface;
12
use RichId\EmailTemplateBundle\Domain\Port\TemplatingInterface;
13
use RichId\EmailTemplateBundle\Domain\Port\TranslatorInterface;
14
use Symfony\Component\Mime\Email;
15
use Symfony\Contracts\Service\Attribute\Required;
16

17
/** @template T of EmailModelInterface */
18
abstract class AbstractEmail
19
{
20
    public const TEMPLATES = [Constant::DEFAULT_TEMPLATE];
21
    public const EMAIL_SLUG_HEADER = 'abstract-email-slug';
22

23
    protected const BODY_TYPE = self::BODY_TYPE_TRANSLATION;
24

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

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

31
    protected const EMAIL_CLASS = Email::class;
32

33
    #[Required]
34
    public TemplatingInterface $templating;
35

36
    #[Required]
37
    public TranslatorInterface $translator;
38

39
    #[Required]
40
    public EmailTemplateFetcher $emailTemplateFetcher;
41

42
    /** @var ?T */
43
    protected ?EmailModelInterface $data = null;
44

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

54
        return $this;
15✔
55
    }
56

57
    abstract public function getEmailSlug(): string;
58

59
    /** @return string[] */
60
    abstract protected function getTo(): array;
61

62
    protected function assertValidParameters(): void
63
    {
64
    }
6✔
65

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

75
    public function canSeeEmailInAdministration(): bool
76
    {
77
        return true;
×
78
    }
79

80
    /** @return string[] */
81
    protected function getCc(): array
82
    {
83
        return [];
1✔
84
    }
85

86
    /** @return string[] */
87
    protected function getBcc(): array
88
    {
89
        return [];
1✔
90
    }
91

92
    /** @return string[] */
93
    protected function getFrom(): array
94
    {
95
        return [];
1✔
96
    }
97

98
    /** @return Attachment[] */
99
    protected function getAttachments(): array
100
    {
101
        return [];
2✔
102
    }
103

104
    protected function skippedIf(): bool
105
    {
106
        return false;
10✔
107
    }
108

109
    protected function getSubject(): string
110
    {
111
        return $this->translator->trans(
7✔
112
            \sprintf('%s.%s.%s', $this->getEmailSlug(), $this->getTemplateSlug(), 'subject'),
7✔
113
            $this->customSubjectParameters(),
7✔
114
            static::TRANSLATION_DOMAIN
7✔
115
        );
7✔
116
    }
117

118
    /** @return array<string, mixed> */
119
    protected function customSubjectParameters(): array
120
    {
121
        return [];
7✔
122
    }
123

124
    protected function getBody(): string
125
    {
126
        if (static::BODY_TYPE === static::BODY_TYPE_TRANSLATION) {
7✔
127
            return $this->getTranslationBody();
5✔
128
        }
129

130
        if (static::BODY_TYPE === static::BODY_TYPE_TWIG) {
2✔
131
            return $this->getTwigBody();
1✔
132
        }
133

134
        throw new \InvalidArgumentException(\sprintf('Invalid argument for BODY_TYPE: %s given.', static::BODY_TYPE));
1✔
135
    }
136

137
    protected function getTranslationBody(): string
138
    {
139
        return $this->translator->trans(
5✔
140
            \sprintf('%s.%s.%s', $this->getEmailSlug(), $this->getTemplateSlug(), 'body'),
5✔
141
            $this->customBodyParameters(),
5✔
142
            static::TRANSLATION_DOMAIN
5✔
143
        );
5✔
144
    }
145

146
    protected function getTwigBody(): string
147
    {
148
        return $this->templating->render(
1✔
149
            \sprintf('%s/%s/%s.html.twig', static::TEMPLATING_FOLDER, $this->getEmailSlug(), $this->getTemplateSlug()),
1✔
150
            $this->customBodyParameters()
1✔
151
        );
1✔
152
    }
153

154
    /** @return array<string, mixed> */
155
    protected function customBodyParameters(): array
156
    {
157
        return [];
1✔
158
    }
159

160
    protected function emailUpdater(Email $email): void
161
    {
162
    }
6✔
163

164
    final protected function getEmail(): ?Email
165
    {
166
        $template = $this->getTemplateSlug();
15✔
167

168
        if (!$this->supportTemplate($template)) {
15✔
169
            throw new InvalidEmailServiceException($this::class, $template);
×
170
        }
171

172
        $this->assertValidParameters();
15✔
173

174
        if ($this->skippedIf()) {
11✔
175
            return null;
1✔
176
        }
177

178
        $to = $this->getTo();
10✔
179

180
        if (empty($to)) {
10✔
181
            return null;
3✔
182
        }
183

184
        /** @var Email $email */
185
        $email = new (static::EMAIL_CLASS)();
7✔
186
        $email->subject($this->getSubject())
7✔
187
            ->html($this->getBody())
7✔
188
            ->to(...\array_unique($to));
7✔
189

190
        $email->getHeaders()->addTextHeader(self::EMAIL_SLUG_HEADER, $this->getEmailSlug());
6✔
191

192
        if (!empty($this->getCc())) {
6✔
193
            $email->cc(...\array_unique($this->getCc()));
5✔
194
        }
195

196
        if (!empty($this->getBcc())) {
6✔
197
            $email->bcc(...\array_unique($this->getBcc()));
5✔
198
        }
199

200
        if (!empty($this->getFrom())) {
6✔
201
            $email->from(...\array_unique($this->getFrom()));
5✔
202
        }
203

204
        foreach ($this->getAttachments() as $attachment) {
6✔
205
            if ($attachment instanceof Attachment) {
4✔
206
                $email->attach($attachment->getData(), $attachment->getFilename(), $attachment->getContentType());
4✔
207
            }
208
        }
209

210
        $this->emailUpdater($email);
6✔
211

212
        return $email;
6✔
213
    }
214

215
    final public function supportTemplate(string $template): bool
216
    {
217
        return \in_array($template, static::TEMPLATES);
15✔
218
    }
219

220
    final protected function getTemplateSlug(): string
221
    {
222
        return ($this->emailTemplateFetcher)($this->getEmailSlug());
15✔
223
    }
224
}
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