Exporter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 20
c 4
b 0
f 0
dl 0
loc 105
rs 10
wmc 7

7 Methods

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