Passed
Push — master ( e861d8...c2b576 )
by Stephen
46s queued 11s
created

Renderer::handleJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\ViewExport\Pdf\Utils;
4
5
use Dompdf\Dompdf;
6
use Dompdf\Exception;
7
use Dompdf\Options;
8
use Illuminate\Support\Facades\Bus;
9
use Sfneal\Helpers\Strings\StringHelpers;
10
use Sfneal\Queueables\AbstractJob;
11
12
class Renderer extends AbstractJob
13
{
14
    /**
15
     * @var string PDF content (either a rendered View or HTML string)
16
     */
17
    private $content;
18
19
    /**
20
     * @var string|null AWS S3 path to upload PDF to after render (if provided)
21
     */
22
    private $uploadPath;
23
24
    /**
25
     * @var Options
26
     */
27
    public $options;
28
29
    /**
30
     * @var Metadata
31
     */
32
    public $metadata;
33
34
    /**
35
     * @var Dompdf
36
     */
37
    private $pdf;
38
39
    /**
40
     * PdfExporter constructor.
41
     *
42
     * - $content can be a View or HTML file contents
43
     *
44
     * @param string $content
45
     * @param string|null $uploadPath
46
     */
47
    public function __construct(string $content, string $uploadPath = null)
48
    {
49
        // Content of the PDF
50
        $this->content = $content;
51
52
        // Upload PDF after rendering (defaults to false)
53
        $this->uploadPath = $uploadPath;
54
55
        // Declare PDF options (use DefaultOptions) if none provided
56
        $this->options = new DefaultOptions();
57
58
        // Instantiate Metadata
59
        $this->metadata = new Metadata();
60
    }
61
62
    /**
63
     * Dispatch this renderer instance to the Job queue without having to construct it statically.
64
     *
65
     * @return mixed
66
     */
67
    public function handleJob()
68
    {
69
        return Bus::dispatch($this);
70
    }
71
72
    /**
73
     * Load PDF content to the Dompdf instance and render the output.
74
     *
75
     *  - storing output in a property avoids potentially calling expensive 'output()' method multiple times
76
     *
77
     * @return Exporter
78
     * @throws Exception
79
     */
80
    public function handle(): Exporter
81
    {
82
        // Instantiate dompdf
83
        $this->pdf = new Dompdf($this->options);
84
85
        // Add metadata
86
        $this->applyMetadata();
87
88
        // Load content
89
        $this->loadContent();
90
91
        // Render the PDF
92
        $this->pdf->render();
93
94
        // Initialize the PdfExporter
95
        $exporter = new Exporter($this->pdf);
96
97
        // Upload after rendering if an upload path was provided
98
        if ($this->uploadPath) {
99
            $exporter->upload($this->uploadPath);
100
        }
101
102
        // Return a PdfExporter
103
        return $exporter;
104
    }
105
106
    /**
107
     * Add Metadata to the PDF.
108
     *
109
     * @return bool
110
     */
111
    private function applyMetadata(): bool
112
    {
113
        // Add Metadata if the array isn't empty
114
        if ($hasMetadata = ! empty($this->metadata->get())) {
115
            foreach ($this->metadata->get() as $key => $value) {
116
                $this->pdf->add_info($key, $value);
117
            }
118
        }
119
120
        return $hasMetadata;
121
    }
122
123
    /**
124
     * Load content into the Dompdf instance.
125
     *
126
     * @throws Exception
127
     */
128
    private function loadContent(): void
129
    {
130
        // Create local HTML file path
131
        $localHTML = StringHelpers::joinPaths($this->options->getRootDir(), uniqid().'.html');
132
133
        // Store View (or HTML) as HTML file within Dompdf root
134
        touch($localHTML);
135
        file_put_contents($localHTML, $this->content);
136
137
        // Load HTML
138
        $this->pdf->loadHtmlFile($localHTML);
139
140
        // Remove temp HTML file
141
        unlink($localHTML);
142
    }
143
}
144