EmailTarget   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 16
c 4
b 1
f 0
dl 0
loc 56
ccs 9
cts 9
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 13 2
A __construct() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Log\Target\Email;
6
7
use InvalidArgumentException;
8
use RuntimeException;
9
use Throwable;
10
use Yiisoft\Log\Target;
11
use Yiisoft\Mailer\MailerInterface;
12
13
use function wordwrap;
14
15
/**
16
 * EmailTarget sends selected log messages to the specified email addresses.
17
 *
18
 * {@see EmailTarget::$mailer} is instance of {@see MailerInterface} that sends email and should be already configured.
19
 */
20
final class EmailTarget extends Target
21
{
22
    /**
23
     * @var string|string[] The receiver email address.
24
     * You may pass an array of addresses if multiple recipients should receive this message.
25
     * You may also specify receiver name in addition to email address using format: `[email => name]`.
26
     */
27
    private array|string $emailTo;
28
29
    /**
30
     * @var string The email message subject.
31
     */
32
    private string $subjectEmail;
33
34
    /**
35
     * @param MailerInterface $mailer The mailer instance.
36
     * @param string|string[] $emailTo The receiver email address.
37
     * You may pass an array of addresses if multiple recipients should receive this message.
38
     * You may also specify receiver name in addition to email address using format: `[email => name]`.
39
     * @param string $subjectEmail The email message subject.
40
     * @param string[] $levels The {@see \Psr\Log\LogLevel log message levels} that this target is interested in.
41
     *
42
     * @throws InvalidArgumentException If the "to" email message argument is invalid.
43
     */
44
    public function __construct(
45
        private MailerInterface $mailer,
46
        array|string $emailTo,
47
        string $subjectEmail = '',
48
        array $levels = []
49
    ) {
50
        if (empty($emailTo)) {
51
            throw new InvalidArgumentException('The "to" argument must be an array or string and must not be empty.');
52
        }
53
        $this->emailTo = $emailTo;
54
        $this->subjectEmail = $subjectEmail ?: 'Application Log';
55 11
        parent::__construct($levels);
56
    }
57
58 11
    /**
59 7
     * Sends log messages to specified email addresses.
60
     *
61
     * @throws RuntimeException If the log cannot be exported.
62 4
     */
63 4
    protected function export(): void
64 4
    {
65 4
        $message = $this->mailer
66
            ->compose()
67
            ->withTo($this->emailTo)
68
            ->withSubject($this->subjectEmail)
69
            ->withTextBody(wordwrap($this->formatMessages("\n"), 70))
70
        ;
71
72
        try {
73 4
            $this->mailer->send($message);
74
        } catch (Throwable $e) {
75 4
            throw new RuntimeException('Unable to export log through email.', 0, $e);
76 4
        }
77 4
    }
78
}
79