RequestHandler::getImages()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace LaravelAt\ImageSanitize;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Arr;
8
use LaravelAt\ImageSanitize\Lists\MimeTypeList;
9
10
class RequestHandler
11
{
12
    public function __construct(
13
        protected ImageSanitize $imageSanitize,
14
        protected MimeTypeList $mimeTypeList,
15
    ) {
16
    }
17
18
    public function handle(Request $request): void
19
    {
20
        foreach ($this->getMaliciousImages($request->allFiles()) as $file) {
21
            file_put_contents($file->getPathname(), $this->imageSanitize->sanitize($file->get()));
22
        }
23
    }
24
25
    public function getMaliciousImages(array $files): array
26
    {
27
        if (! Arr::first($files) instanceof UploadedFile) {
28
            $files = collect($files)->flatten()->toArray();
0 ignored issues
show
Bug introduced by
$files of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

28
            $files = collect(/** @scrutinizer ignore-type */ $files)->flatten()->toArray();
Loading history...
29
        }
30
31
        return array_filter($this->getImages($files), function (UploadedFile $file) {
32
            return $this->imageSanitize->detect($file->get());
33
        });
34
    }
35
36
    public function getImages(array $files): array
37
    {
38
        return array_filter($files, function (UploadedFile $file) {
39
            return in_array(mime_content_type($file->getPathname()), $this->mimeTypeList->get());
40
        });
41
    }
42
}
43