Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
updating for 5.0.0 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien authored Apr 14, 2019
1 parent e2a0ddc commit 6776748
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 94 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
58 changes: 32 additions & 26 deletions src/ChromeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,mixed>
*/
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;
Expand All @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
34 changes: 3 additions & 31 deletions src/HTMLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/MarkdownRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TheCodingMachine\Gotenberg;

final class MarkdownRequest extends HTMLRequest
final class MarkdownRequest extends HTMLRequest implements GotenbergRequestInterface
{
/** @var Document[] */
private $markdowns;
Expand Down
14 changes: 1 addition & 13 deletions src/MergeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,7 @@ public function __construct(array $files)
*/
public function getPostURL(): string
{
return '/merge';
}

/**
* @return array<string,mixed>
*/
public function getFormValues(): array
{
$values = [];
if (!empty($this->webhookURL)) {
$values[self::WEBHOOK_URL] = $this->webhookURL;
}
return $values;
return '/convert/merge';
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/OfficeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

final class OfficeRequest extends Request implements GotenbergRequestInterface
{
private const LANDSCAPE = 'landscape';

/** @var Document[] */
private $files;

Expand All @@ -19,7 +21,6 @@ public function __construct(array $files)
$this->files = $files;
}


/**
* @return string
*/
Expand All @@ -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;
}
Expand Down
55 changes: 44 additions & 11 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,mixed>
*/
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
Expand Down
6 changes: 4 additions & 2 deletions src/URLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 16 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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()
{
Expand Down Expand Up @@ -163,6 +174,7 @@ function testPost()
/**
* @throws ClientException
* @throws FilesystemException
* @throws \HTTP\Client\Exception
*/
function testStore()
{
Expand Down

0 comments on commit 6776748

Please sign in to comment.