Issues (96)

src/Services/FileUpload.php (9 issues)

1
<?php
2
3
namespace Ikechukwukalu\Clamavfileupload\Services;
4
5
use Ikechukwukalu\Clamavfileupload\Contracts\FileUploadInterface;
6
use Ikechukwukalu\Clamavfileupload\Facades\Support\TemporaryFileUpload;
7
use Ikechukwukalu\Clamavfileupload\Events\ClamavFileScan;
8
use Ikechukwukalu\Clamavfileupload\Models\FileUpload as FileUploadModel;
9
use Ikechukwukalu\Clamavfileupload\Support\ClamavFileUpload;
10
use Illuminate\Http\Request;
11
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
12
use Symfony\Component\HttpFoundation\File\UploadedFile;
13
14
class FileUpload extends ClamavFileUpload implements FileUploadInterface
15
{
16
    /**
17
     * Upload single or multiple files.
18
     *
19
     * @param  \Illuminate\Http\Request  $request
20
     * @param  array  $settings
21
     * @return  \Ikechukwukalu\Clamavfileupload\Models\FileUpload
22
     * @return  \Illuminate\Database\Eloquent\Collection
23
     * @return  bool
24
     */
25
    public function uploadFiles(Request $request,
26
                array $settings = []): bool|FileUploadModel|EloquentCollection
27
    {
28
        $this->customFileUploadSettings($settings);
29
        $this->fileUploadSettings($request);
30
        return $this->runFileUpload($settings);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->runFileUpload($settings) also could return the type Illuminate\Database\Eloquent\Collection|boolean which is incompatible with the documented return type Ikechukwukalu\Clamavfileupload\Models\FileUpload.
Loading history...
31
    }
32
33
    /**
34
     * Run files scan and upload.
35
     *
36
     * @param  array $settings
37
     * @return  \Ikechukwukalu\Clamavfileupload\Models\FileUpload
38
     * @return  \Illuminate\Database\Eloquent\Collection
39
     * @return  bool
40
     */
41
    protected function runFileUpload(array $settings = []): bool|FileUploadModel|EloquentCollection
42
    {
43
        ClamavFileScan::dispatch();
44
45
        if (in_array($this->getDisk(), config('clamavfileupload.s3_disks'))) {
46
            return $this->runFileUploadForS3($settings);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->runFileUploadForS3($settings) also could return the type Illuminate\Database\Eloquent\Collection|boolean which is incompatible with the documented return type Ikechukwukalu\Clamavfileupload\Models\FileUpload.
Loading history...
47
        }
48
49
        if ($data = $this->fileUpload()) {
50
            return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data also could return the type Illuminate\Database\Eloquent\Collection|true which is incompatible with the documented return type Ikechukwukalu\Clamavfileupload\Models\FileUpload.
Loading history...
51
        }
52
53
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type Ikechukwukalu\Clamavfileupload\Models\FileUpload.
Loading history...
54
    }
55
56
    /**
57
     * Set file request.
58
     *
59
     * @param array $tmpFiles
60
     * @return \Illuminate\Http\Request
61
     */
62
    private function setFileRequest(array $tmpFiles): Request
63
    {
64
        $request = new Request;
65
66
        if (count($tmpFiles) > 1) {
67
            $request->files->set($this->input, $this->setMultipleFiles($tmpFiles));
68
        } else {
69
            $request->files->set($this->input, $this->setSingleFile($tmpFiles[0]));
70
        }
71
72
        return $request;
73
    }
74
75
    /**
76
     * Set multiple files.
77
     *
78
     * @param array $tmpFiles
79
     * @return array
80
     */
81
    private function setMultipleFiles(array $tmpFiles): array
82
    {
83
        $files = [];
84
85
        foreach ($tmpFiles as $tmpFile) {
86
            $extension = explode('.', $tmpFile)[1];
87
            $files[] = new UploadedFile($tmpFile, ".{$extension}");
88
        }
89
90
        return $files;
91
    }
92
93
    /**
94
     * Set single files.
95
     *
96
     * @param string $tmpFile
97
     * @return UploadedFile
98
     */
99
    private function setSingleFile(string $tmpFile): UploadedFile
100
    {
101
        $extension = explode('.', $tmpFile)[1];
102
        return new UploadedFile($tmpFile, ".{$extension}");
103
    }
104
105
    /**
106
     * Run file upload for s3.
107
     *
108
     * @param  array $settings
109
     * @return  \Ikechukwukalu\Clamavfileupload\Models\FileUpload
110
     * @return  \Illuminate\Database\Eloquent\Collection
111
     * @return  bool
112
     */
113
    protected function runFileUploadForS3(array $settings = []): bool|FileUploadModel|EloquentCollection
114
    {
115
        $fileUpload = new TemporaryFileUpload;
116
        $fileUpload::customFileUploadSettings($settings);
0 ignored issues
show
The method customFileUploadSettings() does not exist on Ikechukwukalu\Clamavfile...ort\TemporaryFileUpload. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

116
        $fileUpload::/** @scrutinizer ignore-call */ 
117
                     customFileUploadSettings($settings);
Loading history...
117
        $fileUpload::fileUploadSettings($this->request);
0 ignored issues
show
The method fileUploadSettings() does not exist on Ikechukwukalu\Clamavfile...ort\TemporaryFileUpload. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

117
        $fileUpload::/** @scrutinizer ignore-call */ 
118
                     fileUploadSettings($this->request);
Loading history...
118
        $tmpFiles = $fileUpload::fileUpload();
0 ignored issues
show
The method fileUpload() does not exist on Ikechukwukalu\Clamavfile...ort\TemporaryFileUpload. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

118
        /** @scrutinizer ignore-call */ 
119
        $tmpFiles = $fileUpload::fileUpload();
Loading history...
119
120
        $request = $this->setFileRequest($tmpFiles);
121
        $this->customFileUploadSettings($settings);
122
        $this->fileUploadSettings($request);
123
        $data = $this->fileUpload();
124
125
        TemporaryFileUpload::removeFiles($tmpFiles);
0 ignored issues
show
The method removeFiles() does not exist on Ikechukwukalu\Clamavfile...ort\TemporaryFileUpload. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

125
        TemporaryFileUpload::/** @scrutinizer ignore-call */ 
126
                             removeFiles($tmpFiles);
Loading history...
126
127
        return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data also could return the type Illuminate\Database\Eloquent\Collection|boolean which is incompatible with the documented return type Ikechukwukalu\Clamavfileupload\Models\FileUpload.
Loading history...
128
    }
129
}
130