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 ( 6e8179...79720b )
by Pedro
15:18
created

HandleRepeatableUploads::uploadRepeatableFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Uploaders\Support\Traits;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
6
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Facades\Storage;
10
11
trait HandleRepeatableUploads
12
{
13
    public bool $handleRepeatableFiles = false;
14
15
    public ?string $repeatableContainerName = null;
16
17
    /*******************************
18
     * Setters - fluently configure the uploader
19
     *******************************/
20
    public function repeats(string $repeatableContainerName): self
21
    {
22
        $this->handleRepeatableFiles = true;
23
24
        $this->repeatableContainerName = $repeatableContainerName;
25
26
        return $this;
27
    }
28
29
    /*******************************
30
     * Getters
31
     *******************************/
32
    public function getRepeatableContainerName(): ?string
33
    {
34
        return $this->repeatableContainerName;
35
    }
36
37
    /*******************************
38
     * Default implementation methods
39
     *******************************/
40
    protected function uploadRepeatableFiles(Model $entry, $values, $previousValues)
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

40
    protected function uploadRepeatableFiles(/** @scrutinizer ignore-unused */ Model $entry, $values, $previousValues)

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...
Unused Code introduced by
The parameter $values 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

40
    protected function uploadRepeatableFiles(Model $entry, /** @scrutinizer ignore-unused */ $values, $previousValues)

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...
Unused Code introduced by
The parameter $previousValues 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

40
    protected function uploadRepeatableFiles(Model $entry, $values, /** @scrutinizer ignore-unused */ $previousValues)

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...
41
    {
42
        return;
43
    }
44
45
    private function handleRepeatableFiles(Model $entry): Model
46
    {
47
        $values = collect(CRUD::getRequest()->get($this->getRepeatableContainerName()));
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

47
        $values = collect(CRUD::/** @scrutinizer ignore-call */ getRequest()->get($this->getRepeatableContainerName()));
Loading history...
48
        $files = collect(CRUD::getRequest()->file($this->getRepeatableContainerName()));
49
        $value = $this->mergeValuesRecursive($values, $files);
50
51
        if ($this->isRelationship) {
52
            return $this->uploadRelationshipFiles($entry, $value);
53
        }
54
55
        $entry->{$this->getRepeatableContainerName()} = json_encode($this->processRepeatableUploads($entry, $value));
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Backpack\CRUD\app\Librar...cessRepeatableUploads() does only seem to accept Illuminate\Support\Collection, maybe add an additional type check? ( Ignorable by Annotation )

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

55
        $entry->{$this->getRepeatableContainerName()} = json_encode($this->processRepeatableUploads($entry, /** @scrutinizer ignore-type */ $value));
Loading history...
56
57
        return $entry;
58
    }
59
60
    private function uploadRelationshipFiles(Model $entry, mixed $value): Model
61
    {
62
        $modelCount = CRUD::get('uploaded_'.$this->getRepeatableContainerName().'_count');
0 ignored issues
show
Bug introduced by
The method get() 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

62
        /** @scrutinizer ignore-call */ 
63
        $modelCount = CRUD::get('uploaded_'.$this->getRepeatableContainerName().'_count');
Loading history...
63
        $value = $value->slice($modelCount, 1)->toArray();
64
65
        foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) {
66
            if (array_key_exists($modelCount, $value) && isset($value[$modelCount][$uploader->getName()])) {
67
                $entry->{$uploader->getName()} = $uploader->uploadFiles($entry, $value[$modelCount][$uploader->getName()]);
68
            }
69
        }
70
71
        return $entry;
72
    }
73
74
    private function processRepeatableUploads(Model $entry, Collection $value): Collection
75
    {
76
        foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) {
77
            $uploadedValues = $uploader->uploadRepeatableFiles($entry, $value->pluck($uploader->getName())->toArray(), $this->getPreviousRepeatableValues($entry, $uploader));
78
79
            $value = $value->map(function ($item, $key) use ($uploadedValues, $uploader) {
80
                $item[$uploader->getName()] = $uploadedValues[$key] ?? null;
81
82
                return $item;
83
            });
84
        }
85
86
        return $value;
87
    }
88
89
    private function retrieveRepeatableFiles(Model $entry): Model
90
    {
91
        if ($this->isRelationship) {
92
            return $this->retrieveFiles($entry);
0 ignored issues
show
Bug introduced by
It seems like retrieveFiles() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

92
            return $this->/** @scrutinizer ignore-call */ retrieveFiles($entry);
Loading history...
93
        }
94
95
        return $entry;
96
    }
97
98
    private function deleteRepeatableFiles(Model $entry): void
99
    {
100
        if ($this->isRelationship) {
101
            $this->deleteFiles($entry);
0 ignored issues
show
Bug introduced by
It seems like deleteFiles() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

101
            $this->/** @scrutinizer ignore-call */ 
102
                   deleteFiles($entry);
Loading history...
102
103
            return;
104
        }
105
106
        $repeatableValues = collect($entry->{$this->getName()});
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

106
        $repeatableValues = collect($entry->{$this->/** @scrutinizer ignore-call */ getName()});
Loading history...
107
        foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $upload) {
108
            if (! $upload->shouldDeleteFiles()) {
109
                continue;
110
            }
111
            $values = $repeatableValues->pluck($upload->getName())->toArray();
112
            foreach ($values as $value) {
113
114
                if (! $value) { continue; }
115
116
                if (is_array($value)) {
117
                    foreach ($value as $subvalue) {
118
                        Storage::disk($upload->getDisk())->delete($upload->getPath().$subvalue);
119
                    }
120
                    continue;
121
                }
122
                
123
                Storage::disk($upload->getDisk())->delete($upload->getPath().$value);
124
            }
125
        }
126
    }
127
    /*******************************
128
     * Helper methods
129
     *******************************/
130
131
    /**
132
     * Given two multidimensional arrays/collections, merge them recursively.
133
     */
134
    private function mergeValuesRecursive(array|Collection $array1,array|Collection $array2): array|Collection
135
    {
136
        $merged = $array1;
137
        foreach ($array2 as $key => &$value) {
138
            if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
139
                $merged[$key] = $this->mergeValuesRecursive($merged[$key], $value);
140
            } else {
141
                $merged[$key] = $value;
142
            }
143
        }
144
145
        return $merged;
146
    }
147
148
    /**
149
     * Repeatable items send `_order_` parameter in the request.
150
     * This holds the order of the items in the repeatable container.
151
     */
152
    protected function getFileOrderFromRequest(): array
153
    {
154
        $items = CRUD::getRequest()->input('_order_'.$this->getRepeatableContainerName()) ?? [];
155
156
        array_walk($items, function (&$key, $value) {
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

156
        array_walk($items, function (&$key, /** @scrutinizer ignore-unused */ $value) {

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...
157
            $requestValue = $key[$this->getName()] ?? null;
158
            $key = $this->handleMultipleFiles ? (is_string($requestValue) ? explode(',', $requestValue) : $requestValue) : $requestValue;
159
        });
160
161
        return $items;
162
    }
163
164
    private function getPreviousRepeatableValues(Model $entry, UploaderInterface $uploader): array
165
    {
166
        $previousValues = json_decode($entry->getOriginal($uploader->getRepeatableContainerName()), true);
0 ignored issues
show
Bug introduced by
It seems like $entry->getOriginal($upl...eatableContainerName()) can also be of type array; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

166
        $previousValues = json_decode(/** @scrutinizer ignore-type */ $entry->getOriginal($uploader->getRepeatableContainerName()), true);
Loading history...
167
       
168
        if (! empty($previousValues)) {
169
            $previousValues = array_column($previousValues, $uploader->getName());
170
        }
171
172
        return $previousValues ?? [];
173
    }
174
}
175