Test Failed
Pull Request — master (#16)
by Stephen
03:05
created

s3_delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Contracts\Filesystem\FileNotFoundException;
4
use Illuminate\Http\Response;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Response. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use Illuminate\Support\Facades\Storage;
6
use Sfneal\Helpers\Aws\S3\Utils\S3;
7
8
/**
9
 * Return either an S3 file url.
10
 *
11
 * @param string $path
12
 * @param bool $temp
13
 * @param DateTimeInterface|null $expiration
14
 * @return string
15
 */
16
function fileURL(string $path, bool $temp = true, DateTimeInterface $expiration = null): string
17
{
18
    // todo: refactor to `fileUrl()`
19
    if ($temp) {
20
        return (new S3($path))->urlTemp($expiration);
21
    } else {
22
        return (new S3($path))->url();
23
    }
24
}
25
26
/**
27
 * Return either a temporary S3 file url.
28
 *
29
 * @param string $path
30
 * @param DateTimeInterface|null $expiration
31
 * @return string
32
 */
33
function fileUrlTemp(string $path, DateTimeInterface $expiration = null): string
34
{
35
    return (new S3($path))->urlTemp($expiration);
36
}
37
38
/**
39
 * Determine if an S3 file exists.
40
 *
41
 * @param string $s3_key
42
 * @return bool
43
 */
44
function s3_exists(string $s3_key): bool
45
{
46
    return Storage::disk('s3')->exists($s3_key);
47
}
48
49
/**
50
 * Upload a file to an S3 bucket.
51
 *
52
 * @param $s3_key
53
 * @param $file_path
54
 * @param null $acl
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $acl is correct as it would always require null to be passed?
Loading history...
55
 * @return string
56
 */
57
function s3_upload($s3_key, $file_path, $acl = null): string
58
{
59
    return (new S3($s3_key))->upload($file_path, $acl);
60
}
61
62
/**
63
 * Download a file from an S3 bucket.
64
 *
65
 * @param $file_url
66
 * @param string|null $file_name
67
 * @return Response
68
 * @throws FileNotFoundException|\League\Flysystem\FileNotFoundException
69
 */
70
function s3_download($file_url, string $file_name = null): Response
71
{
72
    return (new S3($file_url))->download($file_name);
73
}
74
75
/**
76
 * Upload raw file contents to an S3 bucket.
77
 *
78
 * @param $s3_key
79
 * @param string $file_contents
80
 * @param string|null $acl
81
 * @return string
82
 */
83
function s3_upload_raw($s3_key, string $file_contents, string $acl = null): string
84
{
85
    return (new S3($s3_key))->upload_raw($file_contents, $acl);
86
}
87
88
/**
89
 * List all of the files in an S3 directory.
90
 *
91
 * @param $s3_key
92
 * @return array
93
 */
94
function s3_list($s3_key): array
95
{
96
    return (new S3($s3_key))->list();
97
}
98