| Total Complexity | 69 |
| Total Lines | 533 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Mail 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 Mail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Mail extends GmailConnection |
||
| 18 | { |
||
| 19 | use HasDecodableBody, |
||
| 20 | Modifiable, |
||
| 21 | Replyable { |
||
| 22 | Replyable::__construct as private __rConstruct; |
||
| 23 | Modifiable::__construct as private __mConstruct; |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var |
||
| 28 | */ |
||
| 29 | public $id; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var |
||
| 33 | */ |
||
| 34 | public $internalDate; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var |
||
| 38 | */ |
||
| 39 | public $labels; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var |
||
| 43 | */ |
||
| 44 | public $size; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var |
||
| 48 | */ |
||
| 49 | public $threatId; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \Google_Service_Gmail_MessagePart |
||
| 53 | */ |
||
| 54 | public $payload; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Google_Service_Gmail |
||
| 58 | */ |
||
| 59 | public $service; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * SingleMessage constructor. |
||
| 63 | * |
||
| 64 | * @param \Google_Service_Gmail_Message $message |
||
| 65 | * @param bool $preload |
||
| 66 | * |
||
| 67 | */ |
||
| 68 | public function __construct( \Google_Service_Gmail_Message $message = null, $preload = false ) |
||
| 69 | { |
||
| 70 | |||
| 71 | $this->service = new Google_Service_Gmail( $this ); |
||
| 72 | |||
| 73 | $this->__rConstruct(); |
||
| 74 | $this->__mConstruct(); |
||
| 75 | parent::__construct( config() ); |
||
| 76 | |||
| 77 | if ( ! is_null( $message ) ) { |
||
| 78 | if ( $preload ) { |
||
| 79 | $message = $this->service->users_messages->get( 'me', $message->getId() ); |
||
| 80 | } |
||
| 81 | |||
| 82 | $this->id = $message->getId(); |
||
| 83 | $this->internalDate = $message->getInternalDate(); |
||
| 84 | $this->labels = $message->getLabelIds(); |
||
| 85 | $this->size = $message->getSizeEstimate(); |
||
| 86 | $this->threatId = $message->getThreadId(); |
||
| 87 | $this->payload = $message->getPayload(); |
||
| 88 | |||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns ID of the email |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getId() |
||
| 98 | { |
||
| 99 | return $this->id; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Return a UNIX version of the date |
||
| 104 | * |
||
| 105 | * @return int UNIX date |
||
| 106 | */ |
||
| 107 | public function getInternalDate() |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns the labels of the email |
||
| 114 | * Example: INBOX, STARRED, UNREAD |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function getLabels() |
||
| 119 | { |
||
| 120 | return $this->labels; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns approximate size of the email |
||
| 125 | * |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | public function getSize() |
||
| 129 | { |
||
| 130 | return $this->size; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns threat ID of the email |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function getThreatId() |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns all the headers of the email |
||
| 145 | * |
||
| 146 | * @return Collection |
||
| 147 | */ |
||
| 148 | public function getHeaders() |
||
| 149 | { |
||
| 150 | return $this->buildHeaders( $this->payload->getHeaders() ); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the subject of the email |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function getSubject() |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns array of name and email of each recipient |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public function getFrom() |
||
| 169 | { |
||
| 170 | $from = $this->getHeader( 'From' ); |
||
| 171 | |||
| 172 | preg_match( '/<(.*)>/', $from, $matches ); |
||
| 173 | |||
| 174 | $name = preg_replace( '/ <(.*)>/', '', $from ); |
||
| 175 | |||
| 176 | return [ |
||
| 177 | 'name' => $name, |
||
| 178 | 'email' => isset( $matches[ 1 ] ) ? $matches[ 1 ] : null, |
||
| 179 | ]; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns email of sender |
||
| 184 | * |
||
| 185 | * @return string|null |
||
| 186 | */ |
||
| 187 | public function getFromEmail() |
||
| 188 | { |
||
| 189 | $from = $this->getHeader( 'From' ); |
||
| 190 | |||
| 191 | preg_match( '/<(.*)>/', $from, $matches ); |
||
| 192 | |||
| 193 | return isset( $matches[ 1 ] ) ? $matches[ 1 ] : null; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns name of the sender |
||
| 198 | * |
||
| 199 | * @return string|null |
||
| 200 | */ |
||
| 201 | public function getFromName() |
||
| 202 | { |
||
| 203 | $from = $this->getHeader( 'From' ); |
||
| 204 | |||
| 205 | $name = preg_replace( '/ <(.*)>/', '', $from ); |
||
| 206 | |||
| 207 | return $name; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns array list of recipients |
||
| 212 | * |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function getTo() |
||
| 216 | { |
||
| 217 | $allTo = $this->getHeader( 'To' ); |
||
| 218 | |||
| 219 | return $this->formatEmailList( $allTo ); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the original date that the email was sent |
||
| 224 | * |
||
| 225 | * @return Carbon |
||
| 226 | */ |
||
| 227 | public function getDate() |
||
| 228 | { |
||
| 229 | return Carbon::parse( $this->getHeader( 'Date' ) ); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns email of the original recipient |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function getDeliveredTo() |
||
| 238 | { |
||
| 239 | return $this->getHeader( 'Delivered-To' ); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param bool $raw |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getPlainTextBody( $raw = false ) |
||
| 248 | { |
||
| 249 | $content = $this->getBody(); |
||
| 250 | |||
| 251 | return $raw ? $content : $this->getDecodedBody( $content ); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return string base64 version of the body |
||
| 256 | */ |
||
| 257 | public function getRawPlainTextBody() |
||
| 258 | { |
||
| 259 | return $this->getPlainTextBody( true ); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param bool $raw |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function getHtmlBody( $raw = false ) |
||
| 268 | { |
||
| 269 | $content = $this->getBody( 'text/html' ); |
||
| 270 | |||
| 271 | return $raw ? $content : $this->getDecodedBody( $content ); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return string base64 version of the body |
||
| 276 | */ |
||
| 277 | public function getRawHtmlBody() |
||
| 278 | { |
||
| 279 | return $this->getHtmlBody( true ); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns a collection of attachments |
||
| 284 | * |
||
| 285 | * @param bool $preload Preload the attachment's data |
||
| 286 | * |
||
| 287 | * @return Collection |
||
| 288 | * @throws \Exception |
||
| 289 | */ |
||
| 290 | public function getAttachments( $preload = false ) |
||
| 291 | { |
||
| 292 | $attachments = new Collection( [] ); |
||
| 293 | $parts = $this->payload->getParts(); |
||
| 294 | |||
| 295 | /** @var \Google_Service_Gmail_MessagePart $part */ |
||
| 296 | foreach ( $parts as $part ) { |
||
| 297 | |||
| 298 | $body = $part->getBody(); |
||
| 299 | |||
| 300 | if ( $body->getAttachmentId() ) { |
||
| 301 | $attachment = ( new Attachment( $this->getId(), $part ) ); |
||
| 302 | if ( $preload ) { |
||
| 303 | $attachment = $attachment->getData(); |
||
| 304 | } |
||
| 305 | $attachments->push( |
||
| 306 | $attachment |
||
| 307 | ); |
||
| 308 | } |
||
| 309 | |||
| 310 | } |
||
| 311 | |||
| 312 | return $attachments; |
||
| 313 | |||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return Collection |
||
| 318 | * @throws \Exception |
||
| 319 | */ |
||
| 320 | public function getAttachmentsWithData() |
||
| 321 | { |
||
| 322 | return $this->getAttachments( true ); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return boolean |
||
| 327 | */ |
||
| 328 | public function hasAttachments() |
||
| 329 | { |
||
| 330 | $attachments = 0; |
||
| 331 | $parts = $this->payload->getParts(); |
||
| 332 | |||
| 333 | /** @var \Google_Service_Gmail_MessagePart $part */ |
||
| 334 | foreach ( $parts as $part ) { |
||
| 335 | $body = $part->getBody(); |
||
| 336 | if ( $body->getAttachmentId() ) { |
||
| 337 | $attachments ++; |
||
| 338 | break; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | return ! ! $attachments; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $type |
||
| 347 | * |
||
| 348 | * @return \Google_Service_Gmail_MessagePart|null |
||
| 349 | */ |
||
| 350 | private function getBodyPart( $type = 'text/plain' ) |
||
| 351 | { |
||
| 352 | $body = $this->payload->getParts(); |
||
| 353 | |||
| 354 | |||
| 355 | if ( $this->hasAttachments() ) { |
||
| 356 | //Get the first attachment that is the main body |
||
| 357 | $body = isset( $body[ 0 ] ) ? $body[ 0 ] : []; |
||
| 358 | $parts = $body->getParts(); |
||
| 359 | } else { |
||
| 360 | $parts = $body; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** @var \Google_Service_Gmail_MessagePart $part */ |
||
| 364 | foreach ( $parts as $part ) { |
||
| 365 | if ( $part->getMimeType() === $type ) { |
||
| 366 | break; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | return isset($part) ? $part : null; |
||
| 371 | |||
| 372 | } |
||
| 373 | |||
| 374 | public function getBody( $type = 'text/plain' ) |
||
| 375 | { |
||
| 376 | $part = $this->getBodyPart( $type ); |
||
| 377 | $body = $part->getBody(); |
||
| 378 | |||
| 379 | return $body->getData(); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get's the gmail information from the Mail |
||
| 384 | * |
||
| 385 | * @return Mail |
||
| 386 | */ |
||
| 387 | public function load() |
||
| 388 | { |
||
| 389 | $message = $this->service->users_messages->get( 'me', $this->getId() ); |
||
| 390 | |||
| 391 | return new self( $message ); |
||
| 392 | } |
||
| 393 | |||
| 394 | private function buildHeaders( $emailHeaders ) |
||
| 395 | { |
||
| 396 | $headers = []; |
||
| 397 | |||
| 398 | foreach ( $emailHeaders as $header ) { |
||
| 399 | /** @var \Google_Service_Gmail_MessagePartHeader $header */ |
||
| 400 | |||
| 401 | $head = new \stdClass(); |
||
| 402 | |||
| 403 | $head->key = $header->getName(); |
||
| 404 | $head->value = $header->getValue(); |
||
| 405 | |||
| 406 | $headers[] = $head; |
||
| 407 | } |
||
| 408 | |||
| 409 | return collect( $headers ); |
||
| 410 | |||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Returns an array of emails from an string in RFC 822 format |
||
| 415 | * |
||
| 416 | * @param string $emails email list in RFC 822 format |
||
| 417 | * |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | public function formatEmailList( $emails ) |
||
| 421 | { |
||
| 422 | $all = []; |
||
| 423 | $explodedEmails = explode( ',', $emails ); |
||
| 424 | |||
| 425 | foreach ( $explodedEmails as $email ) { |
||
| 426 | |||
| 427 | $item = []; |
||
| 428 | |||
| 429 | preg_match( '/<(.*)>/', $email, $matches ); |
||
| 430 | |||
| 431 | $item[ 'email' ] = str_replace( ' ', '', isset( $matches[ 1 ] ) ? $matches[ 1 ] : $email ); |
||
| 432 | |||
| 433 | $name = preg_replace( '/ <(.*)>/', '', $email ); |
||
| 434 | |||
| 435 | if ( starts_with( $name, ' ' ) ) { |
||
| 436 | $name = substr( $name, 1 ); |
||
| 437 | } |
||
| 438 | |||
| 439 | $item[ 'name' ] = str_replace( "\"", '', $name ?: null ); |
||
| 440 | |||
| 441 | $all[] = $item; |
||
| 442 | |||
| 443 | } |
||
| 444 | |||
| 445 | return $all; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Sets the access token in case we wanna use a different token |
||
| 450 | * |
||
| 451 | * @param string $token |
||
| 452 | * |
||
| 453 | * @return Mail |
||
| 454 | */ |
||
| 455 | public function using( $token ) |
||
| 456 | { |
||
| 457 | $this->setToken($token); |
||
| 458 | |||
| 459 | return $this; |
||
| 460 | } |
||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | /* added by buckfuddey */ |
||
| 465 | |||
| 466 | public function hasNoParts(){ |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | |||
| 475 | public function extractFromBody(){ |
||
| 514 | } |
||
| 515 | } |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | public function traverseData($parts){ |
||
| 543 | } |
||
| 544 | } |
||
| 545 | } |
||
| 546 | |||
| 547 | public function getDecodedBody( $content ) { |
||
| 550 | } |
||
| 551 | |||
| 552 | } |
||
| 553 |