TemporaryFileUpload::storeFiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Ikechukwukalu\Clamavfileupload\Support;
4
5
use Ikechukwukalu\Clamavfileupload\Foundation\FileUpload;
6
use Illuminate\Contracts\Filesystem\Filesystem;
7
use Illuminate\Support\Facades\Storage;
8
9
class TemporaryFileUpload extends FileUpload
10
{
11
    /**
12
     * Run files scan and upload.
13
     *
14
     * @return  bool
15
     */
16
    public function fileUpload(): bool|array
17
    {
18
        if($this->request->file()) {
19
            return $this->storeFiles();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->storeFiles() returns the type array|array<integer,string>|string[] which is incompatible with the documented return type boolean.
Loading history...
20
        }
21
22
        $this->failedUpload(trans('clamavfileupload::clamav.empty_file_input'));
23
        return false;
24
    }
25
26
    /**
27
     * Remove single or multiple files.
28
     *
29
     * @param array $files
30
     * @return  bool
31
     */
32
    public function removeFiles(array $files = [], null|string $disk = null):  bool
33
    {
34
        foreach ($files as $file) {
35
            $file = str_replace($this->storageDisk()->path('tmp'), '', $file);
36
            $this->storageDisk()->delete('tmp' . $file);
37
        }
38
39
        return true;
40
    }
41
42
    /**
43
     * Provide \Illuminate\Support\Facades\Storage::build.
44
     *
45
     * @return  \Illuminate\Contracts\Filesystem\Filesystem
46
     */
47
    protected function provideDisk(): Filesystem
48
    {
49
        return Storage::build([
50
            'driver' => 'local',
51
            'root' => $this->storageDisk()->path('tmp')
52
        ]);
53
    }
54
55
    /**
56
     * Get \Illuminate\Support\Facades\Storage::disk.
57
     *
58
     * @return  \Illuminate\Contracts\Filesystem\Filesystem
59
     */
60
    protected function storageDisk(): Filesystem
61
    {
62
        return Storage::disk('local');
63
    }
64
65
    /**
66
     * Save single or multiple files.
67
     *
68
     * @return  bool
69
     * @return  array
70
     */
71
    protected function storeFiles(): bool|array
72
    {
73
        $this->fileName = $this->setFileName();
74
75
        if (is_array($this->request->file($this->input))) {
76
            return $this->saveMultipleFiles($this->fileName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->saveMultipleFiles($this->fileName) returns the type array|string[] which is incompatible with the documented return type boolean.
Loading history...
77
        }
78
79
        return $this->saveSingleFile($this->fileName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->saveSingleFile($this->fileName) returns the type array<integer,string> which is incompatible with the documented return type boolean.
Loading history...
80
    }
81
82
    /**
83
     * Save multiple files.
84
     *
85
     * @param null|string $fileName
86
     * @return  bool
87
     * @return  array
88
     */
89
    protected function saveMultipleFiles(null|string $fileName = null): bool|array
90
    {
91
        $disk = $this->provideDisk();
92
        $tmpFiles = [];
93
        $i = 1;
94
95
        foreach ($this->request->file($this->input) as $file) {
96
            $tmp = $fileName . "_{$i}" . $this->getExtension($file);
97
            $disk->putFileAs("", $file, $tmp);
98
            $tmpFiles[] = $this->storageDisk()->path("tmp/{$tmp}");
99
100
            $i ++;
101
        }
102
103
        return $tmpFiles;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $tmpFiles returns the type array|string[] which is incompatible with the documented return type boolean.
Loading history...
104
    }
105
106
    /**
107
     * Save single file.
108
     *
109
     * @param null|string $fileName
110
     * @return  bool
111
     * @return  array
112
     */
113
    protected function saveSingleFile(null|string $fileName = null): bool|array
114
    {
115
        $tmp = $fileName . $this->getExtension();
116
117
        $this->provideDisk()->putFileAs("",
118
                $this->request->file($this->input),
0 ignored issues
show
Bug introduced by
It seems like $this->request->file($this->input) can also be of type Illuminate\Http\UploadedFile[] and array; however, parameter $file of Illuminate\Filesystem\Fi...temAdapter::putFileAs() does only seem to accept Illuminate\Http\File|Ill...ttp\UploadedFile|string, maybe add an additional type check? ( Ignorable by Annotation )

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

118
                /** @scrutinizer ignore-type */ $this->request->file($this->input),
Loading history...
119
                $fileName . $this->getExtension());
120
121
        return [$this->storageDisk()->path("tmp/{$tmp}")];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($this->stor...k()->path('tmp/'.$tmp)) returns the type array<integer,string> which is incompatible with the documented return type boolean.
Loading history...
122
    }
123
}
124