ClamavFileUpload::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Ikechukwukalu\Clamavfileupload\Listeners;
4
5
use Ikechukwukalu\Clamavfileupload\Facades\Services\QueuedFileUpload;
6
use Ikechukwukalu\Clamavfileupload\Facades\Support\TemporaryFileUpload;
7
use Ikechukwukalu\Clamavfileupload\Events\ClamavQueuedFileScan;
8
use Illuminate\Contracts\Queue\ShouldQueue;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Illuminate\Http\Request;
11
use Symfony\Component\HttpFoundation\File\UploadedFile;
12
13
class ClamavFileUpload implements ShouldQueue
14
{
15
    private QueuedFileUpload $fileUpload;
16
    private Request $request;
17
18
    /**
19
     * Handle the event.
20
     *
21
     * @param \Ikechukwukalu\Clamavfileupload\Events\ClamavQueuedFileScan $event
22
     * @return  void
23
     */
24
    public function handle(ClamavQueuedFileScan $event): void
25
    {
26
        $this->runFileUploadProcesses($event);
27
    }
28
29
    /**
30
     * Set file request.
31
     *
32
     * @param \Ikechukwukalu\Clamavfileupload\Events\ClamavQueuedFileScan $event
33
     * @return  void
34
     */
35
    private function setFileRequest(ClamavQueuedFileScan $event): void
36
    {
37
        $this->request = new Request;
38
39
        if (count($event->tmpFiles) > 1) {
40
            $this->request->files->set($this->fileUpload::getInput(), $this->setMultipleFiles($event->tmpFiles));
0 ignored issues
show
Bug introduced by
The method getInput() does not exist on Ikechukwukalu\Clamavfile...rvices\QueuedFileUpload. 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

40
            $this->request->files->set($this->fileUpload::/** @scrutinizer ignore-call */ getInput(), $this->setMultipleFiles($event->tmpFiles));
Loading history...
41
        } else {
42
            $this->request->files->set($this->fileUpload::getInput(), $this->setSingleFile($event->tmpFiles[0]));
43
        }
44
    }
45
46
    /**
47
     * Set multiple files.
48
     *
49
     * @param array $tmpFiles
50
     * @return array
51
     */
52
    private function setMultipleFiles(array $tmpFiles): array
53
    {
54
        $files = [];
55
56
        foreach ($tmpFiles as $tmpFile) {
57
            $extension = explode('.', $tmpFile)[1];
58
            $files[] = new UploadedFile($tmpFile, ".{$extension}");
59
        }
60
61
        return $files;
62
    }
63
64
    /**
65
     * Set single files.
66
     *
67
     * @param string $tmpFile
68
     * @return UploadedFile
69
     */
70
    private function setSingleFile(string $tmpFile): UploadedFile
71
    {
72
        $extension = explode('.', $tmpFile)[1];
73
        return new UploadedFile($tmpFile, ".{$extension}");
74
    }
75
76
    /**
77
     * Run file upload processes.
78
     *
79
     * @param Ikechukwukalu\Clamavfileupload\Events\ClamavQueuedFileScan $event
0 ignored issues
show
Bug introduced by
The type Ikechukwukalu\Clamavfile...ts\ClamavQueuedFileScan was not found. Did you mean Ikechukwukalu\Clamavfile...ts\ClamavQueuedFileScan? If so, make sure to prefix the type with \.
Loading history...
80
     * @return  void
81
     */
82
    private function runFileUploadProcesses(ClamavQueuedFileScan $event): void
83
    {
84
        $this->fileUpload = new QueuedFileUpload;
85
        $this->fileUpload::customFileUploadSettings($event->settings);
0 ignored issues
show
Bug introduced by
The method customFileUploadSettings() does not exist on Ikechukwukalu\Clamavfile...rvices\QueuedFileUpload. 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

85
        $this->fileUpload::/** @scrutinizer ignore-call */ 
86
                           customFileUploadSettings($event->settings);
Loading history...
86
        $this->setFileRequest($event);
87
        $this->fileUpload::fileUploadSettings($this->request, $event->ref);
0 ignored issues
show
Bug introduced by
The method fileUploadSettings() does not exist on Ikechukwukalu\Clamavfile...rvices\QueuedFileUpload. 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

87
        $this->fileUpload::/** @scrutinizer ignore-call */ 
88
                           fileUploadSettings($this->request, $event->ref);
Loading history...
88
        $this->fileUpload::fileUpload();
0 ignored issues
show
Bug introduced by
The method fileUpload() does not exist on Ikechukwukalu\Clamavfile...rvices\QueuedFileUpload. 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

88
        $this->fileUpload::/** @scrutinizer ignore-call */ 
89
                           fileUpload();
Loading history...
89
90
        TemporaryFileUpload::removeFiles($event->tmpFiles);
0 ignored issues
show
Bug introduced by
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

90
        TemporaryFileUpload::/** @scrutinizer ignore-call */ 
91
                             removeFiles($event->tmpFiles);
Loading history...
91
    }
92
}
93