1 | <?php |
||
12 | final class PhpMailTransport implements TransportInterface |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var EnvelopeFactory |
||
17 | */ |
||
18 | private $envelopeFactory; |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $parameters; |
||
23 | /** |
||
24 | * @var \Closure |
||
25 | */ |
||
26 | private $replacedMailMethod; |
||
27 | |||
28 | /** |
||
29 | * PhpMailTransport constructor. |
||
30 | * @param EnvelopeFactory $envelopeFactory |
||
31 | * @param array $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 | 1 | $subject, |
|
65 | 1 | (string)$message->getBody(), |
|
66 | 1 | $headers, |
|
67 | 1 | $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 |
|
88 | |||
89 | /** |
||
90 | * @param MessageInterface $message |
||
91 | * @return string |
||
92 | */ |
||
93 | 2 | private function extractHeaders(MessageInterface $message): string |
|
94 | { |
||
95 | 2 | return implode("\r\n", |
|
96 | 2 | array_values( |
|
97 | 2 | array_filter( |
|
98 | 2 | array_map( |
|
99 | 2 | function (array $headers) { |
|
100 | 2 | return implode( |
|
101 | 2 | "\r\n", |
|
102 | 2 | array_map( |
|
103 | 2 | function (HeaderInterface $header) { |
|
104 | 2 | return (string)(new HeaderLine($header)); |
|
105 | 2 | }, |
|
106 | 2 | $headers |
|
107 | ) |
||
108 | ); |
||
109 | 2 | }, |
|
110 | $message |
||
111 | 2 | ->withoutHeader('to') |
|
112 | 2 | ->withoutHeader('subject') |
|
113 | 2 | ->getHeaders() |
|
114 | ) |
||
115 | ) |
||
116 | ) |
||
117 | ); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param MessageInterface $message |
||
122 | * @return string |
||
123 | * @throws EnvelopeException |
||
124 | */ |
||
125 | 2 | private function constructParameters(MessageInterface $message): string |
|
136 | |||
137 | /** |
||
138 | * @param \Closure $callback |
||
139 | * @param EnvelopeFactory $envelopeFactory |
||
140 | * @param array $parameters |
||
141 | * @return PhpMailTransport |
||
142 | */ |
||
143 | 4 | public static function newReplaceMailMethod( |
|
153 | } |
||
154 |