| Total Complexity | 7 | 
| Total Lines | 99 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
| 1 | <?php | ||
| 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') | ||
| 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 | ||
| 97 | } | ||
| 98 | |||
| 99 | /** | ||
| 100 | * Retrieve the PDF's AWS S3 url. | ||
| 101 | * | ||
| 102 | * @return string|null | ||
| 103 | */ | ||
| 104 | public function getUrl(): ?string | ||
| 107 | } | ||
| 108 | } | ||
| 109 |