Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
EmailMessage | |
100.00% |
17 / 17 |
|
100.00% |
9 / 9 |
13 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setText | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getText | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
clearText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasHtml | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setHtml | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getHtml | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
clearHtml | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Mail; |
6 | |
7 | use PeServer\Core\Text; |
8 | use PeServer\Core\Throws\ArgumentException; |
9 | use PeServer\Core\Throws\InvalidOperationException; |
10 | |
11 | /** |
12 | * メール本文。 |
13 | */ |
14 | class EmailMessage |
15 | { |
16 | /** |
17 | * 生成。 |
18 | * |
19 | * @param string|null $text プレーンテキスト。 |
20 | * @param string|null $html HTML。 |
21 | */ |
22 | public function __construct( |
23 | private ?string $text = null, |
24 | private ?string $html = null |
25 | ) { |
26 | } |
27 | |
28 | #region function |
29 | |
30 | /** |
31 | * プレーンテキストが有効か。 |
32 | * |
33 | * @return boolean |
34 | * @phpstan-assert-if-true non-empty-string $this->text |
35 | */ |
36 | public function hasText(): bool |
37 | { |
38 | return !Text::isNullOrWhiteSpace($this->text); |
39 | } |
40 | |
41 | /** |
42 | * プレーンテキストを設定。 |
43 | * |
44 | * @param non-empty-string $value |
45 | * @return void |
46 | */ |
47 | public function setText(string $value): void |
48 | { |
49 | //@phpstan-ignore-next-line [DOCTYPE] |
50 | if (Text::isNullOrWhiteSpace($value)) { |
51 | throw new ArgumentException('$value'); |
52 | } |
53 | |
54 | $this->text = $value; |
55 | } |
56 | |
57 | /** |
58 | * プレーンテキスト取得。 |
59 | * |
60 | * @return string |
61 | * @throws InvalidOperationException 未設定。 |
62 | */ |
63 | public function getText(): string |
64 | { |
65 | if ($this->hasText()) { |
66 | return $this->text; |
67 | } |
68 | |
69 | throw new InvalidOperationException(); |
70 | } |
71 | |
72 | /** |
73 | * プレーンテキストを破棄。 |
74 | * |
75 | * @return void |
76 | */ |
77 | public function clearText(): void |
78 | { |
79 | $this->text = null; |
80 | } |
81 | |
82 | /** |
83 | * HTMLが有効か。 |
84 | * |
85 | * @return boolean |
86 | * @phpstan-assert-if-true non-empty-string $this->html |
87 | */ |
88 | public function hasHtml(): bool |
89 | { |
90 | return !Text::isNullOrWhiteSpace($this->html); |
91 | } |
92 | |
93 | /** |
94 | * HTMLを設定。 |
95 | * |
96 | * @param non-empty-string $value |
97 | * @return void |
98 | */ |
99 | public function setHtml(string $value): void |
100 | { |
101 | //@phpstan-ignore-next-line [DOCTYPE] |
102 | if (Text::isNullOrWhiteSpace($value)) { |
103 | throw new ArgumentException('$value'); |
104 | } |
105 | |
106 | $this->html = $value; |
107 | } |
108 | |
109 | /** |
110 | * HTML取得。 |
111 | * |
112 | * @return string |
113 | * @throws InvalidOperationException 未設定。 |
114 | */ |
115 | public function getHtml(): string |
116 | { |
117 | if ($this->hasHtml()) { |
118 | return $this->html; |
119 | } |
120 | |
121 | throw new InvalidOperationException(); |
122 | } |
123 | |
124 | /** |
125 | * HTMLを破棄。 |
126 | * |
127 | * @return void |
128 | */ |
129 | public function clearHtml(): void |
130 | { |
131 | $this->html = null; |
132 | } |
133 | |
134 | #endregion |
135 | } |