Completed
Push — master ( a6d5b0...1121c7 )
by wen
12:56
created

StorageTrait::getDisk()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Traits;
4
5
use Illuminate\Http\UploadedFile;
6
use Illuminate\Support\Facades\Storage;
7
8
trait StorageTrait
9
{
10
    protected $disk;
11
12
    /**
13
     * @var string|\Closure|null
14
     */
15
    protected $uploadPath;
16
17
    /**
18
     * @var \Closure|null
19
     */
20
    protected $uploadFileNameRule;
21
22
    public function getDisk()
23
    {
24
        if ($this->disk) {
25
            return $this->disk;
26
        }
27
28
        return $this->getDefaultDisk();
29
    }
30
31
    protected function getDefaultDisk()
32
    {
33
        return config('admin.upload.disk', 'public');
34
    }
35
36
    public function setDisk($value)
37
    {
38
        $this->disk = $value;
39
40
        return $this;
41
    }
42
43
    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...
44
    {
45
        return config('admin.upload.directory', 'admin/uploads');
46
    }
47
48
    public function getUploadPath(UploadedFile $file)
49
    {
50
        if (!($path = $this->uploadPath)) {
51
            $path = $this->getDefaultUploadPath($file);
52
        }
53
        if (is_callable($path)) {
54
            return call_user_func($path, $file);
55
        }
56
57
        return $path;
58
    }
59
60
    /**
61
     * The path of file save
62
     *
63
     * @param string|\Closure $value
64
     *
65
     * @return $this
66
     */
67
    public function setUploadPath($value)
68
    {
69
        $this->uploadPath = $value;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Get a filename for the upload file.
76
     *
77
     * @param \Illuminate\Http\UploadedFile $file
78
     *
79
     * @return mixed|string
80
     */
81
    public function getUploadFileName(UploadedFile $file)
82
    {
83
        if (is_callable($this->uploadFileNameRule)) {
84
            return call_user_func($this->uploadFileNameRule, $file);
85
        }
86
87
        return $this->getDefaultFileName($file);
88
    }
89
90
    protected function getDefaultFileName(UploadedFile $file)
91
    {
92
        return $file->hashName();
93
    }
94
95
    /**
96
     * Set the generation rule for the filename of the uploaded file
97
     *
98
     * @param \Closure $value
99
     *
100
     * @return $this
101
     */
102
    public function setUploadFileNameRule(\Closure $value)
103
    {
104
        $this->uploadFileNameRule = $value;
105
106
        return $this;
107
    }
108
109
    protected function existsFile($path)
110
    {
111
        return Storage::disk($this->getDisk())->exists($path);
112
    }
113
114
    protected function getFileUrl($path)
115
    {
116
        if (($disk = $this->getDisk())) {
117
            $url = Storage::disk($disk)->url($path);
118
        } else {
119
            $url = asset($path);
120
        }
121
122
        return $url;
123
    }
124
}
125