1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\ViewExport\Support; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
6
|
|
|
use Sfneal\Helpers\Aws\S3\S3; |
7
|
|
|
|
8
|
|
|
class Exporter |
9
|
|
|
{ |
10
|
|
|
// todo: add ability to store locally |
11
|
|
|
// todo: fix docstrings to not be pdf specific |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string|null local file path |
15
|
|
|
*/ |
16
|
|
|
protected $localPath; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string|null AWS S3 file path |
20
|
|
|
*/ |
21
|
|
|
protected $uploadPath; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string|null AWS S3 file URL |
25
|
|
|
*/ |
26
|
|
|
protected $url; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
protected $output; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Upload a rendered PDF to an AWS S3 file store. |
35
|
|
|
* |
36
|
|
|
* @param string $path |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function upload(string $path): self |
40
|
|
|
{ |
41
|
|
|
$this->uploadPath = $path; |
42
|
|
|
|
43
|
|
|
// todo: add use of Storage |
44
|
|
|
$this->url = (new S3($path))->upload_raw($this->output()); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Store a rendered PDF on the local file system. |
51
|
|
|
* |
52
|
|
|
* @param string $path |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function store(string $path): self |
56
|
|
|
{ |
57
|
|
|
$this->localPath = $path; |
58
|
|
|
Storage::put($path, $this->output()); |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Retrieve the PDF's output. |
65
|
|
|
* |
66
|
|
|
* @return string|null |
67
|
|
|
*/ |
68
|
|
|
public function output(): ?string |
69
|
|
|
{ |
70
|
|
|
return $this->output; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Retrieve the PDF's AWS S3 path if available or the local file path. |
75
|
|
|
* |
76
|
|
|
* @return string|null |
77
|
|
|
*/ |
78
|
|
|
public function path(): ?string |
79
|
|
|
{ |
80
|
|
|
return $this->uploadPath() ?? $this->localPath(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieve the PDF's AWS S3 path. |
85
|
|
|
* |
86
|
|
|
* @return string|null |
87
|
|
|
*/ |
88
|
|
|
public function uploadPath(): ?string |
89
|
|
|
{ |
90
|
|
|
return $this->uploadPath; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retrieve the PDF's local file path. |
95
|
|
|
* |
96
|
|
|
* @return string|null |
97
|
|
|
*/ |
98
|
|
|
public function localPath(): ?string |
99
|
|
|
{ |
100
|
|
|
return $this->localPath; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Retrieve the PDF's AWS S3 url. |
105
|
|
|
* |
106
|
|
|
* @return string|null |
107
|
|
|
*/ |
108
|
|
|
public function url(): ?string |
109
|
|
|
{ |
110
|
|
|
return $this->url; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|