Complex classes like Pdfcrowd 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Pdfcrowd, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Pdfcrowd |
||
| 14 | { |
||
| 15 | /** @var array */ |
||
| 16 | private $fields; |
||
| 17 | |||
| 18 | /** @var string */ |
||
| 19 | private $scheme; |
||
| 20 | |||
| 21 | /** @var int */ |
||
| 22 | private $port; |
||
| 23 | |||
| 24 | /** @var string */ |
||
| 25 | private $api_prefix; |
||
| 26 | |||
| 27 | /** @var int */ |
||
| 28 | private $curlopt_timeout; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | private $hostname; |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | private $user_agent; |
||
| 35 | |||
| 36 | /** @var int */ |
||
| 37 | private $num_tokens_before; |
||
| 38 | |||
| 39 | /** @var int */ |
||
| 40 | private $http_code; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | private $error; |
||
| 44 | |||
| 45 | private $outstream; |
||
| 46 | |||
| 47 | /** @var FactoryInterface */ |
||
| 48 | protected $requestFactory; |
||
| 49 | |||
| 50 | /* @var RequestInterface */ |
||
| 51 | private $request; |
||
| 52 | |||
| 53 | /** @var bool */ |
||
| 54 | private $track_tokens = false; |
||
| 55 | |||
| 56 | public static $client_version = '2.7'; |
||
| 57 | public static $http_port = 80; |
||
| 58 | public static $https_port = 443; |
||
| 59 | public static $api_host = 'pdfcrowd.com'; |
||
| 60 | |||
| 61 | private $proxy_name = null; |
||
| 62 | private $proxy_port = null; |
||
| 63 | private $proxy_username = ''; |
||
| 64 | private $proxy_password = ''; |
||
| 65 | |||
| 66 | const SINGLE_PAGE = 1; |
||
| 67 | const CONTINUOUS = 2; |
||
| 68 | const CONTINUOUS_FACING = 3; |
||
| 69 | |||
| 70 | const NONE_VISIBLE = 1; |
||
| 71 | const THUMBNAILS_VISIBLE = 2; |
||
| 72 | const FULLSCREEN = 3; |
||
| 73 | |||
| 74 | const FIT_WIDTH = 1; |
||
| 75 | const FIT_HEIGHT = 2; |
||
| 76 | const FIT_PAGE = 3; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Pdfcrowd constructor. |
||
| 80 | * |
||
| 81 | * @param string $username |
||
| 82 | * @param string $key |
||
| 83 | */ |
||
| 84 | public function __construct(string $username, string $key) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * This method allows you to override the default CurlRequest object. Added for testing purposes. |
||
| 104 | * |
||
| 105 | * @param \Swis\PdfcrowdClient\Http\FactoryInterface $requestFactory |
||
| 106 | */ |
||
| 107 | public function setRequestFactory(FactoryInterface $requestFactory) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Each httpPost-call uses a clean request object. |
||
| 114 | * |
||
| 115 | * @return \Swis\PdfcrowdClient\Http\RequestInterface |
||
| 116 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 117 | */ |
||
| 118 | protected function getNewRequestObject(): RequestInterface |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Converts an in-memory html document. |
||
| 131 | * |
||
| 132 | * @param string $src a string containing a html document |
||
| 133 | * @param null $outstream output stream, if null then the return value is a string containing the PDF |
||
| 134 | * |
||
| 135 | * @return mixed |
||
| 136 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 137 | */ |
||
| 138 | public function convertHtml($src, $outstream = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Converts an in-memory html document. |
||
| 157 | * |
||
| 158 | * @param string $src a path to an html file |
||
| 159 | * @param null $outstream output stream, if null then the return value is a string containing the PDF |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 163 | */ |
||
| 164 | public function convertFile(string $src, $outstream = null) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Converts a web page. |
||
| 211 | * |
||
| 212 | * @param string $src a web page URL |
||
| 213 | * @param null $outstream output stream, if null then the return value is a string containing the PDF |
||
| 214 | * |
||
| 215 | * @return mixed |
||
| 216 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 217 | */ |
||
| 218 | public function convertURI(string $src, $outstream = null) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns the number of available conversion tokens. |
||
| 238 | * |
||
| 239 | * @return int |
||
| 240 | */ |
||
| 241 | public function numTokens(): int |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the number of tokens used in the last conversion. |
||
| 255 | * This is only possible if you enable tracking tokens using trackTokens(true). |
||
| 256 | * |
||
| 257 | * @see trackTokens() |
||
| 258 | * |
||
| 259 | * @return int |
||
| 260 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 261 | */ |
||
| 262 | public function getUsedTokens(): int |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Track how many tokens are available before each request. |
||
| 277 | * After a request you can ask the number of used tokens with getUsedTokens. |
||
| 278 | * |
||
| 279 | * @see getUsedTokens() |
||
| 280 | * |
||
| 281 | * @param bool $trackTokens |
||
| 282 | */ |
||
| 283 | public function trackTokens(bool $trackTokens = true) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Turn SSL on or off. |
||
| 290 | * |
||
| 291 | * @param bool $use_ssl |
||
| 292 | */ |
||
| 293 | public function useSSL(bool $use_ssl) |
||
| 305 | |||
| 306 | public function setPageWidth($value) |
||
| 310 | |||
| 311 | public function setPageHeight($value) |
||
| 315 | |||
| 316 | public function setHorizontalMargin($value) |
||
| 320 | |||
| 321 | public function setVerticalMargin($value) |
||
| 325 | |||
| 326 | public function setBottomMargin($value) |
||
| 330 | |||
| 331 | public function setPageMargins($top, $right, $bottom, $left) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * If value is set to True then the PDF is encrypted. This prevents search engines from indexing the document. |
||
| 341 | * The default is False. |
||
| 342 | * |
||
| 343 | * @param bool $val |
||
| 344 | */ |
||
| 345 | public function setEncrypted(bool $val = true) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Protects the PDF with a user password. When a PDF has a user password, it must be supplied in order to view the |
||
| 352 | * document and to perform operations allowed by the access permissions. At most 32 characters. |
||
| 353 | * |
||
| 354 | * @param string $pwd |
||
| 355 | */ |
||
| 356 | public function setUserPassword(string $pwd) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Protects the PDF with an owner password. Supplying an owner password grants unlimited access to the PDF |
||
| 363 | * including changing the passwords and access permissions. At most 32 characters. |
||
| 364 | * |
||
| 365 | * @param string $pwd |
||
| 366 | */ |
||
| 367 | public function setOwnerPassword(string $pwd) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set value to True disables printing the generated PDF. The default is False. |
||
| 374 | * |
||
| 375 | * @param bool $val |
||
| 376 | */ |
||
| 377 | public function setNoPrint(bool $val = true) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set value to True to disable modifying the PDF. The default is False. |
||
| 384 | * |
||
| 385 | * @param bool $val |
||
| 386 | */ |
||
| 387 | public function setNoModify(bool $val = true) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set value to True to disable extracting text and graphics from the PDF. The default is False. |
||
| 394 | * |
||
| 395 | * @param bool $val |
||
| 396 | */ |
||
| 397 | public function setNoCopy(bool $val = true) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Specifies the initial page layout when the PDF is opened in a viewer. |
||
| 404 | * |
||
| 405 | * Possible values: |
||
| 406 | * \Swis\PdfcrowdClient\Pdfcrowd::SINGLE_PAGE |
||
| 407 | * \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS |
||
| 408 | * \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS_FACING |
||
| 409 | * |
||
| 410 | * @param int $value |
||
| 411 | */ |
||
| 412 | public function setPageLayout(int $value) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Specifies the appearance of the PDF when opened. |
||
| 420 | * |
||
| 421 | * Possible values: |
||
| 422 | * \Swis\PdfcrowdClient\Pdfcrowd::NONE_VISIBLE |
||
| 423 | * \Swis\PdfcrowdClient\Pdfcrowd::THUMBNAILS_VISIBLE |
||
| 424 | * \Swis\PdfcrowdClient\Pdfcrowd::FULLSCREEN |
||
| 425 | * |
||
| 426 | * @param int $value |
||
| 427 | */ |
||
| 428 | public function setPageMode(int $value) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $value |
||
| 436 | */ |
||
| 437 | public function setFooterText(string $value) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Set value to False to disable printing images to the PDF. The default is True. |
||
| 444 | * |
||
| 445 | * @param bool $value |
||
| 446 | */ |
||
| 447 | public function enableImages(bool $value = true) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Set value to False to disable printing backgrounds to the PDF. The default is True. |
||
| 454 | * |
||
| 455 | * @param bool $value |
||
| 456 | */ |
||
| 457 | public function enableBackgrounds(bool $value = true) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Set HTML zoom in percents. It determines the precision used for rendering of the HTML content. Despite its name, |
||
| 464 | * it does not zoom the HTML content. Higher values can improve glyph positioning and can lead to overall better |
||
| 465 | * visual appearance of generated PDF .The default value is 200. |
||
| 466 | * |
||
| 467 | * @see setPdfScalingFactor |
||
| 468 | * |
||
| 469 | * @param int $value |
||
| 470 | */ |
||
| 471 | public function setHtmlZoom(int $value) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Set value to False to disable JavaScript in web pages. The default is True. |
||
| 478 | * |
||
| 479 | * @param bool $value |
||
| 480 | */ |
||
| 481 | public function enableJavaScript(bool $value = true) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Set value to False to disable hyperlinks in the PDF. The default is True. |
||
| 488 | * |
||
| 489 | * @param bool $value |
||
| 490 | */ |
||
| 491 | public function enableHyperlinks(bool $value = true) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Value is the text encoding used when none is specified in a web page. The default is utf-8. |
||
| 498 | * |
||
| 499 | * @param string $value |
||
| 500 | */ |
||
| 501 | public function setDefaultTextEncoding(string $value) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * If value is True then the print CSS media type is used (if available). |
||
| 508 | * |
||
| 509 | * @param bool $value |
||
| 510 | */ |
||
| 511 | public function usePrintMedia(bool $value = true) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Prints at most npages pages. |
||
| 518 | * |
||
| 519 | * @param int $value |
||
| 520 | */ |
||
| 521 | public function setMaxPages(int $value) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param bool $value |
||
| 528 | */ |
||
| 529 | public function enablePdfcrowdLogo(bool $value = true) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * value specifies the appearance of the PDF when opened. |
||
| 536 | * |
||
| 537 | * Possible values: |
||
| 538 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_WIDTH |
||
| 539 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_HEIGHT |
||
| 540 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_PAGE |
||
| 541 | * |
||
| 542 | * @param int $value |
||
| 543 | */ |
||
| 544 | public function setInitialPdfZoomType(int $value) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * value specifies the initial page zoom of the PDF when opened. |
||
| 552 | * |
||
| 553 | * @param $value |
||
| 554 | */ |
||
| 555 | public function setInitialPdfExactZoom($value) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * The scaling factor used to convert between HTML and PDF. The default value is 1.0. |
||
| 563 | * |
||
| 564 | * @param float $value |
||
| 565 | */ |
||
| 566 | public function setPdfScalingFactor(float $value) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Sets the author field in the created PDF. |
||
| 573 | * |
||
| 574 | * @param string $value |
||
| 575 | */ |
||
| 576 | public function setAuthor(string $value) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * If value is True then the conversion will fail when the source URI returns 4xx or 5xx HTTP status code. The |
||
| 583 | * default is False. |
||
| 584 | * |
||
| 585 | * @param bool $value |
||
| 586 | */ |
||
| 587 | public function setFailOnNon200(bool $value) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Places the specified html code inside the page footer. The following variables are expanded: |
||
| 594 | * %u - URL to convert. |
||
| 595 | * %p - The current page number. |
||
| 596 | * %n - Total number of pages. |
||
| 597 | * |
||
| 598 | * @param string $value |
||
| 599 | */ |
||
| 600 | public function setFooterHtml(string $value) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Loads HTML code from the specified url and places it inside the page footer. See setFooterHtml for the list of |
||
| 607 | * variables that are expanded. |
||
| 608 | * |
||
| 609 | * @see setFooterHtml |
||
| 610 | * |
||
| 611 | * @param string $value |
||
| 612 | */ |
||
| 613 | public function setFooterUrl(string $value) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Places the specified html code inside the page header. See setFooterHtml for the list of variables that are |
||
| 620 | * expanded. |
||
| 621 | * |
||
| 622 | * @see setFooterHtml |
||
| 623 | * |
||
| 624 | * @param string $value |
||
| 625 | */ |
||
| 626 | public function setHeaderHtml(string $value) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Loads HTML code from the specified url and places it inside the page header. See setFooterHtml for the list of |
||
| 633 | * variables that are expanded. |
||
| 634 | * |
||
| 635 | * @see setFooterHtml |
||
| 636 | * |
||
| 637 | * @param string $value |
||
| 638 | */ |
||
| 639 | public function setHeaderUrl(string $value) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * The page background color in RRGGBB hexadecimal format. |
||
| 646 | * |
||
| 647 | * @param string $value |
||
| 648 | */ |
||
| 649 | public function setPageBackgroundColor(string $value) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Does not print the body background. Requires the following CSS rule to be declared: |
||
| 656 | * body {background-color:rgba(255,255,255,0.0);} |
||
| 657 | * |
||
| 658 | * @param bool $value |
||
| 659 | */ |
||
| 660 | public function setTransparentBackground(bool $value = true) |
||
| 664 | |||
| 665 | /** |
||
| 666 | * An offset between physical and logical page numbers. The default value is 0. |
||
| 667 | * |
||
| 668 | * @example if set to "1" then the page numbering will start with 1 on the second page. |
||
| 669 | * |
||
| 670 | * @param int $value |
||
| 671 | */ |
||
| 672 | public function setPageNumberingOffset(int $value) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Value is a comma seperated list of physical page numbers on which the header a footer are not printed. Negative |
||
| 679 | * numbers count backwards from the last page: -1 is the last page, -2 is the last but one page, and so on. |
||
| 680 | * |
||
| 681 | * @example "1,-1" will not print the header and footer on the first and the last page. |
||
| 682 | * |
||
| 683 | * @param string $value |
||
| 684 | */ |
||
| 685 | public function setHeaderFooterPageExcludeList(string $value) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * url is a public absolute URL of the watermark image (must start either with http:// or https://). The supported |
||
| 692 | * formats are PNG and JPEG. offset_x and offset_y is the watermark offset in units. The default offset is (0,0). |
||
| 693 | * |
||
| 694 | * @param string $url |
||
| 695 | * @param int $offset_x |
||
| 696 | * @param int $offset_y |
||
| 697 | */ |
||
| 698 | public function setWatermark(string $url, $offset_x = 0, $offset_y = 0) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Rotates the watermark by angle degrees. |
||
| 707 | * |
||
| 708 | * @param int $angle |
||
| 709 | */ |
||
| 710 | public function setWatermarkRotationsetWatermarkRotation(int $angle) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * When value is set to True then the watermark is be placed in the background. By default, the watermark is |
||
| 717 | * placed in the foreground. |
||
| 718 | * |
||
| 719 | * @param bool $val |
||
| 720 | */ |
||
| 721 | public function setWatermarkInBackground(bool $val = true) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @param string $proxyname |
||
| 728 | * @param int $port |
||
| 729 | * @param string $username |
||
| 730 | * @param string $password |
||
| 731 | */ |
||
| 732 | public function setProxy(string $proxyname, int $port, string $username = '', string $password = '') |
||
| 739 | |||
| 740 | /** |
||
| 741 | * @param string $user_agent |
||
| 742 | */ |
||
| 743 | public function setUserAgent(string $user_agent) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * @param int $timeout |
||
| 750 | */ |
||
| 751 | public function setTimeout(int $timeout) |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @param string $url |
||
| 760 | * @param $postfields |
||
| 761 | * @param $outstream |
||
| 762 | * |
||
| 763 | * @return mixed |
||
| 764 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 765 | */ |
||
| 766 | private function httpPost(string $url, $postfields, $outstream) |
||
| 822 | |||
| 823 | /** |
||
| 824 | * @see http://php.net/manual/en/function.curl-setopt.php and look for CURLOPT_WRITEFUNCTION |
||
| 825 | * |
||
| 826 | * @param $curl |
||
| 827 | * @param $data |
||
| 828 | * |
||
| 829 | * @return bool|int |
||
| 830 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 831 | */ |
||
| 832 | private function receiveToStream($curl, $data) |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Set or unset a parameter that will be sent with the request to the pdfcrowd API. |
||
| 861 | * |
||
| 862 | * @param mixed $val |
||
| 863 | * @param string $field |
||
| 864 | */ |
||
| 865 | private function setOrUnset($val, string $field) |
||
| 873 | } |
||
| 874 |