Passed
Pull Request — master (#5)
by Stephen
02:54
created

S3   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 52
c 4
b 0
f 0
dl 0
loc 174
rs 10
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setDisk() 0 5 1
A delete() 0 3 1
A upload_raw() 0 9 2
A url() 0 6 2
A list() 0 23 4
A upload() 0 9 2
A exists() 0 3 1
A urlTemp() 0 3 1
A download() 0 18 2
A __construct() 0 4 1
1
<?php
2
3
namespace Sfneal\Helpers\Aws\S3;
4
5
use DateTimeInterface;
6
use Illuminate\Contracts\Filesystem\FileNotFoundException;
7
use Illuminate\Support\Facades\Response;
8
use Illuminate\Support\Facades\Storage;
9
10
class S3
11
{
12
    /**
13
     * @var string
14
     */
15
    private $s3_key;
16
17
    /**
18
     * @var string
19
     */
20
    private $disk;
21
22
    /**
23
     * S3 constructor.
24
     *
25
     * @param string $s3_key
26
     */
27
    public function __construct(string $s3_key)
28
    {
29
        $this->s3_key = $s3_key;
30
        $this->disk = config('filesystem.cloud', 's3');
31
    }
32
33
    /**
34
     * Set the filesystem disk.
35
     *
36
     * @param string $disk
37
     * @return $this
38
     */
39
    public function setDisk(string $disk): self
40
    {
41
        $this->disk = $disk;
42
43
        return $this;
44
    }
45
46
    /**
47
     * Return either an S3 file url.
48
     *
49
     * @param bool $temp
50
     * @param DateTimeInterface|null $expiration
51
     * @return string
52
     */
53
    public function url(bool $temp = true, DateTimeInterface $expiration = null): string
54
    {
55
        if ($temp) {
56
            return Storage::disk($this->disk)->temporaryUrl($this->s3_key, $expiration ?? now()->addMinutes(60));
57
        } else {
58
            return Storage::disk($this->disk)->url($this->s3_key);
59
        }
60
    }
61
62
    /**
63
     * Return either a temporary S3 file url.
64
     *
65
     * @param DateTimeInterface|null $expiration
66
     * @return string
67
     */
68
    public function urlTemp(DateTimeInterface $expiration = null): string
69
    {
70
        return $this->url(true, $expiration);
71
    }
72
73
    /**
74
     * Determine if an S3 file exists.
75
     *
76
     * @return bool
77
     */
78
    public function exists(): bool
79
    {
80
        return Storage::disk($this->disk)->exists($this->s3_key);
81
    }
82
83
    /**
84
     * Upload a file to an S3 bucket.
85
     *
86
     * @param string $file_path
87
     * @param string|null $acl
88
     * @return string
89
     */
90
    public function upload(string $file_path, string $acl = null): string
91
    {
92
        if (is_null($acl)) {
93
            Storage::disk($this->disk)->put($this->s3_key, fopen($file_path, 'r+'));
94
        } else {
95
            Storage::disk($this->disk)->put($this->s3_key, fopen($file_path, 'r+'), $acl);
96
        }
97
98
        return $this->url();
99
    }
100
101
    /**
102
     * Upload raw file contents to an S3 bucket.
103
     *
104
     * @param string $file_contents
105
     * @param string|null $acl
106
     * @return string
107
     */
108
    public function upload_raw(string $file_contents, string $acl = null): string
109
    {
110
        if (is_null($acl)) {
111
            Storage::disk($this->disk)->put($this->s3_key, $file_contents);
112
        } else {
113
            Storage::disk($this->disk)->put($this->s3_key, $file_contents, $acl);
114
        }
115
116
        return $this->url();
117
    }
118
119
    /**
120
     * Download a file from an S3 bucket.
121
     *
122
     * @param string|null $file_name
123
     * @return \Illuminate\Http\Response
124
     * @throws FileNotFoundException|\League\Flysystem\FileNotFoundException
125
     */
126
    public function download(string $file_name = null): \Illuminate\Http\Response
127
    {
128
        if (is_null($file_name)) {
129
            $file_name = basename($this->s3_key);
130
        }
131
132
        $mime = Storage::disk($this->disk)->getMimetype($this->s3_key);
0 ignored issues
show
Bug introduced by
The method getMimetype() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        $mime = Storage::disk($this->disk)->/** @scrutinizer ignore-call */ getMimetype($this->s3_key);
Loading history...
133
        $size = Storage::disk($this->disk)->getSize($this->s3_key);
0 ignored issues
show
Bug introduced by
The method getSize() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
        $size = Storage::disk($this->disk)->/** @scrutinizer ignore-call */ getSize($this->s3_key);
Loading history...
134
135
        $response = [
136
            'Content-Type' => $mime,
137
            'Content-Length' => $size,
138
            'Content-Description' => 'File Transfer',
139
            'Content-Disposition' => "attachment; filename={$file_name}",
140
            'Content-Transfer-Encoding' => 'binary',
141
        ];
142
143
        return Response::make(Storage::disk($this->disk)->get($this->s3_key), 200, $response);
144
    }
145
146
    /**
147
     * Delete a file or folder from an S3 bucket.
148
     *
149
     * @return bool
150
     */
151
    public function delete(): bool
152
    {
153
        return Storage::disk($this->disk)->delete($this->s3_key);
154
    }
155
156
    /**
157
     * List all of the files in an S3 directory.
158
     *
159
     * @return array
160
     */
161
    public function list(): array
162
    {
163
        $storage = Storage::disk($this->disk);
164
        $client = $storage->getAdapter()->getClient();
0 ignored issues
show
Bug introduced by
The method getAdapter() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
        $client = $storage->/** @scrutinizer ignore-call */ getAdapter()->getClient();
Loading history...
165
        $command = $client->getCommand('ListObjects');
166
        $command['Bucket'] = $storage->getAdapter()->getBucket();
167
        $command['Prefix'] = $this->s3_key;
168
        $result = $client->execute($command);
169
170
        $files = [];
171
        if (isset($result['Contents']) && ! empty($result['Contents'])) {
172
            foreach ($result['Contents'] as $content) {
173
                $url = fileURL($content['Key']);
174
                $parts = explode('/', explode('?', $url, 2)[0]);
175
                $files[] = [
176
                    'name' => end($parts),
177
                    'url' => $url,
178
                    'key' => $content['Key'],
179
                ];
180
            }
181
        }
182
183
        return $files;
184
    }
185
}
186