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
Push — crud-uploads ( 044930 )
by Pedro
11:38
created

SingleBase64Image   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 28
c 1
b 0
f 1
dl 0
loc 59
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A saveRepeatableSingleBase64() 0 24 5
A save() 0 3 3
A saveSingleBase64() 0 26 5
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Uploads\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 save(Model $entry, $value = null)
13
    {
14
        return $this->isRepeatable && ! $this->isRelationship ? $this->saveRepeatableSingleBase64($entry, $value) : $this->saveSingleBase64($entry, $value);
15
    }
16
17
    private function saveSingleBase64($entry, $value)
18
    {
19
        $value = $value ?? CRUD::getRequest()->get($this->name);
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

19
        $value = $value ?? CRUD::/** @scrutinizer ignore-call */ getRequest()->get($this->name);
Loading history...
20
        $previousImage = $entry->getOriginal($this->name);
21
22
        if (! $value && $previousImage) {
23
            Storage::disk($this->disk)->delete($previousImage);
24
25
            return null;
26
        }
27
28
        if (Str::startsWith($value, 'data:image')) {
29
            if ($previousImage) {
30
                Storage::disk($this->disk)->delete($previousImage);
31
            }
32
33
            $base64Image = Str::after($value, ';base64,');
34
35
            $finalPath = $this->path.$this->getFileNameWithExtension($value);
36
            
37
            Storage::disk($this->disk)->put($finalPath, base64_decode($base64Image));
38
39
            return $finalPath;
40
        }
41
42
        return $previousImage;
43
    }
44
45
    private function saveRepeatableSingleBase64($entry, $value)
46
    {
47
        $previousImages = $this->getPreviousRepeatableValues($entry);
48
49
        foreach ($value as $row => $rowValue) {
50
            if ($rowValue) {
51
                if (Str::startsWith($rowValue, 'data:image')) {
52
                    $base64Image = Str::after($rowValue, ';base64,');
53
                    $finalPath = $this->path.$this->getFileNameWithExtension($rowValue);
54
                    Storage::disk($this->disk)->put($finalPath, base64_decode($base64Image));
55
                    $value[$row] = $previousImages[] = $finalPath;
56
57
                    continue;
58
                }
59
            }
60
        }
61
62
        $imagesToDelete = array_diff($previousImages, $value);
63
64
        foreach ($imagesToDelete as $image) {
65
            Storage::disk($this->disk)->delete($image);
66
        }
67
68
        return $value;
69
    }
70
}
71