From 195bf7d78c47cdd745a7ff20a662341eeb7589a5 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Fri, 23 Aug 2019 15:32:26 +0200 Subject: [PATCH] updating for Gotenberg 6.0.0 --- .github/CONTRIBUTING.md | 1 - .github/PULL_REQUEST_TEMPLATE.md | 4 +-- .travis.yml | 10 ++---- LICENSE | 2 +- Makefile | 16 +++++++++ composer.json | 9 ++--- docker-compose.yml | 4 +-- orbit.yml | 24 ------------- phpcs.xml.dist | 48 +++++++++++++++++++------- src/ChromeRequest.php | 56 ++++++++++--------------------- src/Client.php | 33 ++++++------------ src/ClientException.php | 7 ++-- src/Document.php | 13 ++----- src/DocumentFactory.php | 16 ++------- src/GotenbergRequestInterface.php | 5 ++- src/HTMLRequest.php | 10 ++---- src/MarkdownRequest.php | 8 ++--- src/MergeRequest.php | 10 +++--- src/OfficeRequest.php | 11 +++--- src/Request.php | 30 ++++++++++------- src/RequestException.php | 7 ++-- src/URLRequest.php | 10 ++---- tests/ClientTest.php | 49 +++++++++++++-------------- tests/DocumentFactoryTest.php | 8 +++-- 24 files changed, 173 insertions(+), 218 deletions(-) create mode 100644 Makefile delete mode 100644 orbit.yml diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 035bb58..96c8d03 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -9,7 +9,6 @@ find below useful information about how to contribute to the Gotenberg PHP clien 1. Fork this repository 2. Install Docker and Docker Compose. -3. Install the latest [Orbit](https://github.com/gulien/orbit) release ### Working with git diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index a2838dc..aeb0bf0 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -37,8 +37,8 @@ Fixes # **Checklist** - [ ] Have you followed the guidelines in our [CONTRIBUTING](CONTRIBUTING.md) guide? -- [ ] Have you lint your code locally prior to submission (`orbit run lint`)? +- [ ] Have you lint your code locally prior to submission (`make lint`)? - [ ] Have you written new tests for your core changes, as applicable? -- [ ] Have you successfully ran tests with your changes locally (`orbit run up ci`)? +- [ ] Have you successfully ran tests with your changes locally (`make tests`)? - [ ] I have squashed any insignificant commits - [ ] This change has comments for package types, values, functions, and non-obvious lines of code \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index e9369a9..61b853e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,12 +5,6 @@ language: generic services: - docker -before_install: - - wget -qO- https://github.com/gulien/orbit/releases/download/v3.0.1/orbit_Linux_x86_64.tar.gz | tar xvz -C . - - sudo mv ./orbit /usr/local/bin && chmod +x /usr/local/bin/orbit - -before_script: - - orbit run up - script: - - orbit run lint tests \ No newline at end of file + - make lint + - make tests \ No newline at end of file diff --git a/LICENSE b/LICENSE index 4188a9b..b756c5a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 TheCodingMachine +Copyright (c) 2019 TheCodingMachine Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..761b078 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +# start workspace. +workspace: + docker-compose run --rm php bash + +# run all linters. +lint: + docker-compose run --rm -T php composer install + docker-compose run --rm -T php composer run csfix + docker-compose run --rm -T php composer run cscheck + docker-compose run --rm -T php composer run phpstan + +# run all tests. +test: + docker-compose up -d gotenberg + docker-compose run --rm -T php composer run phpunit + docker-compose down \ No newline at end of file diff --git a/composer.json b/composer.json index b1e6b27..2efdd58 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,8 @@ "thecodingmachine/phpstan-safe-rule": "^0.1.0", "thecodingmachine/phpstan-strict-rules": "^0.10.7", "php-http/mock-client": "^1.0", - "php-http/guzzle6-adapter": "^1.1" + "php-http/guzzle6-adapter": "^1.1", + "doctrine/coding-standard": "^6.0" }, "autoload" : { "psr-4" : { @@ -51,14 +52,14 @@ } }, "scripts": { - "cs-fix": "phpcbf", - "cs-check": "phpcs", + "csfix": "phpcbf", + "cscheck": "phpcs", "phpstan": "phpstan analyse src -c phpstan.neon --level=7 --no-progress -vvv", "phpunit": "phpunit --configuration phpunit.xml.dist" }, "extra": { "branch-alias": { - "dev-master": "3.0.x-dev" + "dev-master": "6.0.x-dev" } } } diff --git a/docker-compose.yml b/docker-compose.yml index ef8000c..c396faf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ version: '3' services: php: - image: thecodingmachine/php:7.2-v2-cli + image: thecodingmachine/php:7.3-v2-cli container_name: php environment: - PHP_EXTENSION_XDEBUG=1 @@ -12,6 +12,6 @@ services: - ./:/usr/src/app:rw gotenberg: - image: thecodingmachine/gotenberg:5.0.0 + image: thecodingmachine/gotenberg:6.0.0 container_name: gotenberg restart: 'no' \ No newline at end of file diff --git a/orbit.yml b/orbit.yml deleted file mode 100644 index 6a9a89f..0000000 --- a/orbit.yml +++ /dev/null @@ -1,24 +0,0 @@ -tasks: - - - use: up - short: Starts the CI stack - run: - - docker-compose up -d gotenberg - - - use: down - short: Stops the CI stack - run: - - docker-compose down - - - use: lint - shorts: Runs linter inside a container - run: - - docker-compose run --rm -T php composer install - - docker-compose run --rm -T php composer run cs-fix - - docker-compose run --rm -T php composer run cs-check - - docker-compose run --rm -T php composer run phpstan - - - use: tests - short: Runs tests inside a container - run: - - docker-compose run --rm -T php composer run phpunit \ No newline at end of file diff --git a/phpcs.xml.dist b/phpcs.xml.dist index d9bb9d0..8dab83a 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,21 +1,45 @@ - - Expressive Skeleton coding standard - - - + + + + - - + + - + src + tests + + tests/dependencies/* + + + + + + + + + + + + + 0 + + + + + 0 + + + 0 + - + + - - + - + \ No newline at end of file diff --git a/src/ChromeRequest.php b/src/ChromeRequest.php index a1bac5e..b9da5f8 100644 --- a/src/ChromeRequest.php +++ b/src/ChromeRequest.php @@ -1,7 +1,11 @@ waitDelay)) { + if ($this->waitDelay !== null) { $values[self::WAIT_DELAY] = $this->waitDelay; } - if (!is_null($this->paperWidth)) { + if ($this->paperWidth !== null) { $values[self::PAPER_WIDTH] = $this->paperWidth; } - if (!is_null($this->paperHeight)) { + if ($this->paperHeight !== null) { $values[self::PAPER_HEIGHT] = $this->paperHeight; } - if (!is_null($this->marginTop)) { + if ($this->marginTop !== null) { $values[self::MARGIN_TOP] = $this->marginTop; } - if (!is_null($this->marginBottom)) { + if ($this->marginBottom !== null) { $values[self::MARGIN_BOTTOM] = $this->marginBottom; } - if (!is_null($this->marginLeft)) { + if ($this->marginLeft !== null) { $values[self::MARGIN_LEFT] = $this->marginLeft; } - if (!is_null($this->marginRight)) { + if ($this->marginRight !== null) { $values[self::MARGIN_RIGHT] = $this->marginRight; } $values[self::LANDSCAPE] = $this->landscape; + return $values; } @@ -80,18 +85,16 @@ public function getFormValues(): array public function getFormFiles(): array { $files = []; - if (!empty($this->header)) { + if (! empty($this->header)) { $files['header.html'] = $this->header; } - if (!empty($this->footer)) { + if (! empty($this->footer)) { $files['footer.html'] = $this->footer; } + return $files; } - /** - * @param float|null $waitDelay - */ public function setWaitDelay(?float $waitDelay): void { $this->waitDelay = $waitDelay; @@ -99,6 +102,7 @@ public function setWaitDelay(?float $waitDelay): void /** * @param float[] $paperSize + * * @throws RequestException */ public function setPaperSize(array $paperSize): void @@ -112,6 +116,7 @@ public function setPaperSize(array $paperSize): void /** * @param float[] $margins + * * @throws RequestException */ public function setMargins(array $margins): void @@ -125,73 +130,46 @@ public function setMargins(array $margins): void $this->marginRight = $margins[3]; } - /** - * @param Document|null $header - */ public function setHeader(?Document $header): void { $this->header = $header; } - /** - * @param Document|null $footer - */ public function setFooter(?Document $footer): void { $this->footer = $footer; } - /** - * @param float|null $paperWidth - */ public function setPaperWidth(?float $paperWidth): void { $this->paperWidth = $paperWidth; } - /** - * @param float|null $paperHeight - */ public function setPaperHeight(?float $paperHeight): void { $this->paperHeight = $paperHeight; } - /** - * @param float|null $marginTop - */ public function setMarginTop(?float $marginTop): void { $this->marginTop = $marginTop; } - /** - * @param float|null $marginBottom - */ public function setMarginBottom(?float $marginBottom): void { $this->marginBottom = $marginBottom; } - /** - * @param float|null $marginLeft - */ public function setMarginLeft(?float $marginLeft): void { $this->marginLeft = $marginLeft; } - /** - * @param float|null $marginRight - */ public function setMarginRight(?float $marginRight): void { $this->marginRight = $marginRight; } - /** - * @param bool $landscape - */ public function setLandscape(bool $landscape): void { $this->landscape = $landscape; diff --git a/src/Client.php b/src/Client.php index da69755..7681ec1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1,7 +1,10 @@ apiURL = $apiURL; $this->client = $client ?: HttpClientDiscovery::find(); @@ -35,10 +33,8 @@ public function __construct(string $apiURL, HttpClient $client = null) /** * Sends the given documents to the API and returns the response. * - * @param GotenbergRequestInterface $request - * @return ResponseInterface * @throws ClientException - * @throws \Exception + * @throws Exception */ public function post(GotenbergRequestInterface $request): ResponseInterface { @@ -48,10 +44,8 @@ public function post(GotenbergRequestInterface $request): ResponseInterface /** * Sends the given documents to the API, stores the resulting PDF in the given destination. * - * @param GotenbergRequestInterface $request - * @param string $destination * @throws ClientException - * @throws \Exception + * @throws Exception * @throws FilesystemException */ public function store(GotenbergRequestInterface $request, string $destination): void @@ -59,14 +53,10 @@ public function store(GotenbergRequestInterface $request, string $destination): $response = $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request))); $fileStream = $response->getBody(); $fp = fopen($destination, 'w'); - fwrite($fp, $fileStream); + fwrite($fp, $fileStream->getContents()); fclose($fp); } - /** - * @param GotenbergRequestInterface $request - * @return RequestInterface - */ private function makeMultipartFormDataRequest(GotenbergRequestInterface $request): RequestInterface { $multipartData = []; @@ -84,11 +74,12 @@ private function makeMultipartFormDataRequest(GotenbergRequestInterface $request $multipartData[] = [ 'name' => 'files', 'filename' => $filename, - 'contents' => $document->getFileStream() + 'contents' => $document->getFileStream(), ]; } $body = new MultipartStream($multipartData); $messageFactory = MessageFactoryDiscovery::find(); + return $messageFactory ->createRequest('POST', $this->apiURL . $request->getPostURL()) ->withHeader('Content-Type', 'multipart/form-data; boundary="' . $body->getBoundary() . '"') @@ -96,8 +87,6 @@ private function makeMultipartFormDataRequest(GotenbergRequestInterface $request } /** - * @param ResponseInterface $response - * @return ResponseInterface * @throws ClientException */ private function handleResponse(ResponseInterface $response): ResponseInterface @@ -106,7 +95,7 @@ private function handleResponse(ResponseInterface $response): ResponseInterface case 200: return $response; default: - throw new ClientException($response->getBody(), $response->getStatusCode()); + throw new ClientException($response->getBody()->getContents(), $response->getStatusCode()); } } } diff --git a/src/ClientException.php b/src/ClientException.php index 44a7bec..aee5edf 100644 --- a/src/ClientException.php +++ b/src/ClientException.php @@ -1,8 +1,11 @@ fileName = $fileName; $this->fileStream = $fileStream; } - /** - * @return string - */ public function getFileName(): string { return $this->fileName; } - /** - * @return StreamInterface - */ public function getFileStream(): StreamInterface { return $this->fileStream; diff --git a/src/DocumentFactory.php b/src/DocumentFactory.php index b78bfa6..85f99f0 100644 --- a/src/DocumentFactory.php +++ b/src/DocumentFactory.php @@ -1,5 +1,7 @@ index = $index; $this->assets = []; } - /** - * @return string - */ public function getPostURL(): string { return '/convert/html'; @@ -38,6 +33,7 @@ public function getFormFiles(): array foreach ($this->assets as $asset) { $files[$asset->getFileName()] = $asset; } + return $files; } diff --git a/src/MarkdownRequest.php b/src/MarkdownRequest.php index 6b6f03d..4808978 100644 --- a/src/MarkdownRequest.php +++ b/src/MarkdownRequest.php @@ -1,5 +1,7 @@ markdowns = $markdowns; } - /** - * @return string - */ public function getPostURL(): string { return '/convert/markdown'; @@ -35,6 +32,7 @@ public function getFormFiles(): array foreach ($this->markdowns as $markdown) { $files[$markdown->getFileName()] = $markdown; } + return $files; } } diff --git a/src/MergeRequest.php b/src/MergeRequest.php index 9a43f96..89e00a8 100644 --- a/src/MergeRequest.php +++ b/src/MergeRequest.php @@ -1,5 +1,7 @@ files = $files; } - - /** - * @return string - */ public function getPostURL(): string { - return '/convert/merge'; + return '/merge'; } /** @@ -34,6 +31,7 @@ public function getFormFiles(): array foreach ($this->files as $file) { $files[$file->getFileName()] = $file; } + return $files; } } diff --git a/src/OfficeRequest.php b/src/OfficeRequest.php index 5a07d92..d319c73 100644 --- a/src/OfficeRequest.php +++ b/src/OfficeRequest.php @@ -1,5 +1,7 @@ files = $files; } - /** - * @return string - */ public function getPostURL(): string { return '/convert/office'; @@ -36,6 +34,7 @@ public function getFormValues(): array { $values = parent::getFormValues(); $values[self::LANDSCAPE] = $this->landscape; + return $values; } @@ -48,12 +47,10 @@ public function getFormFiles(): array foreach ($this->files as $file) { $files[$file->getFileName()] = $file; } + return $files; } - /** - * @param bool $landscape - */ public function setLandscape(bool $landscape): void { $this->landscape = $landscape; diff --git a/src/Request.php b/src/Request.php index 3bae302..01031a3 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1,5 +1,7 @@ */ public function getFormValues(): array { $values = []; - if (!empty($this->resultFilename)) { + if (! empty($this->resultFilename)) { $values[self::RESULT_FILENAME] = $this->resultFilename; } - if (!is_null($this->waitTimeout)) { + if ($this->waitTimeout !== null) { $values[self::WAIT_TIMEOUT] = $this->waitTimeout; } - if (!empty($this->webhookURL)) { + if (! empty($this->webhookURL)) { $values[self::WEBHOOK_URL] = $this->webhookURL; } + if (! empty($this->webhookURLTimeout)) { + $values[self::WEBHOOK_URL_TIMEOUT] = $this->webhookURLTimeout; + } + 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 - */ public function setWebhookURL(?string $webhookURL): void { $this->webhookURL = $webhookURL; } + + public function setWebhookURLTimeout(?float $webhookURLTimeout): void + { + $this->webhookURLTimeout = $webhookURLTimeout; + } } diff --git a/src/RequestException.php b/src/RequestException.php index 964274b..b54f82b 100644 --- a/src/RequestException.php +++ b/src/RequestException.php @@ -1,8 +1,11 @@ URL = $URL; } - /** - * @return string - */ public function getPostURL(): string { return '/convert/url'; @@ -33,6 +28,7 @@ public function getFormValues(): array { $values = parent::getFormValues(); $values[self::REMOTE_URL] = $this->URL; + return $values; } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 21aae94..a28a945 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -1,13 +1,16 @@ HTMLRequest = $this->createHTMLRequest(); $this->URLRequest = $this->createURLRequest(); @@ -37,7 +40,6 @@ public function setUp() } /** - * @return HTMLRequest * @throws RequestException */ private function createHTMLRequest(): HTMLRequest @@ -52,18 +54,18 @@ private function createHTMLRequest(): HTMLRequest ]; $request = new HTMLRequest($index); $request->setResultFilename('foo.pdf'); - $request->setWaitTimeout(5); - $request->setWaitDelay(1); + $request->setWaitTimeout(5.0); + $request->setWaitDelay(1.0); $request->setHeader($header); $request->setFooter($footer); $request->setAssets($assets); $request->setPaperSize(Request::A4); $request->setMargins(Request::NO_MARGINS); + return $request; } /** - * @return URLRequest * @throws RequestException */ private function createURLRequest(): URLRequest @@ -72,17 +74,17 @@ private function createURLRequest(): URLRequest $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->setWaitTimeout(5.0); + $request->setWaitDelay(1.0); $request->setHeader($header); $request->setFooter($footer); $request->setPaperSize(Request::A4); $request->setMargins(Request::NO_MARGINS); + return $request; } /** - * @return MarkdownRequest * @throws RequestException */ public function createMarkdownRequest(): MarkdownRequest @@ -102,19 +104,17 @@ public function createMarkdownRequest(): MarkdownRequest ]; $request = new MarkdownRequest($index, $markdowns); $request->setResultFilename('foo.pdf'); - $request->setWaitTimeout(5); - $request->setWaitDelay(1); + $request->setWaitTimeout(5.0); + $request->setWaitDelay(1.0); $request->setHeader($header); $request->setFooter($footer); $request->setAssets($assets); $request->setPaperSize(Request::A4); $request->setMargins(Request::NO_MARGINS); + return $request; } - /** - * @return OfficeRequest - */ public function createOfficeRequest(): OfficeRequest { $files = [ @@ -122,14 +122,12 @@ public function createOfficeRequest(): OfficeRequest ]; $request = new OfficeRequest($files); $request->setResultFilename('foo.pdf'); - $request->setWaitTimeout(5); + $request->setWaitTimeout(5.0); $request->setLandscape(false); + return $request; } - /** - * @return MergeRequest - */ public function createMergeRequest(): MergeRequest { $files = [ @@ -138,15 +136,16 @@ public function createMergeRequest(): MergeRequest ]; $request = new MergeRequest($files); $request->setResultFilename('foo.pdf'); - $request->setWaitTimeout(5); + $request->setWaitTimeout(5.0); + return $request; } /** * @throws ClientException - * @throws \HTTP\Client\Exception + * @throws Exception */ - function testPost() + public function testPost(): void { $client = new Client(self::API_URL, new \Http\Adapter\Guzzle6\Client()); // case 1: HTML. @@ -174,9 +173,9 @@ function testPost() /** * @throws ClientException * @throws FilesystemException - * @throws \HTTP\Client\Exception + * @throws Exception */ - function testStore() + public function testStore(): void { $client = new Client(self::API_URL); // case 1: HTML. @@ -200,4 +199,4 @@ function testStore() $client->store($this->mergeRequest, $filePath); $this->assertFileExists($filePath); } -} \ No newline at end of file +} diff --git a/tests/DocumentFactoryTest.php b/tests/DocumentFactoryTest.php index fddc342..69248c8 100644 --- a/tests/DocumentFactoryTest.php +++ b/tests/DocumentFactoryTest.php @@ -1,13 +1,15 @@ foo'); $this->assertNotEmpty($document->getFileStream()); } -} \ No newline at end of file +}