Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
AppMailer | |
0.00% |
0 / 30 |
|
0.00% |
0 / 3 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
42 | |||
convertAddress | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
buildSubject | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models; |
6 | |
7 | use PeServer\Core\Collection\Arr; |
8 | use PeServer\Core\Environment; |
9 | use PeServer\Core\Mail\EmailAddress; |
10 | use PeServer\Core\Mail\Mailer; |
11 | use PeServer\Core\Mail\SmtpSetting; |
12 | use PeServer\Core\Text; |
13 | use PeServer\Core\Throws\ArgumentException; |
14 | |
15 | /** |
16 | * アプリケーション側で使用するメール送信機能。 |
17 | */ |
18 | final class AppMailer extends Mailer |
19 | { |
20 | #region variable |
21 | |
22 | private string $overwriteTarget = Text::EMPTY; |
23 | |
24 | /** |
25 | * 件名のヘッダ部分。 |
26 | * |
27 | * 特に指定しない場合は標準のものが使用される。 |
28 | * |
29 | * @var string |
30 | */ |
31 | public string $customSubjectHeader = ''; |
32 | |
33 | #endregion |
34 | |
35 | public function __construct(AppConfiguration $config, Environment $environment) |
36 | { |
37 | $mailSetting = match ($config->setting->mail->mode) { |
38 | 'smtp' => new SmtpSetting( |
39 | $config->setting->mail->smtp->host, |
40 | $config->setting->mail->smtp->port, |
41 | $config->setting->mail->smtp->secure, |
42 | $config->setting->mail->smtp->authentication, |
43 | $config->setting->mail->smtp->userName, |
44 | $config->setting->mail->smtp->password |
45 | ), |
46 | default => throw new ArgumentException($config->setting->mail->mode), |
47 | }; |
48 | parent::__construct($mailSetting); |
49 | |
50 | $fromEmail = $config->setting->config->address->fromEmail; |
51 | $this->fromAddress = new EmailAddress($fromEmail->address, $fromEmail->name); |
52 | $this->returnPath = $config->setting->config->address->returnEmail; |
53 | |
54 | if (!$environment->isProduction() && isset($config->setting->debug)) { |
55 | $target = $config->setting->debug->mailOverwriteTarget; |
56 | if (!Text::isNullOrWhiteSpace($target)) { |
57 | $this->overwriteTarget = $target; |
58 | } |
59 | } |
60 | } |
61 | |
62 | #region Mailer |
63 | |
64 | protected function convertAddress(int $kind, EmailAddress $data): EmailAddress |
65 | { |
66 | $result = parent::convertAddress($kind, $data); |
67 | |
68 | if ($kind != parent::ADDRESS_KIND_TO || Text::isNullOrWhiteSpace($this->overwriteTarget)) { |
69 | return $result; |
70 | } |
71 | |
72 | // 宛先を差し替え |
73 | return new EmailAddress( |
74 | $this->overwriteTarget, |
75 | $result->name . '[差し替え]' . $data->address |
76 | ); |
77 | } |
78 | |
79 | protected function buildSubject(string $subject): string |
80 | { |
81 | $customSubjectHeader = Text::isNullOrWhiteSpace($this->customSubjectHeader) |
82 | ? '[Pe.Server]' |
83 | : $this->customSubjectHeader; |
84 | |
85 | return $customSubjectHeader . ' ' . $subject; |
86 | } |
87 | |
88 | #endregion |
89 | } |