Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RecordingEmailService
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 send
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace App\Domain\Notification\Service;
6
7use App\Domain\Notification\Data\EmailMessage;
8use App\Domain\Notification\Repository\SentEmailRepository;
9use Psr\Log\LoggerInterface;
10use Throwable;
11
12/**
13 * Decorates the real {@see EmailServiceInterface} (SMTP) to record every email
14 * the app sends, powering the in-app per-user email history (FSC-110).
15 *
16 * Recording happens AFTER a successful send and is best-effort: a failure to
17 * record is logged at WARNING and swallowed so it can never block — or appear
18 * to fail — an email that was actually delivered.
19 */
20final readonly class RecordingEmailService implements EmailServiceInterface
21{
22    public function __construct(
23        private EmailServiceInterface $inner,
24        private SentEmailRepository $repository,
25        private LoggerInterface $logger,
26    ) {}
27
28    public function send(EmailMessage $message): void
29    {
30        $this->inner->send($message);
31
32        try {
33            $this->repository->record($message);
34        } catch (Throwable $e) {
35            $this->logger->warning('Failed to record sent email', [
36                'recipient' => $message->to,
37                'subject' => $message->subject,
38                'error' => $e->getMessage(),
39            ]);
40        }
41    }
42}