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 (#4988)
by Pedro
39:53 queued 25:09
created

Uploader::deleteFiles()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
nc 5
nop 1
dl 0
loc 18
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Uploaders;
4
5
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface;
6
use Backpack\CRUD\app\Library\Uploaders\Support\Traits\HandleFileNaming;
7
use Backpack\CRUD\app\Library\Uploaders\Support\Traits\HandleRepeatableUploads;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
use Illuminate\Support\Facades\Storage;
11
use Illuminate\Support\Str;
12
13
abstract class Uploader implements UploaderInterface
14
{
15
    use HandleFileNaming;
16
    use HandleRepeatableUploads;
17
18
    private string $name;
19
20
    private string $disk = 'public';
21
22
    private string $path = '';
23
24
    private bool $handleMultipleFiles = false;
25
26
    private bool $deleteWhenEntryIsDeleted = true;
27
28
    /**
29
     * Cloud disks have the ability to generate temporary URLs to files, should we do it?
30
     */
31
    private bool $useTemporaryUrl = false;
32
33
    /**
34
     * When using temporary urls, define the time that the url will be valid.
35
     */
36
    private int $temporaryUrlExpirationTimeInMinutes = 1;
37
38
    /**
39
     * Indicates if the upload is relative to a relationship field/column.
40
     */
41
    private bool $isRelationship = false;
42
43
    public function __construct(array $crudObject, array $configuration)
44
    {
45
        $this->name = $crudObject['name'];
46
        $this->disk = $configuration['disk'] ?? $crudObject['disk'] ?? $this->disk;
47
        $this->path = $this->getPathFromConfiguration($crudObject, $configuration);
48
        $this->useTemporaryUrl = $configuration['temporary'] ?? $this->useTemporaryUrl;
49
        $this->temporaryUrlExpirationTimeInMinutes = $configuration['expiration'] ?? $this->temporaryUrlExpirationTimeInMinutes;
50
        $this->deleteWhenEntryIsDeleted = $configuration['deleteWhenEntryIsDeleted'] ?? $this->deleteWhenEntryIsDeleted;
51
        $this->fileNamer = is_callable($configuration['fileNamer'] ?? null) ? $configuration['fileNamer'] : $this->getFileNameGeneratorInstance($configuration['fileNamer'] ?? null);
52
    }
53
54
    /*******************************
55
     * Static methods
56
     *******************************/
57
    public static function for(array $crudObject, array $definition): UploaderInterface
58
    {
59
        return new static($crudObject, $definition);
60
    }
61
62
    /*******************************
63
     * public methods - event handler methods
64
     *******************************/
65
    public function storeUploadedFiles(Model $entry): Model
66
    {
67
        if ($this->handleRepeatableFiles) {
68
            return $this->handleRepeatableFiles($entry);
69
        }
70
71
        $entry->{$this->name} = $this->uploadFiles($entry);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $entry->$this->name is correct as $this->uploadFiles($entry) targeting Backpack\CRUD\app\Librar...Uploader::uploadFiles() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
72
73
        return $entry;
74
    }
75
76
    public function retrieveUploadedFiles(Model $entry): Model
77
    {
78
        if ($this->handleRepeatableFiles) {
79
            return $this->retrieveRepeatableFiles($entry);
80
        }
81
82
        return $this->retrieveFiles($entry);
83
    }
84
85
    public function deleteUploadedFiles(Model $entry): void
86
    {
87
        if ($this->deleteWhenEntryIsDeleted) {
88
            if (! in_array(SoftDeletes::class, class_uses_recursive($entry), true)) {
89
                $this->performFileDeletion($entry);
90
91
                return;
92
            }
93
94
            if ($entry->isForceDeleting() === true) {
95
                $this->performFileDeletion($entry);
96
            }
97
        }
98
    }
99
100
    /*******************************
101
     * Getters
102
     *******************************/
103
    public function getName(): string
104
    {
105
        return $this->name;
106
    }
107
108
    public function getDisk(): string
109
    {
110
        return $this->disk;
111
    }
112
113
    public function getPath(): string
114
    {
115
        return $this->path;
116
    }
117
118
    public function useTemporaryUrl(): bool
119
    {
120
        return $this->useTemporaryUrl;
121
    }
122
123
    public function getExpirationTimeInMinutes(): int
124
    {
125
        return $this->temporaryUrlExpirationTimeInMinutes;
126
    }
127
128
    public function shouldDeleteFiles(): bool
129
    {
130
        return $this->deleteWhenEntryIsDeleted;
131
    }
132
133
    public function getIdentifier(): string
134
    {
135
        if ($this->handleRepeatableFiles) {
136
            return $this->repeatableContainerName.'_'.$this->name;
137
        }
138
139
        return $this->name;
140
    }
141
142
    public function canHandleMultipleFiles(): bool
143
    {
144
        return $this->handleMultipleFiles;
145
    }
146
147
    /*******************************
148
     * Setters - fluently configure the uploader
149
     *******************************/
150
    public function multiple(): self
151
    {
152
        $this->handleMultipleFiles = true;
153
154
        return $this;
155
    }
156
157
    public function relationship(bool $isRelationship): self
158
    {
159
        $this->isRelationship = $isRelationship;
160
161
        return $this;
162
    }
163
164
    /*******************************
165
     * Default implementation functions
166
     *******************************/
167
    public function uploadFiles(Model $entry, $values = null)
0 ignored issues
show
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

167
    public function uploadFiles(Model $entry, /** @scrutinizer ignore-unused */ $values = 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...
168
    {
169
    }
170
171
    private function retrieveFiles(Model $entry): Model
172
    {
173
        $value = $entry->{$this->name};
174
175
        if ($this->handleMultipleFiles) {
176
            if (! isset($entry->getCasts()[$this->name]) && is_string($value)) {
177
                $entry->{$this->name} = json_decode($value, true);
178
            }
179
180
            return $entry;
181
        }
182
183
        $entry->{$this->name} = Str::after($value, $this->path);
184
185
        return $entry;
186
    }
187
188
    private function deleteFiles(Model $entry)
189
    {
190
        $values = $entry->{$this->name};
191
192
        if ($this->handleMultipleFiles) {
193
            // ensure we have an array of values when field is not casted in model.
194
            if (! isset($entry->getCasts()[$this->name]) && is_string($values)) {
195
                $values = json_decode($values, true);
196
            }
197
            foreach ($values as $value) {
198
                Storage::disk($this->disk)->delete($this->path.$value);
199
            }
200
201
            return;
202
        }
203
204
        $values = Str::after($values, $this->path);
205
        Storage::disk($this->disk)->delete($this->path.$values);
206
    }
207
208
    private function performFileDeletion(Model $entry)
209
    {
210
        if ($this->isRelationship || ! $this->handleRepeatableFiles) {
211
            $this->deleteFiles($entry);
212
213
            return;
214
        }
215
216
        $this->deleteRepeatableFiles($entry);
217
    }
218
219
    /*******************************
220
     * Private helper methods
221
     *******************************/
222
    private function getPathFromConfiguration(array $crudObject, array $configuration): string
223
    {
224
        $this->path = $configuration['path'] ?? $crudObject['prefix'] ?? $this->path;
225
226
        return empty($this->path) ? $this->path : Str::of($this->path)->finish('/')->value();
227
    }
228
}
229