Passed
Push — master ( 9eb67a...b09c8e )
by Stephen
52s queued 12s
created

Exporter::getOutput()   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 Sfneal\Helpers\Aws\S3\S3;
7
8
class Exporter
9
{
10
    /**
11
     * @var Dompdf
12
     */
13
    private $pdf;
14
15
    /**
16
     * @var string|null AWS S3 file path
17
     */
18
    private $path;
19
20
    /**
21
     * @var string|null AWS S3 file URL
22
     */
23
    private $url;
24
25
    /**
26
     * @var string|null
27
     */
28
    private $output;
29
30
    /**
31
     * PdfExporter constructor.
32
     *
33
     * - $content can be a View or HTML file contents
34
     *
35
     * @param Dompdf $pdf
36
     */
37
    public function __construct(Dompdf $pdf)
38
    {
39
        $this->pdf = $pdf;
40
41
        // Store output to a property to avoid retrieving twice
42
        $this->output = $this->pdf->output();
43
    }
44
45
    /**
46
     * Upload a rendered PDF to an AWS S3 file store.
47
     *
48
     * @param string $path
49
     * @return $this
50
     */
51
    public function upload(string $path): self
52
    {
53
        $this->path = $path;
54
        $this->url = (new S3($path))->upload_raw($this->getOutput());
55
56
        return $this;
57
    }
58
59
    /**
60
     * View the PDF in the clients browser.
61
     *
62
     * @param string $filename
63
     */
64
    public function view(string $filename = 'output.pdf')
65
    {
66
        $this->pdf->stream($filename, ['Attachment' => false]);
67
    }
68
69
    /**
70
     * Download the PDF using the clients browser.
71
     *
72
     * @param string $filename
73
     */
74
    public function download(string $filename = 'output.pdf')
75
    {
76
        $this->pdf->stream($filename, ['Attachment' => true]);
77
    }
78
79
    /**
80
     * Retrieve the PDF's output.
81
     *
82
     * @return string
83
     */
84
    public function getOutput(): string
85
    {
86
        return $this->output;
87
    }
88
89
    /**
90
     * Retrieve the PDF's AWS S3 path.
91
     *
92
     * @return string|null
93
     */
94
    public function getPath(): ?string
95
    {
96
        return $this->path;
97
    }
98
99
    /**
100
     * Retrieve the PDF's AWS S3 url.
101
     *
102
     * @return string|null
103
     */
104
    public function getUrl(): ?string
105
    {
106
        return $this->url;
107
    }
108
}
109