1 | <?php |
||
12 | final class PhpMailTransport implements TransportInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var EnvelopeFactory |
||
16 | */ |
||
17 | private $envelopeFactory; |
||
18 | |||
19 | /** |
||
20 | * @var array<int, string> |
||
21 | */ |
||
22 | private $parameters; |
||
23 | |||
24 | /** |
||
25 | * @var \Closure |
||
26 | */ |
||
27 | private $replacedMailMethod; |
||
28 | |||
29 | /** |
||
30 | * @param EnvelopeFactory $envelopeFactory |
||
31 | * @param array<int, string> $parameters |
||
32 | */ |
||
33 | 4 | public function __construct(EnvelopeFactory $envelopeFactory, array $parameters = []) |
|
38 | |||
39 | /** |
||
40 | * @param MessageInterface $message |
||
41 | * @return void |
||
42 | */ |
||
43 | 4 | public function send(MessageInterface $message): void |
|
44 | { |
||
45 | 4 | $to = $this->extractSingleHeader($message, 'to'); |
|
46 | 3 | $subject = $this->extractSingleHeader($message, 'subject'); |
|
47 | 2 | $headers = $this->extractHeaders($message); |
|
48 | 2 | $parameters = $this->constructParameters($message); |
|
49 | |||
50 | 1 | if ($this->replacedMailMethod === null) { |
|
51 | // @codeCoverageIgnoreStart |
||
52 | \mail( |
||
53 | $to, |
||
54 | $subject, |
||
55 | (string)$message->getBody(), |
||
56 | $headers, |
||
57 | $parameters |
||
58 | ); |
||
59 | // @codeCoverageIgnoreEnd |
||
60 | } else { |
||
61 | 1 | $callback = $this->replacedMailMethod; |
|
62 | 1 | $callback( |
|
63 | 1 | $to, |
|
64 | $subject, |
||
65 | 1 | (string)$message->getBody(), |
|
66 | $headers, |
||
67 | $parameters |
||
68 | ); |
||
69 | } |
||
70 | 1 | } |
|
71 | |||
72 | /** |
||
73 | * @param MessageInterface $message |
||
74 | * @param string $name |
||
75 | * @return string |
||
76 | */ |
||
77 | 4 | private function extractSingleHeader(MessageInterface $message, string $name): string |
|
87 | |||
88 | /** |
||
89 | * @param MessageInterface $message |
||
90 | * @return string |
||
91 | */ |
||
92 | 2 | private function extractHeaders(MessageInterface $message): string |
|
125 | |||
126 | /** |
||
127 | * @param MessageInterface $message |
||
128 | * @return string |
||
129 | * @throws EnvelopeException |
||
130 | */ |
||
131 | 2 | private function constructParameters(MessageInterface $message): string |
|
142 | |||
143 | /** |
||
144 | * @param \Closure $callback |
||
145 | * @param EnvelopeFactory $envelopeFactory |
||
146 | * @param array<int, string> $parameters |
||
147 | * @return PhpMailTransport |
||
148 | */ |
||
149 | 4 | public static function newReplaceMailMethod( |
|
158 | } |
||
159 |