Passed
Push — master ( 83ad03...1c456b )
by Stephen
14:09 queued 10s
created

fileUrlTemp()   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 3
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 Sfneal\Helpers\Aws\S3\S3;
6
7
/**
8
 * Return either an S3 file url.
9
 *
10
 * @param string $path
11
 * @param bool $temp
12
 * @param DateTimeInterface|null $expiration
13
 * @return string
14
 */
15
function fileURL(string $path, bool $temp = true, DateTimeInterface $expiration = null): string
16
{
17
    // todo: refactor to `fileUrl()`
18
    return (new S3($path))->url($temp, $expiration);
19
}
20
21
/**
22
 * Return either a temporary S3 file url.
23
 *
24
 * @param string $path
25
 * @param bool $temp
26
 * @param DateTimeInterface|null $expiration
27
 * @return string
28
 */
29
function fileUrlTemp(string $path, bool $temp = true, DateTimeInterface $expiration = null): string
0 ignored issues
show
Unused Code introduced by
The parameter $temp is not used and could be removed. ( Ignorable by Annotation )

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

29
function fileUrlTemp(string $path, /** @scrutinizer ignore-unused */ bool $temp = true, DateTimeInterface $expiration = null): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
{
31
    return (new S3($path))->urlTemp($expiration);
32
}
33
34
/**
35
 * Determine if an S3 file exists.
36
 *
37
 * @param string $s3_key
38
 * @return bool
39
 */
40
function s3_exists(string $s3_key): bool
41
{
42
    return (new S3($s3_key))->exists();
43
}
44
45
/**
46
 * Upload a file to an S3 bucket.
47
 *
48
 * @param $s3_key
49
 * @param $file_path
50
 * @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...
51
 * @return string
52
 */
53
function s3_upload($s3_key, $file_path, $acl = null): string
54
{
55
    return (new S3($s3_key))->upload($file_path, $acl);
56
}
57
58
/**
59
 * Download a file from an S3 bucket.
60
 *
61
 * @param $file_url
62
 * @param string|null $file_name
63
 * @return Response
64
 * @throws FileNotFoundException|\League\Flysystem\FileNotFoundException
65
 */
66
function s3_download($file_url, string $file_name = null): Response
67
{
68
    return (new S3($file_url))->download($file_name);
69
}
70
71
/**
72
 * Delete a file or folder from an S3 bucket.
73
 *
74
 * @param $s3_key
75
 * @return bool
76
 */
77
function s3_delete($s3_key): bool
78
{
79
    return (new S3($s3_key))->delete();
80
}
81
82
/**
83
 * Upload raw file contents to an S3 bucket.
84
 *
85
 * @param $s3_key
86
 * @param string $file_contents
87
 * @param string|null $acl
88
 * @return string
89
 */
90
function s3_upload_raw($s3_key, string $file_contents, string $acl = null): string
91
{
92
    return (new S3($s3_key))->upload_raw($file_contents, $acl);
93
}
94
95
/**
96
 * List all of the files in an S3 directory.
97
 *
98
 * @param $s3_key
99
 * @return array
100
 */
101
function s3_list($s3_key): array
102
{
103
    return (new S3($s3_key))->list();
104
}
105