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

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

24 May 2024 11:31AM UTC coverage: 88.732% (-0.6%) from 89.362%
#26

push

Matthias Devlamynck
Allow generating and sending email in two steps

0 of 1 new or added line in 1 file covered. (0.0%)

126 of 142 relevant lines covered (88.73%)

5.23 hits per line

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

94.44
/src/Domain/EmailManager.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace RichId\EmailTemplateBundle\Domain;
6

7
use RichId\EmailTemplateBundle\Domain\Email\AbstractEmail;
8
use RichId\EmailTemplateBundle\Domain\Exception\EmailNotFoundException;
9
use RichId\EmailTemplateBundle\Domain\Internal\InternalEmailManager;
10
use RichId\EmailTemplateBundle\Domain\Model\EmailModelInterface;
11
use RichId\EmailTemplateBundle\Domain\Port\MailerInterface;
12
use Symfony\Component\Mime\Email;
13
use Symfony\Contracts\Service\Attribute\Required;
14

15
final class EmailManager
16
{
17
    #[Required]
18
    public InternalEmailManager $internalEmailManager;
19

20
    #[Required]
21
    public MailerInterface $mailer;
22

23
    /** @phpstan-ignore-next-line */
24
    public function getEmail(string $slug): AbstractEmail
25
    {
26
        $service = $this->internalEmailManager->getCurrentEmailService($slug);
17✔
27

28
        if ($service === null) {
17✔
29
            throw new EmailNotFoundException($slug);
2✔
30
        }
31

32
        return $service;
15✔
33
    }
34

35
    public function send(string $slug, ?EmailModelInterface $data = null): void
36
    {
37
        $email = $this->generateEmail($slug, $data);
4✔
38

39
        if ($email === null) {
2✔
40
            return;
1✔
41
        }
42

43
        $this->mailer->send($email);
1✔
44
    }
45

46
    public function sendEmail(Email $email): void
47
    {
NEW
48
        $this->mailer->send($email);
×
49
    }
50

51
    public function getEmailContent(string $slug, ?EmailModelInterface $data = null): ?string
52
    {
53
        $email = $this->generateEmail($slug, $data);
4✔
54
        $body = $email?->getHtmlBody();
2✔
55

56
        return !empty($body) ? (string) $body : null;
2✔
57
    }
58

59
    public function generateEmail(string $slug, ?EmailModelInterface $data = null): ?Email
60
    {
61
        $emailService = $this->getEmail($slug);
8✔
62

63
        $method = new \ReflectionMethod($emailService, 'getEmail');
6✔
64
        $method->setAccessible(true);
6✔
65

66
        $email = $method->invoke($emailService->setData($data));
6✔
67
        $method->setAccessible(false);
4✔
68

69
        return $email;
4✔
70
    }
71
}
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