1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Transport; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\AddressList; |
7
|
|
|
use Genkgo\Mail\EmailAddress; |
8
|
|
|
use Genkgo\Mail\MessageInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class EnvelopOptions |
12
|
|
|
* @package Genkgo\Mail\Transport |
13
|
|
|
*/ |
14
|
|
|
final class EnvelopeFactory |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Closure |
18
|
|
|
*/ |
19
|
|
|
private $callback; |
20
|
|
|
/** |
21
|
|
|
* @var EmailAddress |
22
|
|
|
*/ |
23
|
|
|
private $fallback; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* EnvelopOptions constructor. |
27
|
|
|
*/ |
28
|
14 |
|
private function __construct() |
29
|
|
|
{ |
30
|
14 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param EmailAddress $emailAddress |
34
|
|
|
* @return EnvelopeFactory |
35
|
|
|
*/ |
36
|
3 |
|
public function withFallback(EmailAddress $emailAddress): EnvelopeFactory |
37
|
|
|
{ |
38
|
3 |
|
$clone = clone $this; |
39
|
3 |
|
$clone->fallback = $emailAddress; |
40
|
3 |
|
return $clone; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param MessageInterface $message |
45
|
|
|
* @return EmailAddress |
46
|
|
|
*/ |
47
|
12 |
|
public function make(MessageInterface $message): EmailAddress |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
12 |
|
return $this->callback->call($this, $message); |
51
|
4 |
|
} catch (\RuntimeException $e) { |
52
|
3 |
|
if ($this->fallback === null) { |
53
|
1 |
|
throw $e; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
return $this->fallback; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param MessageInterface $message |
62
|
|
|
* @return EmailAddress |
63
|
|
|
*/ |
64
|
8 |
|
private function extractHeader(MessageInterface $message): EmailAddress |
65
|
|
|
{ |
66
|
8 |
|
foreach (['sender', 'from'] as $header) { |
67
|
8 |
|
if ($message->hasHeader($header)) { |
68
|
|
|
try { |
69
|
6 |
|
return $this->extractFromAddressListHeader($message, $header); |
70
|
1 |
|
} catch (\OutOfRangeException $e) { |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
throw new \RuntimeException('Cannot extract envelope from headers'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param MessageInterface $message |
80
|
|
|
* @param string $headerName |
81
|
|
|
* @return EmailAddress |
82
|
|
|
*/ |
83
|
6 |
|
private function extractFromAddressListHeader (MessageInterface $message, string $headerName): EmailAddress |
84
|
|
|
{ |
85
|
6 |
|
return AddressList::fromString( |
86
|
6 |
|
(string)$message->getHeader($headerName)[0]->getValue()->getRaw() |
87
|
6 |
|
)->first()->getAddress(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return EnvelopeFactory |
92
|
|
|
*/ |
93
|
10 |
|
public static function useExtractedHeader() |
94
|
|
|
{ |
95
|
10 |
|
$options = new self(); |
96
|
10 |
|
$options->callback = \Closure::fromCallable([$options, 'extractHeader']); |
97
|
10 |
|
return $options; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param EmailAddress $fixedAddress |
102
|
|
|
* @return EnvelopeFactory |
103
|
|
|
*/ |
104
|
1 |
|
public static function useFixed(EmailAddress $fixedAddress) |
105
|
|
|
{ |
106
|
1 |
|
$options = new self(); |
107
|
1 |
|
$options->callback = function () use ($fixedAddress) { |
108
|
1 |
|
return $fixedAddress; |
109
|
|
|
}; |
110
|
1 |
|
return $options; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param \Closure $callback |
115
|
|
|
* @return EnvelopeFactory |
116
|
|
|
*/ |
117
|
3 |
|
public static function useCallback(\Closure $callback) |
118
|
|
|
{ |
119
|
3 |
|
$options = new self(); |
120
|
3 |
|
$options->callback = $callback; |
121
|
3 |
|
return $options; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |