Skip to content

Commit 1ba771b

Browse files
committed
improve code readability
1 parent 89fabec commit 1ba771b

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

src/app/Library/Uploaders/Support/RegisterUploadEvents.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ private function setupModelEvents(string $model, UploaderInterface $uploader): v
127127
app('crud')->entry = $uploader->retrieveUploadedFiles(app('crud')->entry);
128128
} else {
129129
// the retrieve model may differ from the deleting and saving models because retrieved event
130-
// is not called in pivot models when eager loading the relations.
131-
$retrieveModel = $this->getRetrieveModel($model, $uploader);
130+
// is not called in pivot models when loading the relations.
131+
$retrieveModel = $this->getModelForRetrieveEvent($model, $uploader);
132132

133133
$retrieveModel::retrieved(function ($entry) use ($uploader) {
134134
if ($entry->translationEnabled()) {
@@ -189,16 +189,14 @@ private function getSubfieldModel(array $subfield, UploaderInterface $uploader)
189189
return $subfield['baseModel'] ?? get_class(app('crud')->getModel());
190190
}
191191

192-
switch($subfield['relation_type']) {
193-
case 'BelongsToMany':
194-
case 'MorphToMany':
195-
return app('crud')->getModel()->{$subfield['baseEntity']}()->getPivotClass();
192+
if(in_array($subfield['relation_type'], ['BelongsToMany', 'MorphToMany'])) {
193+
return app('crud')->getModel()->{$subfield['baseEntity']}()->getPivotClass();
196194
}
197195

198196
return $subfield['baseModel'];
199197
}
200198

201-
private function getRetrieveModel(string $model, UploaderInterface $uploader)
199+
private function getModelForRetrieveEvent(string $model, UploaderInterface $uploader)
202200
{
203201
if (! $uploader->isRelationship()) {
204202
return $model;

src/app/Library/Uploaders/Support/Traits/HandleRepeatableUploads.php

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -150,43 +150,7 @@ private function getUploaderFieldSubfields()
150150
private function deleteRepeatableFiles(Model $entry): void
151151
{
152152
if ($this->isRelationship) {
153-
switch($this->getRepeatableRelationType()) {
154-
case 'BelongsToMany':
155-
case 'MorphToMany':
156-
$pivotAttributes = $entry->getAttributes();
157-
$connectedPivot = $entry->pivotParent->{$this->getRepeatableContainerName()}->where(function ($item) use ($pivotAttributes) {
158-
$itemPivotAttributes = $item->pivot->only(array_keys($pivotAttributes));
159-
160-
return $itemPivotAttributes === $pivotAttributes;
161-
})->first();
162-
163-
if (! $connectedPivot) {
164-
return;
165-
}
166-
167-
$files = $connectedPivot->getOriginal()['pivot_'.$this->getAttributeName()];
168-
169-
if (! $files) {
170-
return;
171-
}
172-
173-
if (is_array($files)) {
174-
foreach ($files as $value) {
175-
$value = Str::start($value, $this->getPath());
176-
Storage::disk($this->getDisk())->delete($value);
177-
}
178-
179-
return;
180-
}
181-
182-
$value = Str::start($files, $this->getPath());
183-
Storage::disk($this->getDisk())->delete($value);
184-
185-
return;
186-
}
187-
188-
$this->deleteFiles($entry);
189-
153+
$this->deleteRelationshipFiles($entry);
190154
return;
191155
}
192156

@@ -272,4 +236,42 @@ private function getValuesWithPathStripped(array|string|null $item, UploaderInte
272236

273237
return isset($uploadedValues) ? Str::after($uploadedValues, $upload->getPath()) : null;
274238
}
239+
240+
private function deleteRelationshipFiles(Model $entry) : void
241+
{
242+
if (in_array($this->getRepeatableRelationType(), ['BelongsToMany', 'MorphToMany'])) {
243+
$pivotAttributes = $entry->getAttributes();
244+
$connectedPivot = $entry->pivotParent->{$this->getRepeatableContainerName()}->where(function ($item) use ($pivotAttributes) {
245+
$itemPivotAttributes = $item->pivot->only(array_keys($pivotAttributes));
246+
247+
return $itemPivotAttributes === $pivotAttributes;
248+
})->first();
249+
250+
if (! $connectedPivot) {
251+
return;
252+
}
253+
254+
$files = $connectedPivot->getOriginal()['pivot_'.$this->getAttributeName()];
255+
256+
if (! $files) {
257+
return;
258+
}
259+
260+
if (is_array($files)) {
261+
foreach ($files as $value) {
262+
$value = Str::start($value, $this->getPath());
263+
Storage::disk($this->getDisk())->delete($value);
264+
}
265+
266+
return;
267+
}
268+
269+
$value = Str::start($files, $this->getPath());
270+
Storage::disk($this->getDisk())->delete($value);
271+
272+
return;
273+
}
274+
275+
$this->deleteFiles($entry);
276+
}
275277
}

0 commit comments

Comments
 (0)