Test Failed
Push — master ( d7ba8d...037364 )
by Stephen
01:00 queued 11s
created

CloudStorage::setDisk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Sfneal\Helpers\Aws\S3\Utils;
4
5
use DateTimeInterface;
6
use Illuminate\Contracts\Filesystem\Filesystem;
7
use Illuminate\Filesystem\FilesystemAdapter;
8
use Illuminate\Support\Facades\Storage;
9
use Sfneal\Helpers\Aws\S3\Utils\Interfaces\S3Accessors;
10
11
class CloudStorage implements S3Accessors
12
{
13
    /**
14
     * @var string AWS S3 file key
15
     */
16
    protected $s3Key;
17
18
    /**
19
     * @var string Storage S3 cloud disk name
20
     */
21
    protected $disk;
22
23
    /**
24
     * S3 constructor.
25
     *
26
     * @param string $s3Key
27
     * @param string|null $disk
28
     */
29
    public function __construct(string $s3Key, string $disk = null)
30
    {
31
        $this->s3Key = $s3Key;
32
        $this->disk = $disk ?? config('filesystem.cloud', 's3');
33
    }
34
35
    /**
36
     * Retrieve the S3 key (useful in conjunctions with `autocompletePath()` method).
37
     *
38
     * @return string
39
     */
40
    public function getKey(): string
41
    {
42
        return $this->s3Key;
43
    }
44
45
    /**
46
     * Return either an S3 file url.
47
     *
48
     * @return string
49
     */
50
    public function url(): string
51
    {
52
        return $this->storageDisk()->url($this->s3Key);
53
    }
54
55
    /**
56
     * Return either a temporary S3 file url.
57
     *
58
     * @param DateTimeInterface|null $expiration
59
     * @return string
60
     */
61
    public function urlTemp(DateTimeInterface $expiration = null): string
62
    {
63
        return $this->storageDisk()->temporaryUrl(
64
            $this->s3Key,
65
            $expiration ?? config('s3-helpers.expiration')
66
        );
67
    }
68
69
    /**
70
     * Retrieve a Filesystem instance for the specified disk.
71
     *
72
     * @return Filesystem|FilesystemAdapter
73
     */
74
    protected function storageDisk(): FilesystemAdapter
75
    {
76
        return Storage::disk($this->disk);
77
    }
78
}
79