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

Commit

Permalink
now using DocumentFactory to simplify Document instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Apr 19, 2018
1 parent 6067edf commit 5471295
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 59 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
19 changes: 2 additions & 17 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace TheCodingMachine\Gotenberg;

use GuzzleHttp\Psr7\LazyOpenStream;
use Psr\Http\Message\StreamInterface;

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

Expand Down
29 changes: 29 additions & 0 deletions src/DocumentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace TheCodingMachine\Gotenberg;

use GuzzleHttp\Psr7\LazyOpenStream;
use Psr\Http\Message\StreamInterface;

class DocumentFactory
{
/**
* @param string $fileName
* @param string $filePath
* @return Document
*/
public static function makeFromPath(string $fileName, string $filePath): Document
{
return new Document($fileName, new LazyOpenStream($filePath, 'r'));
}

/**
* @param string $fileName
* @param StreamInterface $fileStream
* @return Document
*/
public static function makeFromStream(string $fileName, StreamInterface $fileStream): Document
{
return new Document($fileName, $fileStream);
}
}
20 changes: 6 additions & 14 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,11 @@ class ClientTest extends TestCase

public function setUp()
{
$this->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()
Expand All @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions tests/DocumentFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace TheCodingMachine\Gotenberg;

use GuzzleHttp\Psr7\LazyOpenStream;
use PHPUnit\Framework\TestCase;

class DocumentFactoryTest extends TestCase
{
function testMake()
{
// case 1: uses a file path.
$document = DocumentFactory::makeFromPath('file.pdf', __DIR__ . '/assets/file.pdf');
$this->assertNotEmpty($document->getFileStream());

// case 2: uses a stream.
$document = DocumentFactory::makeFromStream('file.pdf', new LazyOpenStream(__DIR__ . '/assets/file.pdf', 'r'));
$this->assertNotEmpty($document->getFileStream());
}
}
22 changes: 0 additions & 22 deletions tests/DocumentTest.php

This file was deleted.

Binary file removed tests/assets/file
Binary file not shown.

0 comments on commit 5471295

Please sign in to comment.