Passed
Push — master ( 7d93ac...50769f )
by Stephen
03:13 queued 11s
created

Renderer::setUploadPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Sfneal\ViewExport\Support\Adapters;
4
5
use Illuminate\Support\Facades\Bus;
6
use Sfneal\Queueables\AbstractJob;
7
8
abstract class Renderer extends AbstractJob
9
{
10
    /**
11
     * @var mixed Renderable content
12
     */
13
    protected $content;
14
15
    /**
16
     * @var string|null Local path to write a file to after rendering
17
     */
18
    protected $storePath;
19
20
    /**
21
     * @var string|null AWS S3 path to upload a file to after rendering
22
     */
23
    protected $uploadPath;
24
25
    /**
26
     * Renderer constructor.
27
     *
28
     * - $content can be a View or HTML file contents
29
     *
30
     * @param mixed $content
31
     */
32
    public function __construct($content)
33
    {
34
        // Content of the PDF
35
        $this->content = $content;
36
    }
37
38
    /**
39
     * Set a path to upload the Exportable to after it's been rendered.
40
     *
41
     * @param string $path
42
     * @return $this
43
     */
44
    public function setUploadPath(string $path): self
45
    {
46
        $this->uploadPath = $path;
47
48
        return $this;
49
    }
50
51
    /**
52
     * Store a rendered export on the local file system.
53
     *
54
     * @param string $storagePath
55
     * @return $this
56
     */
57
    public function setStorePath(string $storagePath): self
58
    {
59
        $this->storePath = $storagePath;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Render the content to a exportable object.
66
     *
67
     * @return object
68
     */
69
    abstract protected function render(): object;
70
71
    /**
72
     * Retrieve in an Exporter instance created from the $exportable.
73
     *
74
     * @param $exportable
75
     * @return Exporter
76
     */
77
    abstract protected function exporter($exportable): Exporter;
78
79
    /**
80
     * Dispatch this renderer instance to the Job queue without having to construct it statically.
81
     *
82
     * @return mixed
83
     */
84
    public function handleJob()
85
    {
86
        return Bus::dispatch($this);
87
    }
88
89
    /**
90
     * Load renderable content to an Exporter instance and render the output.
91
     *
92
     *  - storing output in a property avoids potentially calling expensive 'output()' method multiple times
93
     *
94
     * @return Exporter|mixed
95
     */
96
    public function handle(): Exporter
97
    {
98
        // Render the PDF
99
        $exportable = $this->render();
100
101
        // Instantiate the Exporter
102
        return $this->export($exportable);
103
    }
104
105
    /**
106
     * Create & return an Exporter instance.
107
     *
108
     * @param $exportable
109
     * @return Exporter
110
     */
111
    private function export($exportable): Exporter
112
    {
113
        // Initialize the PdfExporter
114
        $exporter = $this->exporter($exportable);
115
116
        // Upload after rendering
117
        if ($this->uploadPath) {
118
            $exporter->upload($this->uploadPath);
119
        }
120
121
        // Store after rendering
122
        if ($this->storePath) {
123
            $exporter->store($this->storePath);
124
        }
125
126
        // Return a PdfExporter
127
        return $exporter;
128
    }
129
}
130