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 |
||
| 12 | class Pdfcrowd |
||
| 13 | { |
||
| 14 | /** @var array */ |
||
| 15 | private $requestBody; |
||
| 16 | |||
| 17 | /** @var string */ |
||
| 18 | private $scheme; |
||
| 19 | |||
| 20 | /** @var int */ |
||
| 21 | private $port; |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | private $api_prefix; |
||
| 25 | |||
| 26 | /** @var int */ |
||
| 27 | private $curlopt_timeout; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | private $hostname; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | private $user_agent; |
||
| 34 | |||
| 35 | /** @var int */ |
||
| 36 | private $num_tokens_before = false; |
||
| 37 | |||
| 38 | /** @var int */ |
||
| 39 | private $http_code; |
||
| 40 | |||
| 41 | /** @var FactoryInterface */ |
||
| 42 | protected $requestFactory; |
||
| 43 | |||
| 44 | /* @var RequestInterface */ |
||
| 45 | private $request; |
||
| 46 | |||
| 47 | /** @var bool */ |
||
| 48 | private $track_tokens = false; |
||
| 49 | |||
| 50 | protected $output_destination; |
||
| 51 | |||
| 52 | public static $client_version = '2.7'; |
||
| 53 | public static $http_port = 80; |
||
| 54 | public static $https_port = 443; |
||
| 55 | public static $api_host = 'pdfcrowd.com'; |
||
| 56 | |||
| 57 | private $proxy_name; |
||
| 58 | private $proxy_port; |
||
| 59 | private $proxy_username = ''; |
||
| 60 | private $proxy_password = ''; |
||
| 61 | |||
| 62 | const SINGLE_PAGE = 1; |
||
| 63 | const CONTINUOUS = 2; |
||
| 64 | const CONTINUOUS_FACING = 3; |
||
| 65 | |||
| 66 | const NONE_VISIBLE = 1; |
||
| 67 | const THUMBNAILS_VISIBLE = 2; |
||
| 68 | const FULLSCREEN = 3; |
||
| 69 | |||
| 70 | const FIT_WIDTH = 1; |
||
| 71 | const FIT_HEIGHT = 2; |
||
| 72 | const FIT_PAGE = 3; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Pdfcrowd constructor. |
||
| 76 | * |
||
| 77 | * @param string $username |
||
| 78 | * @param string $key |
||
| 79 | */ |
||
| 80 | public function __construct(string $username, string $key) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * This method allows you to override the default CurlRequest object. Added for testing purposes. |
||
| 100 | * |
||
| 101 | * @param \Swis\PdfcrowdClient\Http\FactoryInterface $requestFactory |
||
| 102 | */ |
||
| 103 | public function setRequestFactory(FactoryInterface $requestFactory) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Each httpPost-call uses a clean request object. |
||
| 110 | * |
||
| 111 | * @return \Swis\PdfcrowdClient\Http\RequestInterface |
||
| 112 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 113 | */ |
||
| 114 | protected function getNewRequestObject(): RequestInterface |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Converts an in-memory html document. |
||
| 123 | * |
||
| 124 | * @param string $src a string containing a html document |
||
| 125 | * |
||
| 126 | * @return mixed |
||
| 127 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 128 | */ |
||
| 129 | public function convertHtml($src) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Converts a web page. |
||
| 149 | * |
||
| 150 | * @param string $src a web page URL |
||
| 151 | * |
||
| 152 | * @return mixed |
||
| 153 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 154 | */ |
||
| 155 | public function convertURI(string $src) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns the number of available conversion tokens. |
||
| 174 | * |
||
| 175 | * @return int |
||
| 176 | */ |
||
| 177 | public function availableTokens(): int |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the number of tokens used in the last conversion. |
||
| 195 | * This is only possible if you enable tracking tokens using trackTokens(true). |
||
| 196 | * |
||
| 197 | * @see trackTokens() |
||
| 198 | * |
||
| 199 | * @return int |
||
| 200 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 201 | */ |
||
| 202 | public function getUsedTokens(): int |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Track how many tokens are available before each request. |
||
| 223 | * After a request you can ask the number of used tokens with getUsedTokens. |
||
| 224 | * |
||
| 225 | * @see getUsedTokens() |
||
| 226 | * |
||
| 227 | * @param bool $trackTokens |
||
| 228 | */ |
||
| 229 | public function trackTokens(bool $trackTokens = true) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Save the pdf to the given output destination. The variable $file_handle will serve as input to |
||
| 236 | * the sink-option of Guzzle. |
||
| 237 | * |
||
| 238 | * @see http://docs.guzzlephp.org/en/stable/request-options.html#sink |
||
| 239 | * |
||
| 240 | * @example $pdfcrowd->setOutputDestination(fopen('/path/to/output.pdf', 'w'); |
||
| 241 | * |
||
| 242 | * @param $file_handle |
||
| 243 | */ |
||
| 244 | public function setOutputDestination($file_handle) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Turn SSL on or off. |
||
| 251 | * |
||
| 252 | * @param bool $use_ssl |
||
| 253 | */ |
||
| 254 | public function useSSL(bool $use_ssl) |
||
| 266 | |||
| 267 | public function setPageWidth($value) |
||
| 271 | |||
| 272 | public function setPageHeight($value) |
||
| 276 | |||
| 277 | public function setHorizontalMargin($value) |
||
| 281 | |||
| 282 | public function setVerticalMargin($value) |
||
| 286 | |||
| 287 | public function setBottomMargin($value) |
||
| 291 | |||
| 292 | public function setPageMargins($top, $right, $bottom, $left) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * If value is set to True then the PDF is encrypted. This prevents search engines from indexing the document. |
||
| 302 | * The default is False. |
||
| 303 | * |
||
| 304 | * @param bool $val |
||
| 305 | */ |
||
| 306 | public function setEncrypted(bool $val = true) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Protects the PDF with a user password. When a PDF has a user password, it must be supplied in order to view the |
||
| 313 | * document and to perform operations allowed by the access permissions. At most 32 characters. |
||
| 314 | * |
||
| 315 | * @param string $pwd |
||
| 316 | */ |
||
| 317 | public function setUserPassword(string $pwd) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Protects the PDF with an owner password. Supplying an owner password grants unlimited access to the PDF |
||
| 324 | * including changing the passwords and access permissions. At most 32 characters. |
||
| 325 | * |
||
| 326 | * @param string $pwd |
||
| 327 | */ |
||
| 328 | public function setOwnerPassword(string $pwd) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set value to True disables printing the generated PDF. The default is False. |
||
| 335 | * |
||
| 336 | * @param bool $val |
||
| 337 | */ |
||
| 338 | public function setNoPrint(bool $val = true) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Set value to True to disable modifying the PDF. The default is False. |
||
| 345 | * |
||
| 346 | * @param bool $val |
||
| 347 | */ |
||
| 348 | public function setNoModify(bool $val = true) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Set value to True to disable extracting text and graphics from the PDF. The default is False. |
||
| 355 | * |
||
| 356 | * @param bool $val |
||
| 357 | */ |
||
| 358 | public function setNoCopy(bool $val = true) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Specifies the initial page layout when the PDF is opened in a viewer. |
||
| 365 | * |
||
| 366 | * Possible values: |
||
| 367 | * \Swis\PdfcrowdClient\Pdfcrowd::SINGLE_PAGE |
||
| 368 | * \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS |
||
| 369 | * \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS_FACING |
||
| 370 | * |
||
| 371 | * @param int $value |
||
| 372 | */ |
||
| 373 | public function setPageLayout(int $value) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Specifies the appearance of the PDF when opened. |
||
| 381 | * |
||
| 382 | * Possible values: |
||
| 383 | * \Swis\PdfcrowdClient\Pdfcrowd::NONE_VISIBLE |
||
| 384 | * \Swis\PdfcrowdClient\Pdfcrowd::THUMBNAILS_VISIBLE |
||
| 385 | * \Swis\PdfcrowdClient\Pdfcrowd::FULLSCREEN |
||
| 386 | * |
||
| 387 | * @param int $value |
||
| 388 | */ |
||
| 389 | public function setPageMode(int $value) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $value |
||
| 397 | */ |
||
| 398 | public function setFooterText(string $value) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Set value to False to disable printing images to the PDF. The default is True. |
||
| 405 | * |
||
| 406 | * @param bool $value |
||
| 407 | */ |
||
| 408 | public function enableImages(bool $value = true) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Set value to False to disable printing backgrounds to the PDF. The default is True. |
||
| 415 | * |
||
| 416 | * @param bool $value |
||
| 417 | */ |
||
| 418 | public function enableBackgrounds(bool $value = true) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Set HTML zoom in percents. It determines the precision used for rendering of the HTML content. Despite its name, |
||
| 425 | * it does not zoom the HTML content. Higher values can improve glyph positioning and can lead to overall better |
||
| 426 | * visual appearance of generated PDF .The default value is 200. |
||
| 427 | * |
||
| 428 | * @see setPdfScalingFactor |
||
| 429 | * |
||
| 430 | * @param int $value |
||
| 431 | */ |
||
| 432 | public function setHtmlZoom(int $value) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Set value to False to disable JavaScript in web pages. The default is True. |
||
| 439 | * |
||
| 440 | * @param bool $value |
||
| 441 | */ |
||
| 442 | public function enableJavaScript(bool $value = true) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Set value to False to disable hyperlinks in the PDF. The default is True. |
||
| 449 | * |
||
| 450 | * @param bool $value |
||
| 451 | */ |
||
| 452 | public function enableHyperlinks(bool $value = true) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Value is the text encoding used when none is specified in a web page. The default is utf-8. |
||
| 459 | * |
||
| 460 | * @param string $value |
||
| 461 | */ |
||
| 462 | public function setDefaultTextEncoding(string $value) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * If value is True then the print CSS media type is used (if available). |
||
| 469 | * |
||
| 470 | * @param bool $value |
||
| 471 | */ |
||
| 472 | public function usePrintMedia(bool $value = true) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Prints at most npages pages. |
||
| 479 | * |
||
| 480 | * @param int $value |
||
| 481 | */ |
||
| 482 | public function setMaxPages(int $value) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @param bool $value |
||
| 489 | */ |
||
| 490 | public function enablePdfcrowdLogo(bool $value = true) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * value specifies the appearance of the PDF when opened. |
||
| 497 | * |
||
| 498 | * Possible values: |
||
| 499 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_WIDTH |
||
| 500 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_HEIGHT |
||
| 501 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_PAGE |
||
| 502 | * |
||
| 503 | * @param int $value |
||
| 504 | */ |
||
| 505 | public function setInitialPdfZoomType(int $value) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * value specifies the initial page zoom of the PDF when opened. |
||
| 513 | * |
||
| 514 | * @param $value |
||
| 515 | */ |
||
| 516 | public function setInitialPdfExactZoom($value) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * The scaling factor used to convert between HTML and PDF. The default value is 1.0. |
||
| 524 | * |
||
| 525 | * @param float $value |
||
| 526 | */ |
||
| 527 | public function setPdfScalingFactor(float $value) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Sets the author field in the created PDF. |
||
| 534 | * |
||
| 535 | * @param string $value |
||
| 536 | */ |
||
| 537 | public function setAuthor(string $value) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * If value is True then the conversion will fail when the source URI returns 4xx or 5xx HTTP status code. The |
||
| 544 | * default is False. |
||
| 545 | * |
||
| 546 | * @param bool $value |
||
| 547 | */ |
||
| 548 | public function setFailOnNon200(bool $value) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Places the specified html code inside the page footer. The following variables are expanded: |
||
| 555 | * %u - URL to convert. |
||
| 556 | * %p - The current page number. |
||
| 557 | * %n - Total number of pages. |
||
| 558 | * |
||
| 559 | * @param string $value |
||
| 560 | */ |
||
| 561 | public function setFooterHtml(string $value) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Loads HTML code from the specified url and places it inside the page footer. See setFooterHtml for the list of |
||
| 568 | * variables that are expanded. |
||
| 569 | * |
||
| 570 | * @see setFooterHtml |
||
| 571 | * |
||
| 572 | * @param string $value |
||
| 573 | */ |
||
| 574 | public function setFooterUrl(string $value) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Places the specified html code inside the page header. See setFooterHtml for the list of variables that are |
||
| 581 | * expanded. |
||
| 582 | * |
||
| 583 | * @see setFooterHtml |
||
| 584 | * |
||
| 585 | * @param string $value |
||
| 586 | */ |
||
| 587 | public function setHeaderHtml(string $value) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Loads HTML code from the specified url and places it inside the page header. See setFooterHtml for the list of |
||
| 594 | * variables that are expanded. |
||
| 595 | * |
||
| 596 | * @see setFooterHtml |
||
| 597 | * |
||
| 598 | * @param string $value |
||
| 599 | */ |
||
| 600 | public function setHeaderUrl(string $value) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * The page background color in RRGGBB hexadecimal format. |
||
| 607 | * |
||
| 608 | * @param string $value |
||
| 609 | */ |
||
| 610 | public function setPageBackgroundColor(string $value) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Does not print the body background. Requires the following CSS rule to be declared: |
||
| 617 | * body {background-color:rgba(255,255,255,0.0);} |
||
| 618 | * |
||
| 619 | * @param bool $value |
||
| 620 | */ |
||
| 621 | public function setTransparentBackground(bool $value = true) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * An offset between physical and logical page numbers. The default value is 0. |
||
| 628 | * |
||
| 629 | * @example if set to "1" then the page numbering will start with 1 on the second page. |
||
| 630 | * |
||
| 631 | * @param int $value |
||
| 632 | */ |
||
| 633 | public function setPageNumberingOffset(int $value) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Value is a comma seperated list of physical page numbers on which the header a footer are not printed. Negative |
||
| 640 | * numbers count backwards from the last page: -1 is the last page, -2 is the last but one page, and so on. |
||
| 641 | * |
||
| 642 | * @example "1,-1" will not print the header and footer on the first and the last page. |
||
| 643 | * |
||
| 644 | * @param string $value |
||
| 645 | */ |
||
| 646 | public function setHeaderFooterPageExcludeList(string $value) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * url is a public absolute URL of the watermark image (must start either with http:// or https://). The supported |
||
| 653 | * formats are PNG and JPEG. offset_x and offset_y is the watermark offset in units. The default offset is (0,0). |
||
| 654 | * |
||
| 655 | * @param string $url |
||
| 656 | * @param int $offset_x |
||
| 657 | * @param int $offset_y |
||
| 658 | */ |
||
| 659 | public function setWatermark(string $url, $offset_x = 0, $offset_y = 0) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Rotates the watermark by angle degrees. |
||
| 668 | * |
||
| 669 | * @param int $angle |
||
| 670 | */ |
||
| 671 | public function setWatermarkRotationsetWatermarkRotation(int $angle) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * When value is set to True then the watermark is be placed in the background. By default, the watermark is |
||
| 678 | * placed in the foreground. |
||
| 679 | * |
||
| 680 | * @param bool $val |
||
| 681 | */ |
||
| 682 | public function setWatermarkInBackground(bool $val = true) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @param string $proxyname |
||
| 689 | * @param int $port |
||
| 690 | * @param string $username |
||
| 691 | * @param string $password |
||
| 692 | */ |
||
| 693 | public function setProxy(string $proxyname, int $port, string $username = '', string $password = '') |
||
| 700 | |||
| 701 | /** |
||
| 702 | * @param string $user_agent |
||
| 703 | */ |
||
| 704 | public function setUserAgent(string $user_agent) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @param int $timeout |
||
| 711 | */ |
||
| 712 | public function setTimeout(int $timeout) |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @param string $url |
||
| 721 | * @param array $requestBody |
||
| 722 | * |
||
| 723 | * @return mixed |
||
| 724 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 725 | */ |
||
| 726 | private function httpPost(string $url, array $requestBody) |
||
| 744 | |||
| 745 | protected function buildRequest(string $url, array $requestBody): RequestInterface |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Set or unset a parameter that will be sent with the request to the pdfcrowd API. |
||
| 783 | * |
||
| 784 | * @param mixed $val |
||
| 785 | * @param string $field |
||
| 786 | */ |
||
| 787 | private function setOrUnset($val, string $field) |
||
| 795 | } |
||
| 796 |