Completed
Push — master ( 22fdaf...0ad42b )
by wen
10:44 queued 10s
created

UploadStorageTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 3
cbo 1
dl 0
loc 155
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getDisk() 0 8 2
A getDefaultDisk() 0 4 1
A setDisk() 0 6 1
A getDefaultUploadPath() 0 6 1
A getUploadPath() 0 11 3
A setUploadPath() 0 6 1
A getUploadFileName() 0 8 2
A getDefaultFileName() 0 4 1
A setUploadFileNameRule() 0 6 1
A existsFile() 0 4 1
A getFileUrl() 0 10 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 rtrim($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 ($this->uploadPath) {
82
            if (is_callable($this->uploadPath)) {
83
                return call_user_func($this->uploadPath, $file);
84
            }
85
            return $this->uploadPath;
86
        }
87
88
        return $this->getDefaultUploadPath($file);
89
    }
90
91
    /**
92
     * Set relative path of the upload file.
93
     *
94
     * @param string|\Closure $value
95
     *
96
     * @return $this
97
     */
98
    public function setUploadPath($value)
99
    {
100
        $this->uploadPath = $value;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Get a file name of the upload file.
107
     *
108
     * @param \Illuminate\Http\UploadedFile $file
109
     *
110
     * @return mixed|string
111
     */
112
    public function getUploadFileName(UploadedFile $file)
113
    {
114
        if (is_callable($this->uploadFileNameRule)) {
115
            return call_user_func($this->uploadFileNameRule, $file);
116
        }
117
118
        return $this->getDefaultFileName($file);
119
    }
120
121
    /**
122
     * Get a default name of the upload file.
123
     *
124
     * @param \Illuminate\Http\UploadedFile $file
125
     *
126
     * @return string
127
     */
128
    protected function getDefaultFileName(UploadedFile $file)
129
    {
130
        return $file->hashName();
131
    }
132
133
    /**
134
     * Set the generation rule for the filename of the uploaded file
135
     *
136
     * @param \Closure $value
137
     *
138
     * @return $this
139
     */
140
    public function setUploadFileNameRule(\Closure $value)
141
    {
142
        $this->uploadFileNameRule = $value;
143
144
        return $this;
145
    }
146
147
    protected function existsFile($path)
148
    {
149
        return Storage::disk($this->getDisk())->exists($path);
150
    }
151
152
    protected function getFileUrl($path)
153
    {
154
        if (($disk = $this->getDisk())) {
155
            $url = Storage::disk($disk)->url($path);
156
        } else {
157
            $url = asset($path);
158
        }
159
160
        return $url;
161
    }
162
}
163