Passed
Pull Request — master (#61)
by Stephen
02:49
created

Exporter::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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