Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — main (#5422)
by Cristian
37:08 queued 26:15
created

SingleBase64Image   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A uploadRepeatableFiles() 0 22 5
A uploadFiles() 0 25 5
A shouldUploadFiles() 0 3 3
A shouldKeepPreviousValueUnchanged() 0 3 3
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Uploaders;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Support\Str;
9
10
class SingleBase64Image extends Uploader
11
{
12
    public function uploadFiles(Model $entry, $value = null)
13
    {
14
        $value = $value ?? CRUD::getRequest()->get($this->getName());
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. 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

14
        $value = $value ?? CRUD::/** @scrutinizer ignore-call */ getRequest()->get($this->getName());
Loading history...
15
        $previousImage = $this->getPreviousFiles($entry);
16
17
        if (! $value && $previousImage) {
18
            Storage::disk($this->getDisk())->delete($previousImage);
19
20
            return null;
21
        }
22
23
        if (Str::startsWith($value, 'data:image')) {
24
            if ($previousImage) {
25
                Storage::disk($this->getDisk())->delete($previousImage);
26
            }
27
28
            $base64Image = Str::after($value, ';base64,');
29
            $finalPath = $this->getPath().$this->getFileName($value);
30
31
            Storage::disk($this->getDisk())->put($finalPath, base64_decode($base64Image));
32
33
            return $finalPath;
34
        }
35
36
        return $previousImage;
37
    }
38
39
    public function uploadRepeatableFiles($values, $previousRepeatableValues, $entry = null)
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

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

39
    public function uploadRepeatableFiles($values, $previousRepeatableValues, /** @scrutinizer ignore-unused */ $entry = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        foreach ($values as $row => $rowValue) {
42
            if ($rowValue) {
43
                if (Str::startsWith($rowValue, 'data:image')) {
44
                    $base64Image = Str::after($rowValue, ';base64,');
45
                    $finalPath = $this->getPath().$this->getFileName($rowValue);
46
                    Storage::disk($this->getDisk())->put($finalPath, base64_decode($base64Image));
47
                    $values[$row] = $previousRepeatableValues[] = $finalPath;
48
49
                    continue;
50
                }
51
            }
52
        }
53
54
        $imagesToDelete = array_diff($previousRepeatableValues, $values);
55
56
        foreach ($imagesToDelete as $image) {
57
            Storage::disk($this->getDisk())->delete($image);
58
        }
59
60
        return $values;
61
    }
62
63
    protected function shouldUploadFiles($value): bool
64
    {
65
        return $value && is_string($value) && Str::startsWith($value, 'data:image');
66
    }
67
68
    protected function shouldKeepPreviousValueUnchanged(Model $entry, $entryValue): bool
69
    {
70
        return $entry->exists && is_string($entryValue) && ! Str::startsWith($entryValue, 'data:image');
71
    }
72
}
73