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 string */ |
||
| 21 | private $api_prefix; |
||
| 22 | |||
| 23 | /** @var int */ |
||
| 24 | private $curlopt_timeout; |
||
| 25 | |||
| 26 | /** @var string */ |
||
| 27 | private $hostname; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | private $user_agent; |
||
| 31 | |||
| 32 | /** @var int */ |
||
| 33 | private $num_tokens_before = false; |
||
| 34 | |||
| 35 | /** @var int */ |
||
| 36 | private $http_code; |
||
| 37 | |||
| 38 | /** @var FactoryInterface */ |
||
| 39 | protected $requestFactory; |
||
| 40 | |||
| 41 | /* @var RequestInterface */ |
||
| 42 | private $request; |
||
| 43 | |||
| 44 | /** @var bool */ |
||
| 45 | private $track_tokens = false; |
||
| 46 | |||
| 47 | protected $output_destination; |
||
| 48 | |||
| 49 | public static $client_version = '2.7'; |
||
| 50 | public static $api_host = 'pdfcrowd.com'; |
||
| 51 | |||
| 52 | private $proxy_name; |
||
| 53 | private $proxy_port; |
||
| 54 | private $proxy_username = ''; |
||
| 55 | private $proxy_password = ''; |
||
| 56 | |||
| 57 | const SINGLE_PAGE = 1; |
||
| 58 | const CONTINUOUS = 2; |
||
| 59 | const CONTINUOUS_FACING = 3; |
||
| 60 | |||
| 61 | const NONE_VISIBLE = 1; |
||
| 62 | const THUMBNAILS_VISIBLE = 2; |
||
| 63 | const FULLSCREEN = 3; |
||
| 64 | |||
| 65 | const FIT_WIDTH = 1; |
||
| 66 | const FIT_HEIGHT = 2; |
||
| 67 | const FIT_PAGE = 3; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Pdfcrowd constructor. |
||
| 71 | * |
||
| 72 | * @param string $username |
||
| 73 | * @param string $key |
||
| 74 | */ |
||
| 75 | 144 | public function __construct(string $username, string $key) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * This method allows you to override the default CurlRequest object. Added for testing purposes. |
||
| 95 | * |
||
| 96 | * @param \Swis\PdfcrowdClient\Http\FactoryInterface $requestFactory |
||
| 97 | */ |
||
| 98 | 140 | public function setRequestFactory(FactoryInterface $requestFactory) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Each httpPost-call uses a clean request object. |
||
| 105 | * |
||
| 106 | * @return \Swis\PdfcrowdClient\Http\RequestInterface |
||
| 107 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 108 | */ |
||
| 109 | 136 | protected function getNewRequestObject(): RequestInterface |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Converts an in-memory html document. |
||
| 118 | * |
||
| 119 | * @param string $src a string containing a html document |
||
| 120 | * |
||
| 121 | * @return mixed |
||
| 122 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 123 | */ |
||
| 124 | 132 | public function convertHtml($src) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Converts a web page. |
||
| 144 | * |
||
| 145 | * @param string $src a web page URL |
||
| 146 | * |
||
| 147 | * @return mixed |
||
| 148 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 149 | */ |
||
| 150 | 6 | public function convertURI(string $src) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the number of available conversion tokens. |
||
| 169 | * |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | 6 | public function availableTokens(): int |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Get the number of tokens used in the last conversion. |
||
| 190 | * This is only possible if you enable tracking tokens using trackTokens(true). |
||
| 191 | * |
||
| 192 | * @see trackTokens() |
||
| 193 | * |
||
| 194 | * @return int |
||
| 195 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 196 | */ |
||
| 197 | 8 | public function getUsedTokens(): int |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Track how many tokens are available before each request. |
||
| 218 | * After a request you can ask the number of used tokens with getUsedTokens. |
||
| 219 | * |
||
| 220 | * @see getUsedTokens() |
||
| 221 | * |
||
| 222 | * @param bool $trackTokens |
||
| 223 | */ |
||
| 224 | 10 | public function trackTokens(bool $trackTokens = true) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Save the pdf to the given output destination. The variable $file_handle will serve as input to |
||
| 231 | * the sink-option of Guzzle. |
||
| 232 | * |
||
| 233 | * @see http://docs.guzzlephp.org/en/stable/request-options.html#sink |
||
| 234 | * |
||
| 235 | * @example $pdfcrowd->setOutputDestination(fopen('/path/to/output.pdf', 'w'); |
||
| 236 | * |
||
| 237 | * @param $file_handle |
||
| 238 | */ |
||
| 239 | 2 | public function setOutputDestination($file_handle) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Turn SSL on or off. |
||
| 246 | * |
||
| 247 | * @param bool $use_ssl |
||
| 248 | */ |
||
| 249 | 144 | public function useSSL(bool $use_ssl) |
|
| 259 | |||
| 260 | 2 | public function setPageWidth($value) |
|
| 264 | |||
| 265 | 2 | public function setPageHeight($value) |
|
| 269 | |||
| 270 | 4 | public function setHorizontalMargin($value) |
|
| 274 | |||
| 275 | 4 | public function setVerticalMargin($value) |
|
| 279 | |||
| 280 | 2 | public function setBottomMargin($value) |
|
| 284 | |||
| 285 | 8 | public function setPageMargins($top, $right, $bottom, $left) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * If value is set to True then the PDF is encrypted. This prevents search engines from indexing the document. |
||
| 295 | * The default is False. |
||
| 296 | * |
||
| 297 | * @param bool $val |
||
| 298 | */ |
||
| 299 | 2 | public function setEncrypted(bool $val = true) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Protects the PDF with a user password. When a PDF has a user password, it must be supplied in order to view the |
||
| 306 | * document and to perform operations allowed by the access permissions. At most 32 characters. |
||
| 307 | * |
||
| 308 | * @param string $pwd |
||
| 309 | */ |
||
| 310 | 2 | public function setUserPassword(string $pwd) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Protects the PDF with an owner password. Supplying an owner password grants unlimited access to the PDF |
||
| 317 | * including changing the passwords and access permissions. At most 32 characters. |
||
| 318 | * |
||
| 319 | * @param string $pwd |
||
| 320 | */ |
||
| 321 | 2 | public function setOwnerPassword(string $pwd) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Set value to True disables printing the generated PDF. The default is False. |
||
| 328 | * |
||
| 329 | * @param bool $val |
||
| 330 | */ |
||
| 331 | 2 | public function setNoPrint(bool $val = true) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Set value to True to disable modifying the PDF. The default is False. |
||
| 338 | * |
||
| 339 | * @param bool $val |
||
| 340 | */ |
||
| 341 | 2 | public function setNoModify(bool $val = true) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Set value to True to disable extracting text and graphics from the PDF. The default is False. |
||
| 348 | * |
||
| 349 | * @param bool $val |
||
| 350 | */ |
||
| 351 | 2 | public function setNoCopy(bool $val = true) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Specifies the initial page layout when the PDF is opened in a viewer. |
||
| 358 | * |
||
| 359 | * Possible values: |
||
| 360 | * \Swis\PdfcrowdClient\Pdfcrowd::SINGLE_PAGE |
||
| 361 | * \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS |
||
| 362 | * \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS_FACING |
||
| 363 | * |
||
| 364 | * @param int $value |
||
| 365 | */ |
||
| 366 | 6 | public function setPageLayout(int $value) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Specifies the appearance of the PDF when opened. |
||
| 374 | * |
||
| 375 | * Possible values: |
||
| 376 | * \Swis\PdfcrowdClient\Pdfcrowd::NONE_VISIBLE |
||
| 377 | * \Swis\PdfcrowdClient\Pdfcrowd::THUMBNAILS_VISIBLE |
||
| 378 | * \Swis\PdfcrowdClient\Pdfcrowd::FULLSCREEN |
||
| 379 | * |
||
| 380 | * @param int $value |
||
| 381 | */ |
||
| 382 | 6 | public function setPageMode(int $value) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $value |
||
| 390 | */ |
||
| 391 | 2 | public function setFooterText(string $value) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Set value to False to disable printing images to the PDF. The default is True. |
||
| 398 | * |
||
| 399 | * @param bool $value |
||
| 400 | */ |
||
| 401 | 2 | public function enableImages(bool $value = true) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Set value to False to disable printing backgrounds to the PDF. The default is True. |
||
| 408 | * |
||
| 409 | * @param bool $value |
||
| 410 | */ |
||
| 411 | 2 | public function enableBackgrounds(bool $value = true) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Set HTML zoom in percents. It determines the precision used for rendering of the HTML content. Despite its name, |
||
| 418 | * it does not zoom the HTML content. Higher values can improve glyph positioning and can lead to overall better |
||
| 419 | * visual appearance of generated PDF .The default value is 200. |
||
| 420 | * |
||
| 421 | * @see setPdfScalingFactor |
||
| 422 | * |
||
| 423 | * @param int $value |
||
| 424 | */ |
||
| 425 | 2 | public function setHtmlZoom(int $value) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Set value to False to disable JavaScript in web pages. The default is True. |
||
| 432 | * |
||
| 433 | * @param bool $value |
||
| 434 | */ |
||
| 435 | 2 | public function enableJavaScript(bool $value = true) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Set value to False to disable hyperlinks in the PDF. The default is True. |
||
| 442 | * |
||
| 443 | * @param bool $value |
||
| 444 | */ |
||
| 445 | 2 | public function enableHyperlinks(bool $value = true) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Value is the text encoding used when none is specified in a web page. The default is utf-8. |
||
| 452 | * |
||
| 453 | * @param string $value |
||
| 454 | */ |
||
| 455 | 2 | public function setDefaultTextEncoding(string $value) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * If value is True then the print CSS media type is used (if available). |
||
| 462 | * |
||
| 463 | * @param bool $value |
||
| 464 | */ |
||
| 465 | 2 | public function usePrintMedia(bool $value = true) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Prints at most npages pages. |
||
| 472 | * |
||
| 473 | * @param int $value |
||
| 474 | */ |
||
| 475 | 2 | public function setMaxPages(int $value) |
|
| 479 | |||
| 480 | /** |
||
| 481 | * @param bool $value |
||
| 482 | */ |
||
| 483 | 2 | public function enablePdfcrowdLogo(bool $value = true) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * value specifies the appearance of the PDF when opened. |
||
| 490 | * |
||
| 491 | * Possible values: |
||
| 492 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_WIDTH |
||
| 493 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_HEIGHT |
||
| 494 | * \Swis\Pdfcrowd\Pdfcrowd::FIT_PAGE |
||
| 495 | * |
||
| 496 | * @param int $value |
||
| 497 | */ |
||
| 498 | 6 | public function setInitialPdfZoomType(int $value) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * value specifies the initial page zoom of the PDF when opened. |
||
| 506 | * |
||
| 507 | * @param $value |
||
| 508 | */ |
||
| 509 | 4 | public function setInitialPdfExactZoom($value) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * The scaling factor used to convert between HTML and PDF. The default value is 1.0. |
||
| 517 | * |
||
| 518 | * @param float $value |
||
| 519 | */ |
||
| 520 | 2 | public function setPdfScalingFactor(float $value) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Sets the author field in the created PDF. |
||
| 527 | * |
||
| 528 | * @param string $value |
||
| 529 | */ |
||
| 530 | 2 | public function setAuthor(string $value) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * If value is True then the conversion will fail when the source URI returns 4xx or 5xx HTTP status code. The |
||
| 537 | * default is False. |
||
| 538 | * |
||
| 539 | * @param bool $value |
||
| 540 | */ |
||
| 541 | 2 | public function setFailOnNon200(bool $value) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Places the specified html code inside the page footer. The following variables are expanded: |
||
| 548 | * %u - URL to convert. |
||
| 549 | * %p - The current page number. |
||
| 550 | * %n - Total number of pages. |
||
| 551 | * |
||
| 552 | * @param string $value |
||
| 553 | */ |
||
| 554 | 2 | public function setFooterHtml(string $value) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Loads HTML code from the specified url and places it inside the page footer. See setFooterHtml for the list of |
||
| 561 | * variables that are expanded. |
||
| 562 | * |
||
| 563 | * @see setFooterHtml |
||
| 564 | * |
||
| 565 | * @param string $value |
||
| 566 | */ |
||
| 567 | 2 | public function setFooterUrl(string $value) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Places the specified html code inside the page header. See setFooterHtml for the list of variables that are |
||
| 574 | * expanded. |
||
| 575 | * |
||
| 576 | * @see setFooterHtml |
||
| 577 | * |
||
| 578 | * @param string $value |
||
| 579 | */ |
||
| 580 | 2 | public function setHeaderHtml(string $value) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Loads HTML code from the specified url and places it inside the page header. See setFooterHtml for the list of |
||
| 587 | * variables that are expanded. |
||
| 588 | * |
||
| 589 | * @see setFooterHtml |
||
| 590 | * |
||
| 591 | * @param string $value |
||
| 592 | */ |
||
| 593 | 2 | public function setHeaderUrl(string $value) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * The page background color in RRGGBB hexadecimal format. |
||
| 600 | * |
||
| 601 | * @param string $value |
||
| 602 | */ |
||
| 603 | 2 | public function setPageBackgroundColor(string $value) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Does not print the body background. Requires the following CSS rule to be declared: |
||
| 610 | * body {background-color:rgba(255,255,255,0.0);} |
||
| 611 | * |
||
| 612 | * @param bool $value |
||
| 613 | */ |
||
| 614 | 2 | public function setTransparentBackground(bool $value = true) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * An offset between physical and logical page numbers. The default value is 0. |
||
| 621 | * |
||
| 622 | * @example if set to "1" then the page numbering will start with 1 on the second page. |
||
| 623 | * |
||
| 624 | * @param int $value |
||
| 625 | */ |
||
| 626 | 2 | public function setPageNumberingOffset(int $value) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Value is a comma seperated list of physical page numbers on which the header a footer are not printed. Negative |
||
| 633 | * numbers count backwards from the last page: -1 is the last page, -2 is the last but one page, and so on. |
||
| 634 | * |
||
| 635 | * @example "1,-1" will not print the header and footer on the first and the last page. |
||
| 636 | * |
||
| 637 | * @param string $value |
||
| 638 | */ |
||
| 639 | 2 | public function setHeaderFooterPageExcludeList(string $value) |
|
| 643 | |||
| 644 | /** |
||
| 645 | * url is a public absolute URL of the watermark image (must start either with http:// or https://). The supported |
||
| 646 | * formats are PNG and JPEG. offset_x and offset_y is the watermark offset in units. The default offset is (0,0). |
||
| 647 | * |
||
| 648 | * @param string $url |
||
| 649 | * @param int $offset_x |
||
| 650 | * @param int $offset_y |
||
| 651 | */ |
||
| 652 | 6 | public function setWatermark(string $url, $offset_x = 0, $offset_y = 0) |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Rotates the watermark by angle degrees. |
||
| 661 | * |
||
| 662 | * @param int $angle |
||
| 663 | */ |
||
| 664 | 2 | public function setWatermarkRotationsetWatermarkRotation(int $angle) |
|
| 668 | |||
| 669 | /** |
||
| 670 | * When value is set to True then the watermark is be placed in the background. By default, the watermark is |
||
| 671 | * placed in the foreground. |
||
| 672 | * |
||
| 673 | * @param bool $val |
||
| 674 | */ |
||
| 675 | 4 | public function setWatermarkInBackground(bool $val = true) |
|
| 679 | |||
| 680 | /** |
||
| 681 | * @param string $proxyname |
||
| 682 | * @param int $port |
||
| 683 | * @param string $username |
||
| 684 | * @param string $password |
||
| 685 | */ |
||
| 686 | 2 | public function setProxy(string $proxyname, int $port, string $username = '', string $password = '') |
|
| 693 | |||
| 694 | /** |
||
| 695 | * @param string $user_agent |
||
| 696 | */ |
||
| 697 | 2 | public function setUserAgent(string $user_agent) |
|
| 701 | |||
| 702 | /** |
||
| 703 | * @param int $timeout |
||
| 704 | */ |
||
| 705 | 2 | public function setTimeout(int $timeout) |
|
| 711 | |||
| 712 | /** |
||
| 713 | * @param string $url |
||
| 714 | * @param array $requestBody |
||
| 715 | * |
||
| 716 | * @return mixed |
||
| 717 | * @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
||
| 718 | */ |
||
| 719 | 136 | private function httpPost(string $url, array $requestBody) |
|
| 737 | |||
| 738 | 136 | protected function buildRequest(string $url, array $requestBody): RequestInterface |
|
| 771 | |||
| 772 | /** |
||
| 773 | * Set or unset a parameter that will be sent with the request to the pdfcrowd API. |
||
| 774 | * |
||
| 775 | * @param mixed $val |
||
| 776 | * @param string $field |
||
| 777 | */ |
||
| 778 | 36 | private function setOrUnset($val, string $field) |
|
| 786 | } |
||
| 787 |