Skip to content

Commit e2b6969

Browse files
committed
replaced logic for attachment file with a separate pdf document
Signed-off-by: Christoph Massmann <[email protected]>
1 parent f6489fc commit e2b6969

File tree

5 files changed

+58
-86
lines changed

5 files changed

+58
-86
lines changed

src/Model/Document.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,15 @@
2121

2222
class Document implements DocumentInterface
2323
{
24-
/**
25-
* @var string
26-
*/
24+
/** @var string */
2725
private $htmlContents;
2826

29-
/**
30-
* @var string
31-
*/
27+
/** @var string */
3228
private $pdfBackgroundFile = '';
3329

34-
/**
35-
* @var string
36-
*/
30+
/** @var string */
3731
private $pdfBackgroundFileForFirstPage = '';
3832

39-
/**
40-
* @var string
41-
*/
42-
private $pdfAttachmentFile = '';
43-
4433
/**
4534
* @param string $htmlContents
4635
*
@@ -94,23 +83,6 @@ public function setPdfBackgroundFileForFirstPage($pdfFile)
9483
$this->pdfBackgroundFileForFirstPage = $pdfFile;
9584
}
9685

97-
/**
98-
* @return string
99-
*/
100-
public function getPdfAttachmentFile()
101-
{
102-
return $this->pdfAttachmentFile;
103-
}
104-
105-
/**
106-
* @param string $pdfFile
107-
* @return void
108-
*/
109-
public function setPdfAttachmentFile($pdfFile)
110-
{
111-
$this->pdfAttachmentFile = $pdfFile;
112-
}
113-
11486
/**
11587
* @return string
11688
*/

src/Model/DocumentInterface.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22
/**
3-
* Pdf document interface class
4-
*
53
* @section LICENSE
64
* This file is created by vianetz <[email protected]>.
75
* The code is distributed under the GPL license.
@@ -20,7 +18,7 @@
2018
namespace Vianetz\Pdf\Model;
2119

2220
/**
23-
* Interface DocumentInterface
21+
* @todo rename to HtmlDocumentInterface
2422
*/
2523
interface DocumentInterface
2624
{
@@ -53,17 +51,6 @@ public function getPdfBackgroundFileForFirstPage();
5351
*/
5452
public function setPdfBackgroundFileForFirstPage($pdfFile);
5553

56-
/**
57-
* @return string
58-
*/
59-
public function getPdfAttachmentFile();
60-
61-
/**
62-
* @param string $pdfFile
63-
* @return void
64-
*/
65-
public function setPdfAttachmentFile($pdfFile);
66-
6754
/**
6855
* @return string
6956
*/

src/Model/Pdf.php

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ class Pdf implements PdfInterface
3636
*/
3737
private $generator;
3838

39-
/**
40-
* @var \Vianetz\Pdf\Model\PdfMerge
41-
*/
39+
/** @var \Vianetz\Pdf\Model\PdfMerge */
4240
private $pdfMerge;
4341

4442
/**
@@ -51,18 +49,14 @@ class Pdf implements PdfInterface
5149
/**
5250
* Initialize empty array for PDF documents to print.
5351
*
54-
* @var array<\Vianetz\Pdf\Model\DocumentInterface>
52+
* @var array<\Vianetz\Pdf\Model\DocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface>
5553
*/
56-
private $documents = array();
54+
private $documents = [];
5755

58-
/**
59-
* @var Config
60-
*/
56+
/** @var Config */
6157
protected $config;
6258

63-
/**
64-
* @var \Vianetz\Pdf\Model\EventManagerInterface
65-
*/
59+
/** @var \Vianetz\Pdf\Model\EventManagerInterface */
6660
protected $eventManager;
6761

6862
/**
@@ -108,10 +102,12 @@ final public function saveToFile($fileName)
108102
/**
109103
* Add a new document to generate.
110104
*
105+
* @param \Vianetz\Pdf\Model\DocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface
106+
*
111107
* @api
112108
* @return Pdf
113109
*/
114-
final public function addDocument(DocumentInterface $documentModel)
110+
final public function addDocument($documentModel)
115111
{
116112
$this->documents[] = $documentModel;
117113
// Reset cached pdf contents.
@@ -162,30 +158,31 @@ private function renderPdfContentsForAllDocuments()
162158
{
163159
$hasData = false;
164160
foreach ($this->documents as $documentInstance) {
165-
if (! $documentInstance instanceof DocumentInterface) {
166-
continue;
167-
}
168-
169161
$this->eventManager->dispatch('vianetz_pdf_document_render_before', ['document' => $documentInstance]);
170-
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_before', ['document' => $documentInstance]);
171162

172-
$pdfContents = $this->generator->renderPdfDocument($documentInstance);
173-
if (empty($pdfContents)) {
174-
continue;
175-
}
163+
if ($documentInstance instanceof DocumentInterface) {
164+
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_before', ['document' => $documentInstance]);
176165

177-
$hasData = true;
166+
$pdfContents = $this->generator->renderPdfDocument($documentInstance);
167+
if (empty($pdfContents)) {
168+
continue;
169+
}
178170

179-
$this->eventManager->dispatch('vianetz_pdf_document_render_after', ['document' => $documentInstance]);
180-
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_after', ['document' => $documentInstance]);
171+
$hasData = true;
172+
173+
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_after', ['document' => $documentInstance]);
181174

182-
$this->pdfMerge->mergePdfString($pdfContents, $documentInstance->getPdfBackgroundFile(), $documentInstance->getPdfBackgroundFileForFirstPage());
175+
$this->pdfMerge->mergePdfString($pdfContents, $documentInstance->getPdfBackgroundFile(), $documentInstance->getPdfBackgroundFileForFirstPage());
183176

184-
if (! empty($documentInstance->getPdfAttachmentFile())) {
185-
$this->pdfMerge->mergePdfFile($documentInstance->getPdfAttachmentFile());
177+
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_merge_after', [
178+
'merger' => $this->pdfMerge,
179+
'document' => $documentInstance,
180+
]);
181+
} elseif ($documentInstance instanceof PdfDocumentInterface) {
182+
$this->pdfMerge->mergePdfFile($documentInstance->getPdfFile());
186183
}
187184

188-
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_merge_after', ['merger' => $this->pdfMerge, 'document' => $documentInstance]);
185+
$this->eventManager->dispatch('vianetz_pdf_document_render_after', ['document' => $documentInstance]);
189186
}
190187

191188
if (! $hasData) {

src/Model/PdfDocument.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Vianetz\Pdf\Model;
4+
5+
final class PdfDocument implements PdfDocumentInterface
6+
{
7+
/** @var string */
8+
private $pdfFile;
9+
10+
public function getPdfFile()
11+
{
12+
return $this->pdfFile;
13+
}
14+
15+
public function setPdfFile($pdfFile)
16+
{
17+
$this->pdfFile = $pdfFile;
18+
}
19+
}

src/Test/DocumentTest.php renamed to src/Model/PdfDocumentInterface.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,18 @@
1515
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GENERAL PUBLIC LICENSE
1616
*/
1717

18-
namespace Vianetz\Pdf\Test;
18+
namespace Vianetz\Pdf\Model;
1919

20-
use PHPUnit\Framework\TestCase;
21-
use Vianetz\Pdf\Model\Document;
22-
23-
final class DocumentTest extends TestCase
20+
interface PdfDocumentInterface
2421
{
2522
/**
26-
* @return void
23+
* @return string
2724
*/
28-
public function testSetPdfAttachmentFile()
29-
{
30-
$document = new Document();
31-
$document->setPdfAttachmentFile('attachment1.pdf');
25+
public function getPdfFile();
3226

33-
$this->assertEquals('attachment1.pdf', $document->getPdfAttachmentFile());
34-
}
35-
}
27+
/**
28+
* @param string $pdfFile
29+
* @return void
30+
*/
31+
public function setPdfFile($pdfFile);
32+
}

0 commit comments

Comments
 (0)