Passed
Push — master ( 80f51c...7d93ac )
by Stephen
10:39 queued 11s
created

Renderer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 12 2
A handle() 0 7 1
A __construct() 0 7 1
A handleJob() 0 3 1
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 AWS S3 path to upload a file to after render (if provided)
17
     */
18
    protected $uploadPath;
19
20
    /**
21
     * Renderer constructor.
22
     *
23
     * - $content can be a View or HTML file contents
24
     *
25
     * @param mixed $content
26
     * @param string|null $uploadPath
27
     */
28
    public function __construct($content, string $uploadPath = null)
29
    {
30
        // Content of the PDF
31
        $this->content = $content;
32
33
        // Upload PDF after rendering (defaults to false)
34
        $this->uploadPath = $uploadPath;
35
    }
36
37
    /**
38
     * Render the content to a exportable object.
39
     *
40
     * @return object
41
     */
42
    abstract protected function render(): object;
43
44
    /**
45
     * Retrieve in an Exporter instance created from the $exportable.
46
     *
47
     * @param $exportable
48
     * @return Exporter
49
     */
50
    abstract protected function exporter($exportable): Exporter;
51
52
    /**
53
     * Dispatch this renderer instance to the Job queue without having to construct it statically.
54
     *
55
     * @return mixed
56
     */
57
    public function handleJob()
58
    {
59
        return Bus::dispatch($this);
60
    }
61
62
    /**
63
     * Load renderable content to an Exporter instance and render the output.
64
     *
65
     *  - storing output in a property avoids potentially calling expensive 'output()' method multiple times
66
     *
67
     * @return Exporter
68
     */
69
    public function handle(): Exporter
70
    {
71
        // Render the PDF
72
        $exportable = $this->render();
73
74
        // Instantiate the Exporter
75
        return $this->export($exportable);
76
    }
77
78
    /**
79
     * Create & return an Exporter instance.
80
     *
81
     * @param $exportable
82
     * @return Exporter
83
     */
84
    private function export($exportable): Exporter
85
    {
86
        // Initialize the PdfExporter
87
        $exporter = $this->exporter($exportable);
88
89
        // Upload after rendering if an upload path was provided
90
        if ($this->uploadPath) {
91
            $exporter->upload($this->uploadPath);
92
        }
93
94
        // Return a PdfExporter
95
        return $exporter;
96
    }
97
}
98