Total Complexity | 53 |
Total Lines | 263 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like MandrillApiTransport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MandrillApiTransport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class MandrillApiTransport extends AbstractApiTransport |
||
31 | { |
||
32 | private const HOST = 'mandrillapp.com'; |
||
33 | |||
34 | /** |
||
35 | * @var Mandrill |
||
36 | */ |
||
37 | private $apiClient; |
||
38 | |||
39 | private $apiResult; |
||
40 | |||
41 | public function __construct(Mandrill $apiClient, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null) |
||
42 | { |
||
43 | $this->apiClient = $apiClient; |
||
44 | |||
45 | parent::__construct($client, $dispatcher, $logger); |
||
46 | } |
||
47 | |||
48 | public function __toString(): string |
||
49 | { |
||
50 | return sprintf('mandrill+api://%s', $this->getEndpoint()); |
||
51 | } |
||
52 | |||
53 | protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface |
||
54 | { |
||
55 | $disableSending = $email->getHeaders()->has('X-SendingDisabled') || !MandrillHelper::getSendingEnabled(); |
||
56 | |||
57 | // We don't really care about the actual response |
||
58 | $response = new MockResponse(); |
||
59 | if ($disableSending) { |
||
60 | $result = []; |
||
61 | foreach ($email->getTo() as $recipient) { |
||
62 | $result[] = [ |
||
63 | 'email' => $recipient->toString(), |
||
64 | 'status' => 'sent', |
||
65 | 'reject_reason' => '', |
||
66 | '_id' => uniqid(), |
||
67 | 'disabled' => true, |
||
68 | ]; |
||
69 | } |
||
70 | } else { |
||
71 | $payload = $this->getPayload($email, $envelope); |
||
72 | $result = $this->apiClient->messages->send($payload); |
||
73 | |||
74 | $sendCount = 0; |
||
75 | foreach ($result as $item) { |
||
76 | if ($item['status'] === 'sent' || $item['status'] === 'queued') { |
||
77 | $sendCount++; |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $this->apiResult = $result; |
||
83 | |||
84 | $firstRecipient = reset($result); |
||
85 | if ($firstRecipient) { |
||
86 | $sentMessage->setMessageId($firstRecipient['_id']); |
||
87 | } |
||
88 | |||
89 | if (MandrillHelper::getLoggingEnabled()) { |
||
90 | $this->logMessageContent($email, $result); |
||
91 | } |
||
92 | |||
93 | return $response; |
||
94 | } |
||
95 | |||
96 | public function getApiResult() |
||
97 | { |
||
98 | return $this->apiResult; |
||
99 | } |
||
100 | |||
101 | private function getEndpoint(): ?string |
||
102 | { |
||
103 | return ($this->host ?: self::HOST) . ($this->port ? ':' . $this->port : ''); |
||
104 | } |
||
105 | |||
106 | private function getPayload(Email $email, Envelope $envelope): array |
||
107 | { |
||
108 | $message = array_merge(MandrillHelper::config()->default_params, [ |
||
109 | 'html' => $email->getHtmlBody(), |
||
110 | 'text' => $email->getTextBody(), |
||
111 | 'subject' => $email->getSubject(), |
||
112 | 'from_email' => $envelope->getSender()->getAddress(), |
||
113 | 'to' => $this->getRecipients($email, $envelope), |
||
114 | ]); |
||
115 | |||
116 | if ('' !== $envelope->getSender()->getName()) { |
||
117 | $message['from_name'] = $envelope->getSender()->getName(); |
||
118 | } |
||
119 | |||
120 | foreach ($email->getAttachments() as $attachment) { |
||
121 | $headers = $attachment->getPreparedHeaders(); |
||
122 | $disposition = $headers->getHeaderBody('Content-Disposition'); |
||
123 | $att = [ |
||
124 | 'content' => $attachment->bodyToString(), |
||
125 | 'type' => $headers->get('Content-Type')->getBody(), |
||
126 | ]; |
||
127 | if ($name = $headers->getHeaderParameter('Content-Disposition', 'name')) { |
||
128 | $att['name'] = $name; |
||
129 | } |
||
130 | if ('inline' === $disposition) { |
||
131 | $message['images'][] = $att; |
||
132 | } else { |
||
133 | $message['attachments'][] = $att; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | $headersToBypass = ['from', 'to', 'cc', 'bcc', 'subject', 'content-type']; |
||
138 | foreach ($email->getHeaders()->all() as $name => $header) { |
||
139 | if (\in_array($name, $headersToBypass, true)) { |
||
140 | continue; |
||
141 | } |
||
142 | if ($header instanceof TagHeader) { |
||
143 | $message['tags'] = array_merge( |
||
144 | $message['tags'] ?? [], |
||
145 | explode(',', $header->getValue()) |
||
146 | ); |
||
147 | continue; |
||
148 | } |
||
149 | if ($header instanceof MetadataHeader) { |
||
150 | $message['metadata'][$header->getKey()] = $header->getValue(); |
||
151 | continue; |
||
152 | } |
||
153 | $message['headers'][$header->getName()] = $header->getBodyAsString(); |
||
154 | } |
||
155 | |||
156 | foreach ($email->getHeaders()->all() as $name => $header) { |
||
157 | if (!($header instanceof UnstructuredHeader)) { |
||
158 | continue; |
||
159 | } |
||
160 | $headerValue = $header->getValue(); |
||
161 | switch ($name) { |
||
162 | case 'List-Unsubscribe': |
||
163 | $message['headers']['List-Unsubscribe'] = $headerValue; |
||
164 | break; |
||
165 | case 'X-MC-InlineCSS': |
||
166 | $message['inline_css'] = $headerValue; |
||
167 | break; |
||
168 | case 'X-MC-Tags': |
||
169 | $tags = $headerValue; |
||
170 | if (!is_array($tags)) { |
||
171 | $tags = explode(',', $tags); |
||
172 | } |
||
173 | $message['tags'] = $tags; |
||
174 | break; |
||
175 | case 'X-MC-Autotext': |
||
176 | $autoText = $headerValue; |
||
177 | if (in_array($autoText, array('true', 'on', 'yes', 'y', true), true)) { |
||
178 | $message['auto_text'] = true; |
||
179 | } |
||
180 | if (in_array($autoText, array('false', 'off', 'no', 'n', false), true)) { |
||
181 | $message['auto_text'] = false; |
||
182 | } |
||
183 | break; |
||
184 | case 'X-MC-GoogleAnalytics': |
||
185 | $analyticsDomains = explode(',', $headerValue); |
||
186 | if (is_array($analyticsDomains)) { |
||
187 | $message['google_analytics_domains'] = $analyticsDomains; |
||
188 | } |
||
189 | break; |
||
190 | case 'X-MC-GoogleAnalyticsCampaign': |
||
191 | $message['google_analytics_campaign'] = $headerValue; |
||
192 | break; |
||
193 | default: |
||
194 | if (strncmp($header->getName(), 'X-', 2) === 0) { |
||
195 | $message['headers'][$header->getName()] = $headerValue; |
||
196 | } |
||
197 | break; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | return $message; |
||
202 | } |
||
203 | |||
204 | protected function getRecipients(Email $email, Envelope $envelope): array |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Log message content |
||
232 | * |
||
233 | * @param Email $message |
||
234 | * @param array $results Results from the api |
||
235 | * @throws Exception |
||
236 | */ |
||
237 | protected function logMessageContent(Email $message, $results = []) |
||
296 |