We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 61 |
Total Lines | 314 |
Duplicated Lines | 0 % |
Changes | 17 | ||
Bugs | 10 | Features | 0 |
Complex classes like Uploader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Uploader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class Uploader implements UploaderInterface |
||
15 | { |
||
16 | use HandleFileNaming; |
||
17 | use HandleRepeatableUploads; |
||
|
|||
18 | |||
19 | private string $name; |
||
20 | |||
21 | private string $disk = 'public'; |
||
22 | |||
23 | private string $path = ''; |
||
24 | |||
25 | private bool $handleMultipleFiles = false; |
||
26 | |||
27 | private bool $deleteWhenEntryIsDeleted = true; |
||
28 | |||
29 | private bool|string $attachedToFakeField = false; |
||
30 | |||
31 | /** |
||
32 | * Cloud disks have the ability to generate temporary URLs to files, should we do it? |
||
33 | */ |
||
34 | private bool $useTemporaryUrl = false; |
||
35 | |||
36 | /** |
||
37 | * When using temporary urls, define the time that the url will be valid. |
||
38 | */ |
||
39 | private int $temporaryUrlExpirationTimeInMinutes = 1; |
||
40 | |||
41 | /** |
||
42 | * Indicates if the upload is relative to a relationship field/column. |
||
43 | */ |
||
44 | private bool $isRelationship = false; |
||
45 | |||
46 | /** |
||
47 | * When previous files are updated, we need to keep track of them so that we don't add deleted files to the new list. |
||
48 | */ |
||
49 | private $updatedPreviousFiles = null; |
||
50 | |||
51 | public function __construct(array $crudObject, array $configuration) |
||
52 | { |
||
53 | $this->name = $crudObject['name']; |
||
54 | $this->disk = $configuration['disk'] ?? $crudObject['disk'] ?? $this->disk; |
||
55 | $this->path = $this->getPathFromConfiguration($crudObject, $configuration); |
||
56 | $this->attachedToFakeField = isset($crudObject['fake']) && $crudObject['fake'] ? ($crudObject['store_in'] ?? 'extras') : ($crudObject['store_in'] ?? false); |
||
57 | $this->useTemporaryUrl = $configuration['temporaryUrl'] ?? $this->useTemporaryUrl; |
||
58 | $this->temporaryUrlExpirationTimeInMinutes = $configuration['temporaryUrlExpirationTime'] ?? $this->temporaryUrlExpirationTimeInMinutes; |
||
59 | $this->deleteWhenEntryIsDeleted = $configuration['deleteWhenEntryIsDeleted'] ?? $this->deleteWhenEntryIsDeleted; |
||
60 | $this->fileNamer = is_callable($configuration['fileNamer'] ?? null) ? $configuration['fileNamer'] : $this->getFileNameGeneratorInstance($configuration['fileNamer'] ?? null); |
||
61 | } |
||
62 | |||
63 | /******************************* |
||
64 | * Static methods |
||
65 | *******************************/ |
||
66 | public static function for(array $crudObject, array $definition): UploaderInterface |
||
67 | { |
||
68 | return new static($crudObject, $definition); |
||
69 | } |
||
70 | |||
71 | /******************************* |
||
72 | * public methods - event handler methods |
||
73 | *******************************/ |
||
74 | public function storeUploadedFiles(Model $entry): Model |
||
75 | { |
||
76 | if ($this->handleRepeatableFiles) { |
||
77 | return $this->handleRepeatableFiles($entry); |
||
78 | } |
||
79 | |||
80 | if ($this->attachedToFakeField) { |
||
81 | $fakeFieldValue = $entry->{$this->attachedToFakeField}; |
||
82 | $fakeFieldValue = is_string($fakeFieldValue) ? json_decode($fakeFieldValue, true) : (array) $fakeFieldValue; |
||
83 | $fakeFieldValue[$this->getAttributeName()] = $this->uploadFiles($entry); |
||
84 | |||
85 | $entry->{$this->attachedToFakeField} = isset($entry->getCasts()[$this->attachedToFakeField]) ? $fakeFieldValue : json_encode($fakeFieldValue); |
||
86 | |||
87 | return $entry; |
||
88 | } |
||
89 | |||
90 | $entry->{$this->getAttributeName()} = $this->uploadFiles($entry); |
||
91 | |||
92 | return $entry; |
||
93 | } |
||
94 | |||
95 | public function retrieveUploadedFiles(Model $entry): Model |
||
96 | { |
||
97 | if ($this->handleRepeatableFiles) { |
||
98 | return $this->retrieveRepeatableFiles($entry); |
||
99 | } |
||
100 | |||
101 | return $this->retrieveFiles($entry); |
||
102 | } |
||
103 | |||
104 | public function deleteUploadedFiles(Model $entry): void |
||
105 | { |
||
106 | if ($this->deleteWhenEntryIsDeleted) { |
||
107 | if (! in_array(SoftDeletes::class, class_uses_recursive($entry), true)) { |
||
108 | $this->performFileDeletion($entry); |
||
109 | |||
110 | return; |
||
111 | } |
||
112 | |||
113 | if ($entry->isForceDeleting() === true) { |
||
114 | $this->performFileDeletion($entry); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /******************************* |
||
120 | * Getters |
||
121 | *******************************/ |
||
122 | public function getName(): string |
||
123 | { |
||
124 | return $this->name; |
||
125 | } |
||
126 | |||
127 | public function getAttributeName(): string |
||
128 | { |
||
129 | return Str::afterLast($this->name, '.'); |
||
130 | } |
||
131 | |||
132 | public function getDisk(): string |
||
133 | { |
||
134 | return $this->disk; |
||
135 | } |
||
136 | |||
137 | public function getPath(): string |
||
138 | { |
||
139 | return $this->path; |
||
140 | } |
||
141 | |||
142 | public function useTemporaryUrl(): bool |
||
143 | { |
||
144 | return $this->useTemporaryUrl; |
||
145 | } |
||
146 | |||
147 | public function getExpirationTimeInMinutes(): int |
||
148 | { |
||
149 | return $this->temporaryUrlExpirationTimeInMinutes; |
||
150 | } |
||
151 | |||
152 | public function shouldDeleteFiles(): bool |
||
153 | { |
||
154 | return $this->deleteWhenEntryIsDeleted; |
||
155 | } |
||
156 | |||
157 | public function getIdentifier(): string |
||
158 | { |
||
159 | if ($this->handleRepeatableFiles) { |
||
160 | return $this->repeatableContainerName.'_'.$this->name; |
||
161 | } |
||
162 | |||
163 | return $this->name; |
||
164 | } |
||
165 | |||
166 | public function getNameForRequest(): string |
||
167 | { |
||
168 | return $this->repeatableContainerName ?? $this->name; |
||
169 | } |
||
170 | |||
171 | public function canHandleMultipleFiles(): bool |
||
172 | { |
||
173 | return $this->handleMultipleFiles; |
||
174 | } |
||
175 | |||
176 | public function isRelationship(): bool |
||
177 | { |
||
178 | return $this->isRelationship; |
||
179 | } |
||
180 | |||
181 | public function getPreviousFiles(Model $entry): mixed |
||
182 | { |
||
183 | if (! $this->attachedToFakeField) { |
||
184 | return $this->getOriginalValue($entry); |
||
185 | } |
||
186 | |||
187 | $value = $this->getOriginalValue($entry, $this->attachedToFakeField); |
||
188 | $value = is_string($value) ? json_decode($value, true) : (array) $value; |
||
189 | |||
190 | return $value[$this->getAttributeName()] ?? null; |
||
191 | } |
||
192 | |||
193 | /******************************* |
||
194 | * Setters - fluently configure the uploader |
||
195 | *******************************/ |
||
196 | public function multiple(): self |
||
197 | { |
||
198 | $this->handleMultipleFiles = true; |
||
199 | |||
200 | return $this; |
||
201 | } |
||
202 | |||
203 | public function relationship(bool $isRelationship): self |
||
204 | { |
||
205 | $this->isRelationship = $isRelationship; |
||
206 | |||
207 | return $this; |
||
208 | } |
||
209 | |||
210 | /******************************* |
||
211 | * Default implementation functions |
||
212 | *******************************/ |
||
213 | public function uploadFiles(Model $entry, $values = null) |
||
215 | } |
||
216 | |||
217 | private function retrieveFiles(Model $entry): Model |
||
218 | { |
||
219 | $value = $entry->{$this->getAttributeName()}; |
||
220 | |||
221 | if ($this->handleMultipleFiles) { |
||
222 | if (! isset($entry->getCasts()[$this->getName()]) && is_string($value)) { |
||
223 | $entry->{$this->getAttributeName()} = json_decode($value, true); |
||
224 | } |
||
225 | |||
226 | return $entry; |
||
227 | } |
||
228 | |||
229 | if ($this->attachedToFakeField) { |
||
230 | $values = $entry->{$this->attachedToFakeField}; |
||
231 | $values = is_string($values) ? json_decode($values, true) : (array) $values; |
||
232 | |||
233 | $values[$this->getAttributeName()] = isset($values[$this->getAttributeName()]) ? Str::after($values[$this->getAttributeName()], $this->path) : null; |
||
234 | $entry->{$this->attachedToFakeField} = json_encode($values); |
||
235 | |||
236 | return $entry; |
||
237 | } |
||
238 | |||
239 | $entry->{$this->getAttributeName()} = Str::after($value, $this->path); |
||
240 | |||
241 | return $entry; |
||
242 | } |
||
243 | |||
244 | private function deleteFiles(Model $entry) |
||
245 | { |
||
246 | $values = $entry->{$this->getAttributeName()}; |
||
247 | |||
248 | if ($values === null) { |
||
249 | return; |
||
250 | } |
||
251 | |||
252 | if ($this->handleMultipleFiles) { |
||
253 | // ensure we have an array of values when field is not casted in model. |
||
254 | if (! isset($entry->getCasts()[$this->name]) && is_string($values)) { |
||
255 | $values = json_decode($values, true); |
||
256 | } |
||
257 | foreach ($values ?? [] as $value) { |
||
258 | $value = Str::start($value, $this->path); |
||
259 | Storage::disk($this->disk)->delete($value); |
||
260 | } |
||
261 | |||
262 | return; |
||
263 | } |
||
264 | |||
265 | $values = Str::start($values, $this->path); |
||
266 | Storage::disk($this->disk)->delete($values); |
||
267 | } |
||
268 | |||
269 | private function performFileDeletion(Model $entry) |
||
278 | } |
||
279 | |||
280 | /******************************* |
||
281 | * Private helper methods |
||
282 | *******************************/ |
||
283 | private function getPathFromConfiguration(array $crudObject, array $configuration): string |
||
284 | { |
||
288 | } |
||
289 | |||
290 | private function getOriginalValue(Model $entry, $field = null) |
||
291 | { |
||
292 | if ($this->updatedPreviousFiles !== null) { |
||
293 | return $this->updatedPreviousFiles; |
||
294 | } |
||
295 | |||
307 | } |
||
308 | |||
309 | /******************************* |
||
310 | * Helper methods |
||
311 | *******************************/ |
||
312 | |||
313 | /** |
||
314 | * Given two multidimensional arrays/collections, merge them recursively. |
||
315 | */ |
||
316 | public static function mergeFilesAndValuesRecursive(array|Collection $array1, array|Collection $array2): array|Collection |
||
328 | } |
||
329 | } |
||
330 |