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

Commit cd49686

Browse files
author
Julien Neuhart
committed
2 parents 4122a4c + 20b4bf1 commit cd49686

File tree

7 files changed

+61
-59
lines changed

7 files changed

+61
-59
lines changed

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace YourAwesomeNamespace;
6565

6666
use TheCodingMachine\Gotenberg\Client;
6767
use TheCodingMachine\Gotenberg\ClientException;
68-
use TheCodingMachine\Gotenberg\Document;
68+
use TheCodingMachine\Gotenberg\DocumentFactory;
6969

7070
use GuzzleHttp\Psr7\LazyOpenStream;
7171

@@ -78,15 +78,13 @@ class YourAwesomeClass {
7878
$client = new Client('gotenberg:3000');
7979

8080
# let's instantiate some documents you wish to convert.
81-
$yourOfficeDocument = new Document('file.docx');
82-
$yourOfficeDocument->feedFromPath('path/to/file');
83-
84-
$yourHTMLDocument = new Document('file.html');
85-
$yourHTMLDocument->feedFromStream(new LazyOpenStream('path/to/file', 'r'));
81+
$yourOfficeDocument = DocumentFactory::makeFromPath('file.docx', '/path/to/file');
82+
$yourHTMLDocument = DocumentFactory::makeFromStream('file.html', new LazyOpenStream('path/to/file', 'r'));
8683

8784
# now let's send those documents!
8885
try {
8986
# store method allows you to... store the resulting PDF in a particular folder.
87+
# this method also returns the resulting PDF path.
9088
$filePath = $client->store([
9189
$yourOfficeDocument,
9290
$yourHTMLDocument

src/Document.php

+2-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace TheCodingMachine\Gotenberg;
44

5-
use GuzzleHttp\Psr7\LazyOpenStream;
65
use Psr\Http\Message\StreamInterface;
76

87
class Document
@@ -16,25 +15,11 @@ class Document
1615
/**
1716
* Document constructor.
1817
* @param string $fileName
19-
*/
20-
public function __construct(string $fileName)
21-
{
22-
$this->fileName = $fileName;
23-
}
24-
25-
/**
26-
* @param string $filePath
27-
*/
28-
public function feedFromPath(string $filePath): void
29-
{
30-
$this->fileStream = new LazyOpenStream($filePath, 'r');
31-
}
32-
33-
/**
3418
* @param StreamInterface $fileStream
3519
*/
36-
public function feedFromStream(StreamInterface $fileStream): void
20+
public function __construct(string $fileName, StreamInterface $fileStream)
3721
{
22+
$this->fileName = $fileName;
3823
$this->fileStream = $fileStream;
3924
}
4025

src/DocumentFactory.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace TheCodingMachine\Gotenberg;
4+
5+
use GuzzleHttp\Psr7\LazyOpenStream;
6+
use Psr\Http\Message\StreamInterface;
7+
8+
class DocumentFactory
9+
{
10+
/**
11+
* @param string $fileName
12+
* @param string $filePath
13+
* @return Document
14+
*/
15+
public static function makeFromPath(string $fileName, string $filePath): Document
16+
{
17+
return new Document($fileName, new LazyOpenStream($filePath, 'r'));
18+
}
19+
20+
/**
21+
* @param string $fileName
22+
* @param StreamInterface $fileStream
23+
* @return Document
24+
*/
25+
public static function makeFromStream(string $fileName, StreamInterface $fileStream): Document
26+
{
27+
return new Document($fileName, $fileStream);
28+
}
29+
}

tests/ClientTest.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,11 @@ class ClientTest extends TestCase
2525

2626
public function setUp()
2727
{
28-
$this->officeDocument = $this->makeDocument('file.docx');
29-
$this->htmlDocument = $this->makeDocument('file.html');
30-
$this->markdownDocument = $this->makeDocument('file.md');
31-
$this->pdfDocument = $this->makeDocument('file.pdf');
32-
$this->noExtensionDocument = $this->makeDocument('file');
33-
}
34-
35-
private function makeDocument(string $fileName): Document
36-
{
37-
$document = new Document($fileName);
38-
$document->feedFromPath(__DIR__ . '/assets/' . $fileName);
39-
40-
return $document;
28+
$this->officeDocument = DocumentFactory::makeFromPath('file.docx', __DIR__ . '/assets/file.docx');
29+
$this->htmlDocument = DocumentFactory::makeFromPath('file.html', __DIR__ . '/assets/file.html');
30+
$this->markdownDocument = DocumentFactory::makeFromPath('file.md', __DIR__ . '/assets/file.md');
31+
$this->pdfDocument = DocumentFactory::makeFromPath('file.pdf', __DIR__ . '/assets/file.pdf');
32+
$this->noExtensionDocument = DocumentFactory::makeFromPath('file', __DIR__ . '/assets/file.pdf');
4133
}
4234

4335
function testForward()
@@ -63,7 +55,7 @@ function testForward()
6355

6456
function testStore()
6557
{
66-
$client = new Client(self::API_URL, new \Http\Adapter\Guzzle6\Client());
58+
$client = new Client(self::API_URL);
6759
$storingPath = __DIR__ . '/store';
6860

6961
// case 1: sends a single document.

tests/DocumentFactoryTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace TheCodingMachine\Gotenberg;
4+
5+
use GuzzleHttp\Psr7\LazyOpenStream;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class DocumentFactoryTest extends TestCase
9+
{
10+
function testMake()
11+
{
12+
// case 1: uses a file path.
13+
$document = DocumentFactory::makeFromPath('file.pdf', __DIR__ . '/assets/file.pdf');
14+
$this->assertNotEmpty($document->getFileStream());
15+
16+
// case 2: uses a stream.
17+
$document = DocumentFactory::makeFromStream('file.pdf', new LazyOpenStream(__DIR__ . '/assets/file.pdf', 'r'));
18+
$this->assertNotEmpty($document->getFileStream());
19+
}
20+
}

tests/DocumentTest.php

-22
This file was deleted.

tests/assets/file

-6.83 KB
Binary file not shown.

0 commit comments

Comments
 (0)