From 677674830321715bc9908110c640e39ca54f63fb Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Sun, 14 Apr 2019 17:38:40 +0200 Subject: [PATCH] updating for 5.0.0 (#7) --- docker-compose.yml | 2 +- src/ChromeRequest.php | 58 +++++++++++++++++++++++------------------ src/HTMLRequest.php | 34 +++--------------------- src/MarkdownRequest.php | 2 +- src/MergeRequest.php | 14 +--------- src/OfficeRequest.php | 8 +++--- src/Request.php | 55 ++++++++++++++++++++++++++++++-------- src/URLRequest.php | 6 +++-- tests/ClientTest.php | 20 +++++++++++--- 9 files changed, 105 insertions(+), 94 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9aaf3ab..ef8000c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,6 @@ services: - ./:/usr/src/app:rw gotenberg: - image: thecodingmachine/gotenberg:4.1 + image: thecodingmachine/gotenberg:5.0.0 container_name: gotenberg restart: 'no' \ No newline at end of file diff --git a/src/ChromeRequest.php b/src/ChromeRequest.php index 25fcd60..a1bac5e 100644 --- a/src/ChromeRequest.php +++ b/src/ChromeRequest.php @@ -4,44 +4,53 @@ abstract class ChromeRequest extends Request implements GotenbergRequestInterface { + private const WAIT_DELAY = 'waitDelay'; + private const PAPER_WIDTH = 'paperWidth'; + private const PAPER_HEIGHT = 'paperHeight'; + private const MARGIN_TOP = 'marginTop'; + private const MARGIN_BOTTOM = 'marginBottom'; + private const MARGIN_LEFT = 'marginLeft'; + private const MARGIN_RIGHT = 'marginRight'; + private const LANDSCAPE = 'landscape'; + + /** @var float|null */ + private $waitDelay; + /** @var Document|null */ - protected $header; + private $header; /** @var Document|null */ - protected $footer; + private $footer; /** @var float|null */ - protected $paperWidth; + private $paperWidth; /** @var float|null */ - protected $paperHeight; + private $paperHeight; /** @var float|null */ - protected $marginTop; + private $marginTop; /** @var float|null */ - protected $marginBottom; + private $marginBottom; /** @var float|null */ - protected $marginLeft; + private $marginLeft; /** @var float|null */ - protected $marginRight; + private $marginRight; /** @var bool */ - protected $landscape; - - /** @var int|null */ - protected $webFontsTimeout; + private $landscape; /** * @return array */ public function getFormValues(): array { - $values = []; - if (!empty($this->webhookURL)) { - $values[self::WEBHOOK_URL] = $this->webhookURL; + $values = parent::getFormValues(); + if (!is_null($this->waitDelay)) { + $values[self::WAIT_DELAY] = $this->waitDelay; } if (!is_null($this->paperWidth)) { $values[self::PAPER_WIDTH] = $this->paperWidth; @@ -61,9 +70,6 @@ public function getFormValues(): array if (!is_null($this->marginRight)) { $values[self::MARGIN_RIGHT] = $this->marginRight; } - if (!is_null($this->webFontsTimeout)) { - $values[self::WEB_FONTS_TIMEOUT] = $this->webFontsTimeout; - } $values[self::LANDSCAPE] = $this->landscape; return $values; } @@ -83,6 +89,14 @@ public function getFormFiles(): array return $files; } + /** + * @param float|null $waitDelay + */ + public function setWaitDelay(?float $waitDelay): void + { + $this->waitDelay = $waitDelay; + } + /** * @param float[] $paperSize * @throws RequestException @@ -182,12 +196,4 @@ public function setLandscape(bool $landscape): void { $this->landscape = $landscape; } - - /** - * @param int|null $webFontsTimeout - */ - public function setWebFontsTimeout(?int $webFontsTimeout): void - { - $this->webFontsTimeout = $webFontsTimeout; - } } diff --git a/src/HTMLRequest.php b/src/HTMLRequest.php index 9c3b40f..f09e158 100644 --- a/src/HTMLRequest.php +++ b/src/HTMLRequest.php @@ -2,13 +2,13 @@ namespace TheCodingMachine\Gotenberg; -class HTMLRequest extends ChromeRequest +class HTMLRequest extends ChromeRequest implements GotenbergRequestInterface { /** @var Document */ - protected $index; + private $index; /** @var Document[] */ - protected $assets; + private $assets; /** * HTMLRequest constructor. @@ -41,34 +41,6 @@ public function getFormFiles(): array return $files; } - /** - * @param float[] $paperSize - * @throws RequestException - */ - public function setPaperSize(array $paperSize): void - { - if (count($paperSize) !== 2) { - throw new RequestException('Wrong paper size.'); - } - $this->paperWidth = $paperSize[0]; - $this->paperHeight = $paperSize[1]; - } - - /** - * @param float[] $margins - * @throws RequestException - */ - public function setMargins(array $margins): void - { - if (count($margins) !== 4) { - throw new RequestException('Wrong margins.'); - } - $this->marginTop = $margins[0]; - $this->marginBottom = $margins[1]; - $this->marginLeft = $margins[2]; - $this->marginRight = $margins[3]; - } - /** * @param Document[] $assets */ diff --git a/src/MarkdownRequest.php b/src/MarkdownRequest.php index 1e1f157..6b6f03d 100644 --- a/src/MarkdownRequest.php +++ b/src/MarkdownRequest.php @@ -2,7 +2,7 @@ namespace TheCodingMachine\Gotenberg; -final class MarkdownRequest extends HTMLRequest +final class MarkdownRequest extends HTMLRequest implements GotenbergRequestInterface { /** @var Document[] */ private $markdowns; diff --git a/src/MergeRequest.php b/src/MergeRequest.php index 7e69b15..9a43f96 100644 --- a/src/MergeRequest.php +++ b/src/MergeRequest.php @@ -22,19 +22,7 @@ public function __construct(array $files) */ public function getPostURL(): string { - return '/merge'; - } - - /** - * @return array - */ - public function getFormValues(): array - { - $values = []; - if (!empty($this->webhookURL)) { - $values[self::WEBHOOK_URL] = $this->webhookURL; - } - return $values; + return '/convert/merge'; } /** diff --git a/src/OfficeRequest.php b/src/OfficeRequest.php index 82b0bf7..5a07d92 100644 --- a/src/OfficeRequest.php +++ b/src/OfficeRequest.php @@ -4,6 +4,8 @@ final class OfficeRequest extends Request implements GotenbergRequestInterface { + private const LANDSCAPE = 'landscape'; + /** @var Document[] */ private $files; @@ -19,7 +21,6 @@ public function __construct(array $files) $this->files = $files; } - /** * @return string */ @@ -33,10 +34,7 @@ public function getPostURL(): string */ public function getFormValues(): array { - $values = []; - if (!empty($this->webhookURL)) { - $values[self::WEBHOOK_URL] = $this->webhookURL; - } + $values = parent::getFormValues(); $values[self::LANDSCAPE] = $this->landscape; return $values; } diff --git a/src/Request.php b/src/Request.php index aeeba84..3bae302 100644 --- a/src/Request.php +++ b/src/Request.php @@ -16,19 +16,52 @@ abstract class Request public const NORMAL_MARGINS = [1, 1, 1, 1]; public const LARGE_MARGINS = [2, 2, 2, 2]; - protected const REMOTE_URL = 'remoteURL'; - protected const WEBHOOK_URL = 'webhookURL'; - protected const PAPER_WIDTH = 'paperWidth'; - protected const PAPER_HEIGHT = 'paperHeight'; - protected const MARGIN_TOP = 'marginTop'; - protected const MARGIN_BOTTOM = 'marginBottom'; - protected const MARGIN_LEFT = 'marginLeft'; - protected const MARGIN_RIGHT = 'marginRight'; - protected const LANDSCAPE = 'landscape'; - protected const WEB_FONTS_TIMEOUT = 'webFontsTimeout'; + private const RESULT_FILENAME = 'resultFilename'; + private const WAIT_TIMEOUT = 'waitTimeout'; + private const WEBHOOK_URL = 'webhookURL'; /** @var string|null */ - protected $webhookURL; + private $resultFilename; + + /** @var float|null */ + private $waitTimeout; + + /** @var string|null */ + private $webhookURL; + + /** + * @return array + */ + public function getFormValues(): array + { + $values = []; + if (!empty($this->resultFilename)) { + $values[self::RESULT_FILENAME] = $this->resultFilename; + } + if (!is_null($this->waitTimeout)) { + $values[self::WAIT_TIMEOUT] = $this->waitTimeout; + } + if (!empty($this->webhookURL)) { + $values[self::WEBHOOK_URL] = $this->webhookURL; + } + return $values; + } + + /** + * @param string|null $resultFilename + */ + public function setResultFilename(?string $resultFilename): void + { + $this->resultFilename = $resultFilename; + } + + /** + * @param float|null $waitTimeout + */ + public function setWaitTimeout(?float $waitTimeout): void + { + $this->waitTimeout = $waitTimeout; + } /** * @param string|null $webhookURL diff --git a/src/URLRequest.php b/src/URLRequest.php index 81d8ef2..262520f 100644 --- a/src/URLRequest.php +++ b/src/URLRequest.php @@ -2,10 +2,12 @@ namespace TheCodingMachine\Gotenberg; -class URLRequest extends ChromeRequest +final class URLRequest extends ChromeRequest implements GotenbergRequestInterface { + private const REMOTE_URL = 'remoteURL'; + /** @var string */ - protected $URL; + private $URL; /** * HTMLRequest constructor. diff --git a/tests/ClientTest.php b/tests/ClientTest.php index bba3383..21aae94 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -51,12 +51,14 @@ private function createHTMLRequest(): HTMLRequest DocumentFactory::makeFromPath('style.css', __DIR__ . '/assets/html/style.css'), ]; $request = new HTMLRequest($index); + $request->setResultFilename('foo.pdf'); + $request->setWaitTimeout(5); + $request->setWaitDelay(1); $request->setHeader($header); $request->setFooter($footer); $request->setAssets($assets); $request->setPaperSize(Request::A4); $request->setMargins(Request::NO_MARGINS); - $request->setWebFontsTimeout(500); return $request; } @@ -69,11 +71,13 @@ private function createURLRequest(): URLRequest $header = DocumentFactory::makeFromPath('header.html', __DIR__ . '/assets/url/header.html'); $footer = DocumentFactory::makeFromPath('footer.html', __DIR__ . '/assets/url/footer.html'); $request = new URLRequest('https://google.com'); + $request->setResultFilename('foo.pdf'); + $request->setWaitTimeout(5); + $request->setWaitDelay(1); $request->setHeader($header); $request->setFooter($footer); $request->setPaperSize(Request::A4); $request->setMargins(Request::NO_MARGINS); - $request->setWebFontsTimeout(500); return $request; } @@ -97,12 +101,14 @@ public function createMarkdownRequest(): MarkdownRequest DocumentFactory::makeFromPath('style.css', __DIR__ . '/assets/markdown/style.css'), ]; $request = new MarkdownRequest($index, $markdowns); + $request->setResultFilename('foo.pdf'); + $request->setWaitTimeout(5); + $request->setWaitDelay(1); $request->setHeader($header); $request->setFooter($footer); $request->setAssets($assets); $request->setPaperSize(Request::A4); $request->setMargins(Request::NO_MARGINS); - $request->setWebFontsTimeout(500); return $request; } @@ -115,6 +121,9 @@ public function createOfficeRequest(): OfficeRequest DocumentFactory::makeFromPath('document.docx', __DIR__ . '/assets/office/document.docx'), ]; $request = new OfficeRequest($files); + $request->setResultFilename('foo.pdf'); + $request->setWaitTimeout(5); + $request->setLandscape(false); return $request; } @@ -128,12 +137,14 @@ public function createMergeRequest(): MergeRequest DocumentFactory::makeFromPath('gotenberg2.pdf', __DIR__ . '/assets/pdf/gotenberg.pdf'), ]; $request = new MergeRequest($files); - + $request->setResultFilename('foo.pdf'); + $request->setWaitTimeout(5); return $request; } /** * @throws ClientException + * @throws \HTTP\Client\Exception */ function testPost() { @@ -163,6 +174,7 @@ function testPost() /** * @throws ClientException * @throws FilesystemException + * @throws \HTTP\Client\Exception */ function testStore() {