From 547129519cc1bb5ee0ea92a82d1d1a5d0b06c11f Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Thu, 19 Apr 2018 17:16:42 +0200 Subject: [PATCH] now using DocumentFactory to simplify Document instantiation --- README.md | 10 ++++------ src/Document.php | 19 ++----------------- src/DocumentFactory.php | 29 +++++++++++++++++++++++++++++ tests/ClientTest.php | 20 ++++++-------------- tests/DocumentFactoryTest.php | 20 ++++++++++++++++++++ tests/DocumentTest.php | 22 ---------------------- tests/assets/file | Bin 6999 -> 0 bytes 7 files changed, 61 insertions(+), 59 deletions(-) create mode 100644 src/DocumentFactory.php create mode 100644 tests/DocumentFactoryTest.php delete mode 100644 tests/DocumentTest.php delete mode 100644 tests/assets/file diff --git a/README.md b/README.md index 6e56ee9..581263d 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ namespace YourAwesomeNamespace; use TheCodingMachine\Gotenberg\Client; use TheCodingMachine\Gotenberg\ClientException; -use TheCodingMachine\Gotenberg\Document; +use TheCodingMachine\Gotenberg\DocumentFactory; use GuzzleHttp\Psr7\LazyOpenStream; @@ -78,15 +78,13 @@ class YourAwesomeClass { $client = new Client('gotenberg:3000'); # let's instantiate some documents you wish to convert. - $yourOfficeDocument = new Document('file.docx'); - $yourOfficeDocument->feedFromPath('path/to/file'); - - $yourHTMLDocument = new Document('file.html'); - $yourHTMLDocument->feedFromStream(new LazyOpenStream('path/to/file', 'r')); + $yourOfficeDocument = DocumentFactory::makeFromPath('file.docx', '/path/to/file'); + $yourHTMLDocument = DocumentFactory::makeFromStream('file.html', new LazyOpenStream('path/to/file', 'r')); # now let's send those documents! try { # store method allows you to... store the resulting PDF in a particular folder. + # this method also returns the resulting PDF path. $filePath = $client->store([ $yourOfficeDocument, $yourHTMLDocument diff --git a/src/Document.php b/src/Document.php index e4177a8..d79f336 100644 --- a/src/Document.php +++ b/src/Document.php @@ -2,7 +2,6 @@ namespace TheCodingMachine\Gotenberg; -use GuzzleHttp\Psr7\LazyOpenStream; use Psr\Http\Message\StreamInterface; class Document @@ -16,25 +15,11 @@ class Document /** * Document constructor. * @param string $fileName - */ - public function __construct(string $fileName) - { - $this->fileName = $fileName; - } - - /** - * @param string $filePath - */ - public function feedFromPath(string $filePath): void - { - $this->fileStream = new LazyOpenStream($filePath, 'r'); - } - - /** * @param StreamInterface $fileStream */ - public function feedFromStream(StreamInterface $fileStream): void + public function __construct(string $fileName, StreamInterface $fileStream) { + $this->fileName = $fileName; $this->fileStream = $fileStream; } diff --git a/src/DocumentFactory.php b/src/DocumentFactory.php new file mode 100644 index 0000000..8cea2ef --- /dev/null +++ b/src/DocumentFactory.php @@ -0,0 +1,29 @@ +officeDocument = $this->makeDocument('file.docx'); - $this->htmlDocument = $this->makeDocument('file.html'); - $this->markdownDocument = $this->makeDocument('file.md'); - $this->pdfDocument = $this->makeDocument('file.pdf'); - $this->noExtensionDocument = $this->makeDocument('file'); - } - - private function makeDocument(string $fileName): Document - { - $document = new Document($fileName); - $document->feedFromPath(__DIR__ . '/assets/' . $fileName); - - return $document; + $this->officeDocument = DocumentFactory::makeFromPath('file.docx', __DIR__ . '/assets/file.docx'); + $this->htmlDocument = DocumentFactory::makeFromPath('file.html', __DIR__ . '/assets/file.html'); + $this->markdownDocument = DocumentFactory::makeFromPath('file.md', __DIR__ . '/assets/file.md'); + $this->pdfDocument = DocumentFactory::makeFromPath('file.pdf', __DIR__ . '/assets/file.pdf'); + $this->noExtensionDocument = DocumentFactory::makeFromPath('file', __DIR__ . '/assets/file.pdf'); } function testForward() @@ -63,7 +55,7 @@ function testForward() function testStore() { - $client = new Client(self::API_URL, new \Http\Adapter\Guzzle6\Client()); + $client = new Client(self::API_URL); $storingPath = __DIR__ . '/store'; // case 1: sends a single document. diff --git a/tests/DocumentFactoryTest.php b/tests/DocumentFactoryTest.php new file mode 100644 index 0000000..af3d253 --- /dev/null +++ b/tests/DocumentFactoryTest.php @@ -0,0 +1,20 @@ +assertNotEmpty($document->getFileStream()); + + // case 2: uses a stream. + $document = DocumentFactory::makeFromStream('file.pdf', new LazyOpenStream(__DIR__ . '/assets/file.pdf', 'r')); + $this->assertNotEmpty($document->getFileStream()); + } +} \ No newline at end of file diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php deleted file mode 100644 index 5abac54..0000000 --- a/tests/DocumentTest.php +++ /dev/null @@ -1,22 +0,0 @@ -feedFromPath(__DIR__ . '/assets/' . $document->getFileName()); - $this->assertNotEmpty($document->getFileStream()); - - // case 2: uses a stream. - $document = new Document('file.pdf'); - $document->feedFromStream(new LazyOpenStream(__DIR__ . '/assets/' . $document->getFileName(), 'r')); - $this->assertNotEmpty($document->getFileStream()); - } -} \ No newline at end of file diff --git a/tests/assets/file b/tests/assets/file deleted file mode 100644 index 191b31222dd8bdaaf3d9f566218a836e4c005c43..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6999 zcmb7}c|4Te+rTB+msXOc+ZM9TZpOY0S$@b`*$0DR%rG-#7b;JYT`Hv_WX%#q6v|eV zQsGgQHCYp~_uey7&r|R3^M2m@^O-sKxz2T+bDitD&i%*tNFBxMD?k+yoKOG)kX^hu z)zmnZtOztB9+27iF3YK;OTpu4WD32OaK#vNs$T4 z1Oxx!;132`9i@=n{9W;zX6B$Wz=Q;cl9l8aNL;6@Y^Z7XL6n{9&LMNIPOl16SfyveX7> z6o2}Q2>?-qz&Mp$aR5{i0&Y`#v(+}V1l(~%DnkcH^#+to$t3(Qe~>?>w*!}_(eM-! zpoGN-5M1#VM|2#voJRfO1Kn4;#}qt?_QPc*T{4LVcBufGu>~ayJeBNEaRv7Tg=|W+ zNhC5j4yEVDf>X%^??%AskOKib`ZyAU1kebW!$0=Uu-i(aE}2NCSo-2z@c=!iM|CZI z0lN3#MD*8RZZrBGTY@7LTo8RqrJrGAm>;pk0rb!^Lj7~^;KX!QeQ+lXEq!G``KQux zGZ!zsD`Nmu`V~UPcIaN(0Y4)~55bn>|G|?yXr2IO(^q{05eze+q)!Ahh{d~--SG6e zs5G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