| Total Complexity | 79 |
| Total Lines | 484 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like SparkPostSwiftTransport 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 SparkPostSwiftTransport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class SparkPostSwiftTransport implements Swift_Transport |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Swift_Transport_SimpleMailInvoker |
||
|
|
|||
| 34 | */ |
||
| 35 | protected $invoker; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Swift_Events_SimpleEventDispatcher |
||
| 39 | */ |
||
| 40 | protected $eventDispatcher; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var LeKoala\SparkPost\Api\SparkPostApiClient |
||
| 44 | */ |
||
| 45 | protected $client; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $resultApi; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $fromEmail; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var boolean |
||
| 59 | */ |
||
| 60 | protected $isStarted = false; |
||
| 61 | |||
| 62 | public function __construct(SparkPostApiClient $client) |
||
| 63 | { |
||
| 64 | $this->client = $client; |
||
| 65 | |||
| 66 | $this->invoker = new \Swift_Transport_SimpleMailInvoker(); |
||
| 67 | $this->eventDispatcher = new \Swift_Events_SimpleEventDispatcher(); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Not used |
||
| 72 | */ |
||
| 73 | public function isStarted() |
||
| 74 | { |
||
| 75 | return $this->isStarted; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Not used |
||
| 80 | */ |
||
| 81 | public function start() |
||
| 82 | { |
||
| 83 | $this->isStarted = true; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Not used |
||
| 88 | */ |
||
| 89 | public function stop() |
||
| 90 | { |
||
| 91 | $this->isStarted = false; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function ping() |
||
| 95 | { |
||
| 96 | return true; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param Swift_Mime_SimpleMessage $message |
||
| 101 | * @param string[] $failedRecipients |
||
| 102 | * @return int Number of messages sent |
||
| 103 | */ |
||
| 104 | public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
||
| 105 | { |
||
| 106 | $this->resultApi = null; |
||
| 107 | if ($event = $this->eventDispatcher->createSendEvent($this, $message)) { |
||
| 108 | $this->eventDispatcher->dispatchEvent($event, 'beforeSendPerformed'); |
||
| 109 | if ($event->bubbleCancelled()) { |
||
| 110 | return 0; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | $sendCount = 0; |
||
| 115 | $disableSending = $message->getHeaders()->has('X-SendingDisabled') || SparkPostHelper::config()->disable_sending; |
||
| 116 | |||
| 117 | $transmissionData = $this->getTransmissionFromMessage($message); |
||
| 118 | |||
| 119 | /* @var $client LeKoala\SparkPost\Api\SparkPostApiClient */ |
||
| 120 | $client = $this->client; |
||
| 121 | |||
| 122 | if ($disableSending) { |
||
| 123 | $result = [ |
||
| 124 | 'total_rejected_recipients' => 0, |
||
| 125 | 'total_accepted_recipients' => 1, |
||
| 126 | 'id' => uniqid(), |
||
| 127 | 'disabled' => true, |
||
| 128 | ]; |
||
| 129 | } else { |
||
| 130 | $result = $client->createTransmission($transmissionData); |
||
| 131 | } |
||
| 132 | $this->resultApi = $result; |
||
| 133 | |||
| 134 | if (SparkPostHelper::config()->enable_logging) { |
||
| 135 | $this->logMessageContent($message, $result); |
||
| 136 | } |
||
| 137 | |||
| 138 | $sendCount = $this->resultApi['total_accepted_recipients']; |
||
| 139 | |||
| 140 | // We don't know which recipients failed, so simply add fromEmail since it's the only one we know |
||
| 141 | if ($this->resultApi['total_rejected_recipients'] > 0) { |
||
| 142 | $failedRecipients[] = $this->fromEmail; |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($event) { |
||
| 146 | if ($sendCount > 0) { |
||
| 147 | $event->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
||
| 148 | } else { |
||
| 149 | $event->setResult(Swift_Events_SendEvent::RESULT_FAILED); |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->eventDispatcher->dispatchEvent($event, 'sendPerformed'); |
||
| 153 | } |
||
| 154 | |||
| 155 | return $sendCount; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Log message content |
||
| 160 | * |
||
| 161 | * @param Swift_Mime_SimpleMessage $message |
||
| 162 | * @param array $results Results from the api |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | protected function logMessageContent(Swift_Mime_SimpleMessage $message, $results = []) |
||
| 166 | { |
||
| 167 | $subject = $message->getSubject(); |
||
| 168 | $body = $message->getBody(); |
||
| 169 | $contentType = $this->getMessagePrimaryContentType($message); |
||
| 170 | |||
| 171 | $logContent = $body; |
||
| 172 | |||
| 173 | // Append some extra information at the end |
||
| 174 | $logContent .= '<hr><pre>Debug infos:' . "\n\n"; |
||
| 175 | $logContent .= 'To : ' . print_r($message->getTo(), true) . "\n"; |
||
| 176 | $logContent .= 'Subject : ' . $subject . "\n"; |
||
| 177 | $logContent .= 'From : ' . print_r($message->getFrom(), true) . "\n"; |
||
| 178 | $logContent .= 'Headers:' . "\n"; |
||
| 179 | foreach ($message->getHeaders()->getAll() as $header) { |
||
| 180 | $logContent .= ' ' . $header->getFieldName() . ': ' . $header->getFieldBody() . "\n"; |
||
| 181 | } |
||
| 182 | if (!empty($params['recipients'])) { |
||
| 183 | $logContent .= 'Recipients : ' . print_r($message->getTo(), true) . "\n"; |
||
| 184 | } |
||
| 185 | $logContent .= 'Results:' . "\n"; |
||
| 186 | foreach ($results as $resultKey => $resultValue) { |
||
| 187 | $logContent .= ' ' . $resultKey . ': ' . $resultValue . "\n"; |
||
| 188 | } |
||
| 189 | $logContent .= '</pre>'; |
||
| 190 | |||
| 191 | $logFolder = SparkPostHelper::getLogFolder(); |
||
| 192 | |||
| 193 | // Generate filename |
||
| 194 | $filter = new FileNameFilter(); |
||
| 195 | $title = substr($filter->filter($subject), 0, 35); |
||
| 196 | $logName = date('Ymd_His') . '_' . $title; |
||
| 197 | |||
| 198 | // Store attachments if any |
||
| 199 | $attachments = $message->getChildren(); |
||
| 200 | if (!empty($attachments)) { |
||
| 201 | $logContent .= '<hr />'; |
||
| 202 | foreach ($attachments as $attachment) { |
||
| 203 | if ($attachment instanceof Swift_Attachment) { |
||
| 204 | $attachmentDestination = $logFolder . '/' . $logName . '_' . $attachment->getFilename(); |
||
| 205 | file_put_contents($attachmentDestination, $attachment->getBody()); |
||
| 206 | $logContent .= 'File : <a href="' . $attachmentDestination . '">' . $attachment->getFilename() . '</a><br/>'; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | // Store it |
||
| 212 | $ext = ($contentType == 'text/html') ? 'html' : 'txt'; |
||
| 213 | $r = file_put_contents($logFolder . '/' . $logName . '.' . $ext, $logContent); |
||
| 214 | |||
| 215 | if (!$r && Director::isDev()) { |
||
| 216 | throw new Exception('Failed to store email in ' . $logFolder); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return LoggerInterface |
||
| 222 | */ |
||
| 223 | public function getLogger() |
||
| 224 | { |
||
| 225 | return Injector::inst()->get(LoggerInterface::class)->withName('SparkPost'); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param Swift_Events_EventListener $plugin |
||
| 230 | */ |
||
| 231 | public function registerPlugin(Swift_Events_EventListener $plugin) |
||
| 232 | { |
||
| 233 | $this->eventDispatcher->bindEventListener($plugin); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | protected function getSupportedContentTypes() |
||
| 240 | { |
||
| 241 | return array( |
||
| 242 | 'text/plain', |
||
| 243 | 'text/html' |
||
| 244 | ); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $contentType |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | protected function supportsContentType($contentType) |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param Swift_Mime_SimpleMessage $message |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | protected function getMessagePrimaryContentType(Swift_Mime_SimpleMessage $message) |
||
| 261 | { |
||
| 262 | $contentType = $message->getContentType(); |
||
| 263 | |||
| 264 | if ($this->supportsContentType($contentType)) { |
||
| 265 | return $contentType; |
||
| 266 | } |
||
| 267 | |||
| 268 | // SwiftMailer hides the content type set in the constructor of Swift_Mime_SimpleMessage as soon |
||
| 269 | // as you add another part to the message. We need to access the protected property |
||
| 270 | // _userContentType to get the original type. |
||
| 271 | $messageRef = new \ReflectionClass($message); |
||
| 272 | if ($messageRef->hasProperty('_userContentType')) { |
||
| 273 | $propRef = $messageRef->getProperty('_userContentType'); |
||
| 274 | $propRef->setAccessible(true); |
||
| 275 | $contentType = $propRef->getValue($message); |
||
| 276 | } |
||
| 277 | |||
| 278 | return $contentType; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param Swift_Mime_Headers_UnstructuredHeader|null $header |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected static function getHeaderValue(Swift_Mime_Header $header = null) |
||
| 286 | { |
||
| 287 | if (!$header) { |
||
| 288 | return ''; |
||
| 289 | } |
||
| 290 | if (method_exists($header, 'getValue')) { |
||
| 291 | return $header->getValue(); |
||
| 292 | } |
||
| 293 | return $header->getFieldBody(); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Convert a Swift Message to a transmission |
||
| 298 | * |
||
| 299 | * @param Swift_Mime_SimpleMessage $message |
||
| 300 | * @return array SparkPost Send Message |
||
| 301 | * @throws \Swift_SwiftException |
||
| 302 | */ |
||
| 303 | public function getTransmissionFromMessage(Swift_Mime_SimpleMessage $message) |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @return null|array |
||
| 509 | */ |
||
| 510 | public function getResultApi() |
||
| 511 | { |
||
| 512 | return $this->resultApi; |
||
| 513 | } |
||
| 514 | } |
||
| 515 |