UploadStorageTrait::existsFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Sco\Admin\Traits;
4
5
use Illuminate\Http\UploadedFile;
6
use Illuminate\Support\Facades\Storage;
7
8
trait UploadStorageTrait
9
{
10
    /**
11
     * @var string storage disk name
12
     */
13
    protected $disk;
14
15
    /**
16
     * @var string|\Closure|null The relative path of file
17
     */
18
    protected $uploadPath;
19
20
    /**
21
     * @var \Closure|null The rule of generate file name
22
     */
23
    protected $uploadFileNameRule;
24
25
    /**
26
     * Get Filesystem Disk of the upload file.
27
     *
28
     * @return string
29
     */
30
    public function getDisk()
31
    {
32
        if ($this->disk) {
33
            return $this->disk;
34
        }
35
36
        return $this->getDefaultDisk();
37
    }
38
39
    protected function getDefaultDisk()
40
    {
41
        return config('admin.upload.disk', 'public');
42
    }
43
44
    /**
45
     * Set Filesystem Disk of the upload file.
46
     *
47
     * @param string $value
48
     *
49
     * @return $this
50
     */
51
    public function setDisk($value)
52
    {
53
        $this->disk = $value;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Get a default relative path of the upload file.
60
     *
61
     * @param \Illuminate\Http\UploadedFile $file
62
     *
63
     * @return string
64
     */
65
    protected function getDefaultUploadPath(UploadedFile $file)
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

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

Loading history...
66
    {
67
        $root = config('admin.upload.directory', 'admin/uploads');
68
69
        return trim($root, '/') . date('/Y/m/d');
70
    }
71
72
    /**
73
     * Get relative path of the upload file.
74
     *
75
     * @param \Illuminate\Http\UploadedFile $file
76
     *
77
     * @return \Closure|mixed|null|string
78
     */
79
    public function getUploadPath(UploadedFile $file)
80
    {
81
        if (($path = $this->uploadPath)) {
82
            if (is_callable($path)) {
83
                $path = call_user_func($path, $file);
84
            }
85
86
            return trim($path, '/');
87
        }
88
89
        return $this->getDefaultUploadPath($file);
90
    }
91
92
    /**
93
     * Set relative path of the upload file.
94
     *
95
     * @param string|\Closure $value
96
     *
97
     * @return $this
98
     */
99
    public function setUploadPath($value)
100
    {
101
        $this->uploadPath = $value;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Get a file name of the upload file.
108
     *
109
     * @param \Illuminate\Http\UploadedFile $file
110
     *
111
     * @return mixed|string
112
     */
113
    public function getUploadFileName(UploadedFile $file)
114
    {
115
        if (is_callable($this->uploadFileNameRule)) {
116
            return call_user_func($this->uploadFileNameRule, $file);
117
        }
118
119
        return $this->getDefaultFileName($file);
120
    }
121
122
    /**
123
     * Get a default name of the upload file.
124
     *
125
     * @param \Illuminate\Http\UploadedFile $file
126
     *
127
     * @return string
128
     */
129
    protected function getDefaultFileName(UploadedFile $file)
130
    {
131
        return $file->hashName();
132
    }
133
134
    /**
135
     * Set the generation rule for the filename of the uploaded file
136
     *
137
     * @param \Closure $value
138
     *
139
     * @return $this
140
     */
141
    public function setUploadFileNameRule(\Closure $value)
142
    {
143
        $this->uploadFileNameRule = $value;
144
145
        return $this;
146
    }
147
148
    protected function existsFile($path)
149
    {
150
        return Storage::disk($this->getDisk())->exists($path);
151
    }
152
153
    protected function getFileUrl($path)
154
    {
155
        if (($disk = $this->getDisk())) {
156
            $url = Storage::disk($disk)->url($path);
157
        } else {
158
            $url = asset($path);
159
        }
160
161
        return $url;
162
    }
163
}
164