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
43:09 queued 28:06
created

Uploader::for()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
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['whenDelete'] ?? $this->deleteWhenEntryIsDeleted;
51
52
        $this->fileNameGenerator = $this->getFileNameGeneratorInstance($configuration['fileNameGenerator'] ?? null);
53
        $this->fileName = $configuration['fileName'] ?? $this->fileName;
54
    }
55
56
    /*******************************
57
     * Static methods
58
     *******************************/
59
    public static function for(array $crudObject, array $definition): UploaderInterface
60
    {
61
        return new static($crudObject, $definition);
62
    }
63
64
    /*******************************
65
     * public methods - event handler methods
66
     *******************************/
67
    public function storeUploadedFiles(Model $entry): Model
68
    {
69
        if ($this->handleRepeatableFiles) {
70
            return $this->handleRepeatableFiles($entry);
71
        }
72
73
        $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...
74
75
        return $entry;
76
    }
77
78
    public function retrieveUploadedFiles(Model $entry): Model
79
    {
80
        if ($this->handleRepeatableFiles) {
81
            return $this->retrieveRepeatableFiles($entry);
82
        }
83
84
        return $this->retrieveFiles($entry);
85
    }
86
87
    public function deleteUploadedFiles(Model $entry): void
88
    {
89
        if ($this->deleteWhenEntryIsDeleted) {
90
            if (! in_array(SoftDeletes::class, class_uses_recursive($entry), true)) {
91
                $this->performFileDeletion($entry);
92
93
                return;
94
            }
95
96
            if ($entry->isForceDeleting() === true) {
97
                $this->performFileDeletion($entry);
98
            }
99
        }
100
    }
101
102
    /*******************************
103
     * Getters
104
     *******************************/
105
    public function getName(): string
106
    {
107
        return $this->name;
108
    }
109
110
    public function getDisk(): string
111
    {
112
        return $this->disk;
113
    }
114
115
    public function getPath(): string
116
    {
117
        return $this->path;
118
    }
119
120
    public function useTemporaryUrl(): bool
121
    {
122
        return $this->useTemporaryUrl;
123
    }
124
125
    public function getExpirationTimeInMinutes(): int
126
    {
127
        return $this->temporaryUrlExpirationTimeInMinutes;
128
    }
129
130
    public function shouldDeleteFiles(): bool
131
    {
132
        return $this->deleteWhenEntryIsDeleted;
133
    }
134
135
    public function getIdentifier(): string
136
    {
137
        if ($this->handleRepeatableFiles) {
138
            return $this->repeatableContainerName.'_'.$this->name;
139
        }
140
141
        return $this->name;
142
    }
143
144
    public function canHandleMultipleFiles(): bool
145
    {
146
        return $this->handleMultipleFiles;
147
    }
148
149
    /*******************************
150
     * Setters - fluently configure the uploader
151
     *******************************/
152
    public function multiple(): self
153
    {
154
        $this->handleMultipleFiles = true;
155
156
        return $this;
157
    }
158
159
    public function relationship(bool $isRelationship): self
160
    {
161
        $this->isRelationship = $isRelationship;
162
163
        return $this;
164
    }
165
166
    /*******************************
167
     * Default implementation functions
168
     *******************************/
169
    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

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