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 | 20 | 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 | 18 | 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 | 18 | 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 | 20 | 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) |
||
294 | { |
||
295 | 20 | if ($use_ssl) { |
|
296 | 20 | $this->port = self::$https_port; |
|
297 | 20 | $this->scheme = 'https'; |
|
298 | } else { |
||
299 | 2 | $this->port = self::$http_port; |
|
300 | 2 | $this->scheme = 'http'; |
|
301 | } |
||
302 | |||
303 | 20 | $this->api_prefix = "{$this->scheme}://{$this->hostname}/api"; |
|
304 | 20 | } |
|
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) |
||
722 | { |
||
723 | 2 | $this->setOrUnset($val, 'watermark_in_background'); |
|
724 | 2 | } |
|
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 = '') |
||
733 | { |
||
734 | 2 | $this->proxy_name = $proxyname; |
|
735 | 2 | $this->proxy_port = $port; |
|
736 | 2 | $this->proxy_username = $username; |
|
737 | 2 | $this->proxy_password = $password; |
|
738 | 2 | } |
|
739 | |||
740 | /** |
||
741 | * @param string $user_agent |
||
742 | */ |
||
743 | public function setUserAgent(string $user_agent) |
||
744 | { |
||
745 | 2 | $this->user_agent = $user_agent; |
|
746 | 2 | } |
|
747 | |||
748 | /** |
||
749 | * @param int $timeout |
||
750 | */ |
||
751 | public function setTimeout(int $timeout) |
||
752 | { |
||
753 | 2 | if (is_int($timeout) && $timeout > 0) { |
|
754 | 2 | $this->curlopt_timeout = $timeout; |
|
755 | } |
||
756 | 2 | } |
|
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) |
||
767 | { |
||
768 | 18 | $this->request = $this->getNewRequestObject(); |
|
769 | |||
770 | 18 | $this->request->setOption(CURLOPT_URL, $url); |
|
771 | 18 | $this->request->setOption(CURLOPT_HEADER, false); |
|
772 | 18 | $this->request->setOption(CURLOPT_CONNECTTIMEOUT, 10); |
|
773 | 18 | $this->request->setOption(CURLOPT_RETURNTRANSFER, true); |
|
774 | 18 | $this->request->setOption(CURLOPT_POST, true); |
|
775 | 18 | $this->request->setOption(CURLOPT_PORT, $this->port); |
|
776 | 18 | $this->request->setOption(CURLOPT_POSTFIELDS, $postfields); |
|
777 | 18 | $this->request->setOption(CURLOPT_DNS_USE_GLOBAL_CACHE, false); |
|
778 | 18 | $this->request->setOption(CURLOPT_USERAGENT, $this->user_agent); |
|
779 | |||
780 | 18 | if (isset($this->curlopt_timeout)) { |
|
781 | 2 | $this->request->setOption(CURLOPT_TIMEOUT, $this->curlopt_timeout); |
|
782 | } |
||
783 | |||
784 | 18 | if ($outstream) { |
|
785 | $this->outstream = $outstream; |
||
786 | $this->request->setOption(CURLOPT_WRITEFUNCTION, [$this, 'receiveToStream']); |
||
787 | } |
||
788 | |||
789 | 18 | if ($this->scheme == 'https' && self::$api_host == 'pdfcrowd.com') { |
|
790 | 16 | $this->request->setOption(CURLOPT_SSL_VERIFYPEER, true); |
|
791 | } else { |
||
792 | 2 | $this->request->setOption(CURLOPT_SSL_VERIFYPEER, false); |
|
793 | } |
||
794 | |||
795 | 18 | if ($this->proxy_name) { |
|
796 | 2 | $this->request->setOption(CURLOPT_PROXY, $this->proxy_name.':'.$this->proxy_port); |
|
797 | 2 | if ($this->proxy_username) { |
|
798 | 2 | $this->request->setOption(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
|
799 | 2 | $this->request->setOption(CURLOPT_PROXYUSERPWD, $this->proxy_username.':'.$this->proxy_password); |
|
800 | } |
||
801 | } |
||
802 | |||
803 | 18 | $this->http_code = 0; |
|
804 | 18 | $this->error = ''; |
|
805 | |||
806 | 18 | $response = $this->request->execute(); |
|
807 | 18 | $this->http_code = (int) $this->request->getInfo(CURLINFO_HTTP_CODE); |
|
808 | 18 | $error_str = $this->request->getErrorMessage(); |
|
809 | 18 | $error_nr = (int) $this->request->getErrorNumber(); |
|
810 | 18 | $this->request->close(); |
|
811 | |||
812 | 18 | if ($error_nr !== 0) { |
|
813 | 2 | throw new PdfcrowdException($error_str, $error_nr); |
|
814 | 16 | } elseif ($this->http_code === 200) { |
|
815 | 14 | if ($outstream === null) { |
|
816 | 14 | return $response; |
|
817 | } |
||
818 | } |
||
819 | |||
820 | 2 | throw new PdfcrowdException($this->error ? $this->error : $response, $this->http_code); |
|
821 | } |
||
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 |