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 = false; |
||
| 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 | 44 | public function __construct(string $username, string $key) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * This method allows you to override the default CurlRequest object. Added for testing purposes. |
||
| 105 | * |
||
| 106 | * @param \Swis\PdfcrowdClient\Http\FactoryInterface $requestFactory |
||
| 107 | */ |
||
| 108 | 40 | public function setRequestFactory(FactoryInterface $requestFactory) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Each httpPost-call uses a clean request object. |
||
| 115 | * |
||
| 116 | * @return \Swis\PdfcrowdClient\Http\RequestInterface |
||
| 117 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 118 | */ |
||
| 119 | 38 | protected function getNewRequestObject(): RequestInterface |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Converts an in-memory html document. |
||
| 132 | * |
||
| 133 | * @param string $src a string containing a html document |
||
| 134 | * @param null $outstream output stream, if null then the return value is a string containing the PDF |
||
| 135 | * |
||
| 136 | * @return mixed |
||
| 137 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 138 | */ |
||
| 139 | 38 | public function convertHtml($src, $outstream = null) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Converts an in-memory html document. |
||
| 159 | * |
||
| 160 | * @param string $src a path to an html file |
||
| 161 | * @param null $outstream output stream, if null then the return value is a string containing the PDF |
||
| 162 | * |
||
| 163 | * @return mixed |
||
| 164 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 165 | */ |
||
| 166 | public function convertFile(string $src, $outstream = null) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Converts a web page. |
||
| 213 | * |
||
| 214 | * @param string $src a web page URL |
||
| 215 | * @param null $outstream output stream, if null then the return value is a string containing the PDF |
||
| 216 | * |
||
| 217 | * @return mixed |
||
| 218 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 219 | */ |
||
| 220 | public function convertURI(string $src, $outstream = null) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns the number of available conversion tokens. |
||
| 239 | * |
||
| 240 | * @return int |
||
| 241 | */ |
||
| 242 | public function numTokens(): int |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the number of tokens used in the last conversion. |
||
| 260 | * This is only possible if you enable tracking tokens using trackTokens(true). |
||
| 261 | * |
||
| 262 | * @see trackTokens() |
||
| 263 | * |
||
| 264 | * @return int |
||
| 265 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 266 | */ |
||
| 267 | public function getUsedTokens(): int |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Track how many tokens are available before each request. |
||
| 288 | * After a request you can ask the number of used tokens with getUsedTokens. |
||
| 289 | * |
||
| 290 | * @see getUsedTokens() |
||
| 291 | * |
||
| 292 | * @param bool $trackTokens |
||
| 293 | */ |
||
| 294 | public function trackTokens(bool $trackTokens = true) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Turn SSL on or off. |
||
| 301 | * |
||
| 302 | * @param bool $use_ssl |
||
| 303 | */ |
||
| 304 | public function useSSL(bool $use_ssl) |
||
| 316 | |||
| 317 | public function setPageWidth($value) |
||
| 321 | |||
| 322 | public function setPageHeight($value) |
||
| 326 | |||
| 327 | public function setHorizontalMargin($value) |
||
| 331 | |||
| 332 | public function setVerticalMargin($value) |
||
| 336 | |||
| 337 | public function setBottomMargin($value) |
||
| 341 | |||
| 342 | public function setPageMargins($top, $right, $bottom, $left) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * If value is set to True then the PDF is encrypted. This prevents search engines from indexing the document. |
||
| 352 | * The default is False. |
||
| 353 | * |
||
| 354 | * @param bool $val |
||
| 355 | */ |
||
| 356 | public function setEncrypted(bool $val = true) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Protects the PDF with a user password. When a PDF has a user password, it must be supplied in order to view the |
||
| 363 | * document and to perform operations allowed by the access permissions. At most 32 characters. |
||
| 364 | * |
||
| 365 | * @param string $pwd |
||
| 366 | */ |
||
| 367 | public function setUserPassword(string $pwd) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Protects the PDF with an owner password. Supplying an owner password grants unlimited access to the PDF |
||
| 374 | * including changing the passwords and access permissions. At most 32 characters. |
||
| 375 | * |
||
| 376 | * @param string $pwd |
||
| 377 | */ |
||
| 378 | public function setOwnerPassword(string $pwd) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Set value to True disables printing the generated PDF. The default is False. |
||
| 385 | * |
||
| 386 | * @param bool $val |
||
| 387 | */ |
||
| 388 | public function setNoPrint(bool $val = true) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set value to True to disable modifying the PDF. The default is False. |
||
| 395 | * |
||
| 396 | * @param bool $val |
||
| 397 | */ |
||
| 398 | public function setNoModify(bool $val = true) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Set value to True to disable extracting text and graphics from the PDF. The default is False. |
||
| 405 | * |
||
| 406 | * @param bool $val |
||
| 407 | */ |
||
| 408 | public function setNoCopy(bool $val = true) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Specifies the initial page layout when the PDF is opened in a viewer. |
||
| 415 | * |
||
| 416 | * Possible values: |
||
| 417 | * \Swis\PdfcrowdClient\Pdfcrowd::SINGLE_PAGE |
||
| 418 | * \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS |
||
| 419 | * \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS_FACING |
||
| 420 | * |
||
| 421 | * @param int $value |
||
| 422 | */ |
||
| 423 | public function setPageLayout(int $value) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Specifies the appearance of the PDF when opened. |
||
| 431 | * |
||
| 432 | * Possible values: |
||
| 433 | * \Swis\PdfcrowdClient\Pdfcrowd::NONE_VISIBLE |
||
| 434 | * \Swis\PdfcrowdClient\Pdfcrowd::THUMBNAILS_VISIBLE |
||
| 435 | * \Swis\PdfcrowdClient\Pdfcrowd::FULLSCREEN |
||
| 436 | * |
||
| 437 | * @param int $value |
||
| 438 | */ |
||
| 439 | public function setPageMode(int $value) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param string $value |
||
| 447 | */ |
||
| 448 | public function setFooterText(string $value) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Set value to False to disable printing images to the PDF. The default is True. |
||
| 455 | * |
||
| 456 | * @param bool $value |
||
| 457 | */ |
||
| 458 | public function enableImages(bool $value = true) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Set value to False to disable printing backgrounds to the PDF. The default is True. |
||
| 465 | * |
||
| 466 | * @param bool $value |
||
| 467 | */ |
||
| 468 | public function enableBackgrounds(bool $value = true) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Set HTML zoom in percents. It determines the precision used for rendering of the HTML content. Despite its name, |
||
| 475 | * it does not zoom the HTML content. Higher values can improve glyph positioning and can lead to overall better |
||
| 476 | * visual appearance of generated PDF .The default value is 200. |
||
| 477 | * |
||
| 478 | * @see setPdfScalingFactor |
||
| 479 | * |
||
| 480 | * @param int $value |
||
| 481 | */ |
||
| 482 | public function setHtmlZoom(int $value) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Set value to False to disable JavaScript in web pages. The default is True. |
||
| 489 | * |
||
| 490 | * @param bool $value |
||
| 491 | */ |
||
| 492 | public function enableJavaScript(bool $value = true) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Set value to False to disable hyperlinks in the PDF. The default is True. |
||
| 499 | * |
||
| 500 | * @param bool $value |
||
| 501 | */ |
||
| 502 | public function enableHyperlinks(bool $value = true) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Value is the text encoding used when none is specified in a web page. The default is utf-8. |
||
| 509 | * |
||
| 510 | * @param string $value |
||
| 511 | */ |
||
| 512 | public function setDefaultTextEncoding(string $value) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * If value is True then the print CSS media type is used (if available). |
||
| 519 | * |
||
| 520 | * @param bool $value |
||
| 521 | */ |
||
| 522 | public function usePrintMedia(bool $value = true) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Prints at most npages pages. |
||
| 529 | * |
||
| 530 | * @param int $value |
||
| 531 | */ |
||
| 532 | public function setMaxPages(int $value) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param bool $value |
||
| 539 | */ |
||
| 540 | public function enablePdfcrowdLogo(bool $value = true) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * value specifies the appearance of the PDF when opened. |
||
| 547 | * |
||
| 548 | * Possible values: |
||
| 549 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_WIDTH |
||
| 550 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_HEIGHT |
||
| 551 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_PAGE |
||
| 552 | * |
||
| 553 | * @param int $value |
||
| 554 | */ |
||
| 555 | public function setInitialPdfZoomType(int $value) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * value specifies the initial page zoom of the PDF when opened. |
||
| 563 | * |
||
| 564 | * @param $value |
||
| 565 | */ |
||
| 566 | public function setInitialPdfExactZoom($value) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * The scaling factor used to convert between HTML and PDF. The default value is 1.0. |
||
| 574 | * |
||
| 575 | * @param float $value |
||
| 576 | */ |
||
| 577 | public function setPdfScalingFactor(float $value) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Sets the author field in the created PDF. |
||
| 584 | * |
||
| 585 | * @param string $value |
||
| 586 | */ |
||
| 587 | public function setAuthor(string $value) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * If value is True then the conversion will fail when the source URI returns 4xx or 5xx HTTP status code. The |
||
| 594 | * default is False. |
||
| 595 | * |
||
| 596 | * @param bool $value |
||
| 597 | */ |
||
| 598 | public function setFailOnNon200(bool $value) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Places the specified html code inside the page footer. The following variables are expanded: |
||
| 605 | * %u - URL to convert. |
||
| 606 | * %p - The current page number. |
||
| 607 | * %n - Total number of pages. |
||
| 608 | * |
||
| 609 | * @param string $value |
||
| 610 | */ |
||
| 611 | public function setFooterHtml(string $value) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Loads HTML code from the specified url and places it inside the page footer. See setFooterHtml for the list of |
||
| 618 | * variables that are expanded. |
||
| 619 | * |
||
| 620 | * @see setFooterHtml |
||
| 621 | * |
||
| 622 | * @param string $value |
||
| 623 | */ |
||
| 624 | public function setFooterUrl(string $value) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Places the specified html code inside the page header. See setFooterHtml for the list of variables that are |
||
| 631 | * expanded. |
||
| 632 | * |
||
| 633 | * @see setFooterHtml |
||
| 634 | * |
||
| 635 | * @param string $value |
||
| 636 | */ |
||
| 637 | public function setHeaderHtml(string $value) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Loads HTML code from the specified url and places it inside the page header. See setFooterHtml for the list of |
||
| 644 | * variables that are expanded. |
||
| 645 | * |
||
| 646 | * @see setFooterHtml |
||
| 647 | * |
||
| 648 | * @param string $value |
||
| 649 | */ |
||
| 650 | public function setHeaderUrl(string $value) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * The page background color in RRGGBB hexadecimal format. |
||
| 657 | * |
||
| 658 | * @param string $value |
||
| 659 | */ |
||
| 660 | public function setPageBackgroundColor(string $value) |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Does not print the body background. Requires the following CSS rule to be declared: |
||
| 667 | * body {background-color:rgba(255,255,255,0.0);} |
||
| 668 | * |
||
| 669 | * @param bool $value |
||
| 670 | */ |
||
| 671 | public function setTransparentBackground(bool $value = true) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * An offset between physical and logical page numbers. The default value is 0. |
||
| 678 | * |
||
| 679 | * @example if set to "1" then the page numbering will start with 1 on the second page. |
||
| 680 | * |
||
| 681 | * @param int $value |
||
| 682 | */ |
||
| 683 | public function setPageNumberingOffset(int $value) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Value is a comma seperated list of physical page numbers on which the header a footer are not printed. Negative |
||
| 690 | * numbers count backwards from the last page: -1 is the last page, -2 is the last but one page, and so on. |
||
| 691 | * |
||
| 692 | * @example "1,-1" will not print the header and footer on the first and the last page. |
||
| 693 | * |
||
| 694 | * @param string $value |
||
| 695 | */ |
||
| 696 | public function setHeaderFooterPageExcludeList(string $value) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * url is a public absolute URL of the watermark image (must start either with http:// or https://). The supported |
||
| 703 | * formats are PNG and JPEG. offset_x and offset_y is the watermark offset in units. The default offset is (0,0). |
||
| 704 | * |
||
| 705 | * @param string $url |
||
| 706 | * @param int $offset_x |
||
| 707 | * @param int $offset_y |
||
| 708 | */ |
||
| 709 | public function setWatermark(string $url, $offset_x = 0, $offset_y = 0) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Rotates the watermark by angle degrees. |
||
| 718 | * |
||
| 719 | * @param int $angle |
||
| 720 | */ |
||
| 721 | public function setWatermarkRotationsetWatermarkRotation(int $angle) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * When value is set to True then the watermark is be placed in the background. By default, the watermark is |
||
| 728 | * placed in the foreground. |
||
| 729 | * |
||
| 730 | * @param bool $val |
||
| 731 | */ |
||
| 732 | public function setWatermarkInBackground(bool $val = true) |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @param string $proxyname |
||
| 739 | * @param int $port |
||
| 740 | * @param string $username |
||
| 741 | * @param string $password |
||
| 742 | */ |
||
| 743 | public function setProxy(string $proxyname, int $port, string $username = '', string $password = '') |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @param string $user_agent |
||
| 753 | */ |
||
| 754 | public function setUserAgent(string $user_agent) |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @param int $timeout |
||
| 761 | */ |
||
| 762 | public function setTimeout(int $timeout) |
||
| 768 | |||
| 769 | /** |
||
| 770 | * @param string $url |
||
| 771 | * @param $postfields |
||
| 772 | * @param $outstream |
||
| 773 | * |
||
| 774 | * @return mixed |
||
| 775 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 776 | */ |
||
| 777 | private function httpPost(string $url, array $postfields, $outstream) |
||
| 799 | |||
| 800 | protected function buildRequest(string $url, array $postfields, $outstream) |
||
| 836 | |||
| 837 | /** |
||
| 838 | * @see http://php.net/manual/en/function.curl-setopt.php and look for CURLOPT_WRITEFUNCTION |
||
| 839 | * |
||
| 840 | * @param $curl |
||
| 841 | * @param $data |
||
| 842 | * |
||
| 843 | * @return bool|int |
||
| 844 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 845 | */ |
||
| 846 | private function receiveToStream($curl, $data) |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Set or unset a parameter that will be sent with the request to the pdfcrowd API. |
||
| 875 | * |
||
| 876 | * @param mixed $val |
||
| 877 | * @param string $field |
||
| 878 | */ |
||
| 879 | private function setOrUnset($val, string $field) |
||
| 887 | } |
||
| 888 |