22
33namespace SynergiTech \ChromePDF ;
44
5- use GuzzleHttp \ Psr7 \ StreamWrapper ;
5+ use SynergiTech \ ChromePDF \ Browserless \ Client ;
66
77/**
88 * Driver to render PDFs remotely using browserless.io
99 */
1010class Browserless extends AbstractPDF
1111{
12- /**
13- * @var string|null
14- */
15- private $ apiKey ;
16- /**
17- * @var string
18- */
19- private $ apiUrl = 'https://chrome.browserless.io ' ;
12+ use Client;
13+
2014 /**
2115 * @var string
2216 */
2317 private $ pdfEndpoint = '/pdf ' ;
24- /**
25- * @var \GuzzleHttp\Client
26- */
27- private $ client ;
18+
2819 /**
2920 * @var bool
3021 */
@@ -38,25 +29,6 @@ class Browserless extends AbstractPDF
3829 */
3930 private $ timeout ;
4031
41- /**
42- * @param string $apiKey api key from browserless.io
43- * @param \GuzzleHttp\Client $client custom Guzzle client
44- */
45- public function __construct (string $ apiKey = null , $ client = null )
46- {
47- if ($ client === null ) {
48- // @codeCoverageIgnoreStart
49- $ client = new \GuzzleHttp \Client ([
50- 'base_uri ' => $ this ->apiUrl ,
51- ]);
52- // @codeCoverageIgnoreEnd
53- }
54- $ this ->client = $ client ;
55- if ($ apiKey !== null ) {
56- $ this ->setApiKey ($ apiKey );
57- }
58- }
59-
6032 /**
6133 * Sets the PDF documents rotation
6234 *
@@ -69,18 +41,6 @@ public function setRotation(int $rotation = null): self
6941 return $ this ;
7042 }
7143
72- /**
73- * Sets the browserless API key
74- *
75- * @param string $apiKey
76- * @return self
77- */
78- public function setApiKey (string $ apiKey ): self
79- {
80- $ this ->apiKey = $ apiKey ;
81- return $ this ;
82- }
83-
8444 /**
8545 * Sets whether or not to ask Browserless to attempt to render the document in safe mode
8646 *
@@ -116,16 +76,6 @@ public function getTimeout(): ?int
11676 return $ this ->timeout ;
11777 }
11878
119- /**
120- * Retrieves the browserless.io API key
121- *
122- * @return string|null
123- */
124- public function getApiKey (): ?string
125- {
126- return $ this ->apiKey ;
127- }
128-
12979 /**
13080 * Whether the document will be rendered in safe mode or not
13181 *
@@ -232,41 +182,7 @@ public function getFormattedOptions(): array
232182 */
233183 private function render (array $ options )
234184 {
235- try {
236- $ response = $ this ->client ->post ($ this ->pdfEndpoint , [
237- 'query ' => [
238- 'token ' => $ this ->getApiKey (),
239- ],
240- 'json ' => $ options ,
241- ]);
242- } catch (\GuzzleHttp \Exception \ClientException $ e ) {
243- $ message = 'No response ' ;
244-
245- $ response = $ e ->getResponse ();
246-
247- /**
248- * You could use $e->hasResponse() but that is not accurate enough,
249- * as phpstan will be analysing against method signatures from guzzle 6 & 7
250- */
251- if ($ response !== null ) {
252- $ message = $ response ->getBody ();
253-
254- $ json = json_decode ($ message );
255- if (json_last_error () === JSON_ERROR_NONE ) {
256- $ messages = [];
257- foreach ($ json as $ error ) {
258- $ messages [] = $ error ->message ;
259- }
260- $ message = implode (', ' , $ messages );
261- }
262- }
263-
264- throw new Browserless \APIException ("Failed to render PDF: {$ message }" , $ e ->getCode (), $ e );
265- } catch (\Exception $ e ) {
266- throw new Browserless \APIException ("Failed to render PDF: {$ e ->getMessage ()}" , $ e ->getCode (), $ e );
267- }
268-
269- return StreamWrapper::getResource ($ response ->getBody ());
185+ return $ this ->request ($ this ->pdfEndpoint , $ options );
270186 }
271187
272188 /**
0 commit comments