| Total Complexity | 46 |
| Total Lines | 621 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Attachment 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 Attachment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | final class Attachment |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * The fallback text to use for clients that don't support attachments. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $fallback = ' '; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Optional text that should appear within the attachment. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $text = ' '; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Optional image that should appear within the attachment. |
||
| 37 | * |
||
| 38 | * @var string|null |
||
| 39 | */ |
||
| 40 | private $imageUrl; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Optional thumbnail that should appear within the attachment. |
||
| 44 | * |
||
| 45 | * @var string|null |
||
| 46 | */ |
||
| 47 | private $thumbUrl; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Optional text that should appear above the formatted data. |
||
| 51 | * |
||
| 52 | * @var string|null |
||
| 53 | */ |
||
| 54 | private $pretext; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Optional title for the attachment. |
||
| 58 | * |
||
| 59 | * @var string|null |
||
| 60 | */ |
||
| 61 | private $title; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Optional title link for the attachment. |
||
| 65 | * |
||
| 66 | * @var string|null |
||
| 67 | */ |
||
| 68 | private $titleLink; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Optional author name for the attachment. |
||
| 72 | * |
||
| 73 | * @var string|null |
||
| 74 | */ |
||
| 75 | private $authorName; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Optional author link for the attachment. |
||
| 79 | * |
||
| 80 | * @var string|null |
||
| 81 | */ |
||
| 82 | private $authorLink; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Optional author icon for the attachment. |
||
| 86 | * |
||
| 87 | * @var string|null |
||
| 88 | */ |
||
| 89 | private $authorIcon; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The color to use for the attachment. |
||
| 93 | * |
||
| 94 | * @var string|null |
||
| 95 | */ |
||
| 96 | private $color = 'good'; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The text to use for the attachment footer. |
||
| 100 | * |
||
| 101 | * @var string|null |
||
| 102 | */ |
||
| 103 | private $footer; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The icon to use for the attachment footer. |
||
| 107 | * |
||
| 108 | * @var string|null |
||
| 109 | */ |
||
| 110 | private $footerIcon; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The timestamp to use for the attachment. |
||
| 114 | * |
||
| 115 | * @var \DateTime|null |
||
| 116 | */ |
||
| 117 | private $timestamp; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * The fields of the attachment. |
||
| 121 | * |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | private $fields = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The fields of the attachment that Slack should interpret |
||
| 128 | * with its Markdown-like language. |
||
| 129 | * |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private $markdownFields = []; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * A collection of actions (buttons) to include in the attachment. |
||
| 136 | * A maximum of 5 actions may be provided. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private $actions = []; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function getFallback(): string |
||
| 146 | { |
||
| 147 | return $this->fallback; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Set the fallback text. |
||
| 152 | * |
||
| 153 | * @param string $fallback |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function setFallback(string $fallback) |
||
| 158 | { |
||
| 159 | $this->fallback = $fallback; |
||
| 160 | |||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getText(): string |
||
| 168 | { |
||
| 169 | return $this->text; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set the optional text to appear within the attachment. |
||
| 174 | * |
||
| 175 | * @param string $text |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | public function setText(string $text) |
||
| 180 | { |
||
| 181 | $this->text = $text; |
||
| 182 | |||
| 183 | return $this; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return string|null |
||
| 188 | */ |
||
| 189 | public function getImageUrl(): ?string |
||
| 190 | { |
||
| 191 | return $this->imageUrl; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set the optional image to appear within the attachment. |
||
| 196 | * |
||
| 197 | * @param string|null $imageUrl |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setImageUrl(?string $imageUrl) |
||
| 202 | { |
||
| 203 | $this->imageUrl = $imageUrl; |
||
| 204 | |||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string|null |
||
| 210 | */ |
||
| 211 | public function getThumbUrl(): ?string |
||
| 212 | { |
||
| 213 | return $this->thumbUrl; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Set the optional thumbnail to appear within the attachment. |
||
| 218 | * |
||
| 219 | * @param string|null $thumbUrl |
||
| 220 | * |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | public function setThumbUrl(?string $thumbUrl) |
||
| 224 | { |
||
| 225 | $this->thumbUrl = $thumbUrl; |
||
| 226 | |||
| 227 | return $this; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return string|null |
||
| 232 | */ |
||
| 233 | public function getPretext(): ?string |
||
| 234 | { |
||
| 235 | return $this->pretext; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set the text that should appear above the formatted data. |
||
| 240 | * |
||
| 241 | * @param string|null $pretext |
||
| 242 | * |
||
| 243 | * @return $this |
||
| 244 | */ |
||
| 245 | public function setPretext(?string $pretext) |
||
| 246 | { |
||
| 247 | $this->pretext = $pretext; |
||
| 248 | |||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return string|null |
||
| 254 | */ |
||
| 255 | public function getColor(): ?string |
||
| 256 | { |
||
| 257 | return $this->color; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set the color to use for the attachment. |
||
| 262 | * |
||
| 263 | * @param string|null $color |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function setColor(?string $color) |
||
| 268 | { |
||
| 269 | $this->color = $color; |
||
| 270 | |||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return string|null |
||
| 276 | */ |
||
| 277 | public function getFooter(): ?string |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Set the footer text to use for the attachment. |
||
| 284 | * |
||
| 285 | * @param string|null $footer |
||
| 286 | * |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function setFooter(?string $footer) |
||
| 290 | { |
||
| 291 | $this->footer = $footer; |
||
| 292 | |||
| 293 | return $this; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return string|null |
||
| 298 | */ |
||
| 299 | public function getFooterIcon(): ?string |
||
| 300 | { |
||
| 301 | return $this->footerIcon; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Set the footer icon to use for the attachment. |
||
| 306 | * |
||
| 307 | * @param string|null $footerIcon |
||
| 308 | * |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | public function setFooterIcon(?string $footerIcon) |
||
| 312 | { |
||
| 313 | $this->footerIcon = $footerIcon; |
||
| 314 | |||
| 315 | return $this; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return \DateTime|null |
||
| 320 | */ |
||
| 321 | public function getTimestamp(): ?\DateTime |
||
| 322 | { |
||
| 323 | return $this->timestamp; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set the timestamp to use for the attachment. |
||
| 328 | * |
||
| 329 | * @param \DateTime|null $timestamp |
||
| 330 | * |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | public function setTimestamp(?\DateTime $timestamp) |
||
| 334 | { |
||
| 335 | $this->timestamp = $timestamp; |
||
| 336 | |||
| 337 | return $this; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return string|null |
||
| 342 | */ |
||
| 343 | public function getTitle(): ?string |
||
| 344 | { |
||
| 345 | return $this->title; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Set the title to use for the attachment. |
||
| 350 | * |
||
| 351 | * @param string|null $title |
||
| 352 | * |
||
| 353 | * @return $this |
||
| 354 | */ |
||
| 355 | public function setTitle(?string $title) |
||
| 356 | { |
||
| 357 | $this->title = $title; |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return string|null |
||
| 364 | */ |
||
| 365 | public function getTitleLink(): ?string |
||
| 366 | { |
||
| 367 | return $this->titleLink; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Set the title link to use for the attachment. |
||
| 372 | * |
||
| 373 | * @param string|null $titleLink |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function setTitleLink(?string $titleLink) |
||
| 378 | { |
||
| 379 | $this->titleLink = $titleLink; |
||
| 380 | |||
| 381 | return $this; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return string|null |
||
| 386 | */ |
||
| 387 | public function getAuthorName(): ?string |
||
| 388 | { |
||
| 389 | return $this->authorName; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set the author name to use for the attachment. |
||
| 394 | * |
||
| 395 | * @param string|null $authorName |
||
| 396 | * |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | public function setAuthorName(?string $authorName) |
||
| 400 | { |
||
| 401 | $this->authorName = $authorName; |
||
| 402 | |||
| 403 | return $this; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return string|null |
||
| 408 | */ |
||
| 409 | public function getAuthorLink(): ?string |
||
| 410 | { |
||
| 411 | return $this->authorLink; |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Set the author link to use for the attachment. |
||
| 416 | * |
||
| 417 | * @param string|null $authorLink |
||
| 418 | * |
||
| 419 | * @return $this |
||
| 420 | */ |
||
| 421 | public function setAuthorLink(?string $authorLink) |
||
| 422 | { |
||
| 423 | $this->authorLink = $authorLink; |
||
| 424 | |||
| 425 | return $this; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return string|null |
||
| 430 | */ |
||
| 431 | public function getAuthorIcon(): ?string |
||
| 432 | { |
||
| 433 | return $this->authorIcon; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Set the author icon to use for the attachment. |
||
| 438 | * |
||
| 439 | * @param string|null $authorIcon |
||
| 440 | * |
||
| 441 | * @return $this |
||
| 442 | */ |
||
| 443 | public function setAuthorIcon(?string $authorIcon) |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | public function getFields(): array |
||
| 454 | { |
||
| 455 | return $this->fields; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Set the fields for the attachment. |
||
| 460 | * |
||
| 461 | * @param array $fields |
||
| 462 | * |
||
| 463 | * @return $this |
||
| 464 | */ |
||
| 465 | public function setFields(array $fields) |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Add a field to the attachment. |
||
| 478 | * |
||
| 479 | * @param AttachmentField $field |
||
| 480 | * |
||
| 481 | * @return $this |
||
| 482 | */ |
||
| 483 | public function addField(AttachmentField $field) |
||
| 484 | { |
||
| 485 | $this->fields[] = $field; |
||
| 486 | |||
| 487 | return $this; |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Clear the fields for the attachment. |
||
| 492 | * |
||
| 493 | * @return $this |
||
| 494 | */ |
||
| 495 | public function clearFields() |
||
| 496 | { |
||
| 497 | $this->fields = []; |
||
| 498 | |||
| 499 | return $this; |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @return array |
||
| 504 | */ |
||
| 505 | public function getMarkdownFields(): array |
||
| 506 | { |
||
| 507 | return $this->markdownFields; |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Set the fields Slack should interpret in its |
||
| 512 | * Markdown-like language. |
||
| 513 | * |
||
| 514 | * @param array $fields |
||
| 515 | * |
||
| 516 | * @return $this |
||
| 517 | */ |
||
| 518 | public function setMarkdownFields(array $fields) |
||
| 519 | { |
||
| 520 | $this->markdownFields = $fields; |
||
| 521 | |||
| 522 | return $this; |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | public function getActions(): array |
||
| 529 | { |
||
| 530 | return $this->actions; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Set the collection of actions (buttons) to include in the attachment. |
||
| 535 | * |
||
| 536 | * @param array $actions |
||
| 537 | * |
||
| 538 | * @return Attachment |
||
| 539 | */ |
||
| 540 | public function setActions($actions) |
||
| 541 | { |
||
| 542 | $this->clearActions(); |
||
| 543 | |||
| 544 | foreach ($actions as $action) { |
||
| 545 | $this->addAction($action); |
||
| 546 | } |
||
| 547 | |||
| 548 | return $this; |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Add an action to the attachment. |
||
| 553 | * |
||
| 554 | * @param AttachmentAction $action |
||
| 555 | * |
||
| 556 | * @return $this |
||
| 557 | */ |
||
| 558 | public function addAction(AttachmentAction $action) |
||
| 559 | { |
||
| 560 | $this->actions[] = $action; |
||
| 561 | |||
| 562 | return $this; |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Clear the actions for the attachment. |
||
| 567 | * |
||
| 568 | * @return $this |
||
| 569 | */ |
||
| 570 | public function clearActions() |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Convert this attachment to its array representation. |
||
| 579 | * |
||
| 580 | * @return array |
||
| 581 | */ |
||
| 582 | public function toArray() |
||
| 583 | { |
||
| 584 | $data = [ |
||
| 585 | 'fallback' => $this->fallback, |
||
| 586 | 'text' => $this->text, |
||
| 587 | 'pretext' => $this->pretext, |
||
| 588 | 'color' => $this->color, |
||
| 589 | 'footer' => $this->footer, |
||
| 590 | 'footer_icon' => $this->footerIcon, |
||
| 591 | 'ts' => $this->timestamp ? $this->timestamp->getTimestamp() : null, |
||
| 592 | 'mrkdwn_in' => $this->markdownFields, |
||
| 593 | 'image_url' => $this->imageUrl, |
||
| 594 | 'thumb_url' => $this->thumbUrl, |
||
| 595 | 'title' => $this->title, |
||
| 596 | 'title_link' => $this->titleLink, |
||
| 597 | 'author_name' => $this->authorName, |
||
| 598 | 'author_link' => $this->authorLink, |
||
| 599 | 'author_icon' => $this->authorIcon, |
||
| 600 | ]; |
||
| 601 | |||
| 602 | $data['fields'] = $this->getFieldsAsArrays(); |
||
| 603 | $data['actions'] = $this->getActionsAsArrays(); |
||
| 604 | |||
| 605 | return $data; |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Iterates over all fields in this attachment and returns |
||
| 610 | * them in their array form. |
||
| 611 | * |
||
| 612 | * @return array |
||
| 613 | */ |
||
| 614 | private function getFieldsAsArrays() |
||
| 615 | { |
||
| 616 | $fields = []; |
||
| 617 | |||
| 618 | foreach ($this->fields as $field) { |
||
| 619 | $fields[] = $field->toArray(); |
||
| 620 | } |
||
| 621 | |||
| 622 | return $fields; |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Iterates over all actions in this attachment and returns |
||
| 627 | * them in their array form. |
||
| 628 | * |
||
| 629 | * @return array |
||
| 630 | */ |
||
| 631 | private function getActionsAsArrays() |
||
| 640 | } |
||
| 641 | } |
||
| 642 |