Passed
Pull Request — master (#21)
by Evgeniy
03:00
created

EmailTarget::composeMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 is_array;
14
use function is_string;
15
use function wordwrap;
16
17
/**
18
 * EmailTarget sends selected log messages to the specified email addresses.
19
 *
20
 * {@see EmailTarget::$mailer} is instance of {@see MailerInterface} that sends email and should be already configured.
21
 */
22
final class EmailTarget extends Target
23
{
24
    /**
25
     * @var MailerInterface The mailer instance.
26
     */
27
    private MailerInterface $mailer;
28
29
    /**
30
     * @var array|string The receiver email address.
31
     * You may pass an array of addresses if multiple recipients should receive this message.
32
     * You may also specify receiver name in addition to email address using format: `[email => name]`.
33
     */
34
    private $emailTo;
35
36
    /**
37
     * @var string The email message subject.
38
     */
39
    private string $subjectEmail;
40
41
    /**
42
     * @param MailerInterface $mailer The mailer instance.
43
     * @param array|string $emailTo The receiver email address.
44
     * You may pass an array of addresses if multiple recipients should receive this message.
45
     * You may also specify receiver name in addition to email address using format: `[email => name]`.
46
     * @param string $subjectEmail The email message subject.
47
     *
48
     * @throws InvalidArgumentException If the "to" email message argument is invalid.
49
     *
50
     * @psalm-suppress DocblockTypeContradiction
51
     */
52 10
    public function __construct(MailerInterface $mailer, $emailTo, string $subjectEmail = '')
53
    {
54 10
        if (empty($emailTo) || (!is_string($emailTo) && !is_array($emailTo))) {
55 7
            throw new InvalidArgumentException('The "to" argument must be an array or string and must not be empty.');
56
        }
57
58 3
        $this->mailer = $mailer;
59 3
        $this->emailTo = $emailTo;
60 3
        $this->subjectEmail = $subjectEmail ?: 'Application Log';
61 3
        parent::__construct();
62 3
    }
63
64
    /**
65
     * Sends log messages to specified email addresses.
66
     *
67
     * @throws RuntimeException If the log cannot be exported.
68
     */
69 3
    protected function export(): void
70
    {
71 3
        $message = $this->mailer->compose()
72 3
            ->setTo($this->emailTo)
73 3
            ->setSubject($this->subjectEmail)
74 3
            ->setTextBody(wordwrap($this->formatMessages("\n"), 70))
75
        ;
76
77
        try {
78 3
            $message->setMailer($this->mailer)->send();
79 1
        } catch (Throwable $e) {
80 1
            throw new RuntimeException('Unable to export log through email.', 0, $e);
81
        }
82 2
    }
83
}
84