|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibraryPro; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media; |
|
|
|
|
|
|
8
|
|
|
use Spatie\MediaLibraryPro\Dto\MediaLibraryRequestItem; |
|
9
|
|
|
use Spatie\MediaLibraryPro\Dto\PendingMediaItem; |
|
10
|
|
|
|
|
11
|
|
|
class MediaLibraryRequestHandler |
|
12
|
|
|
{ |
|
13
|
|
|
protected Model $model; |
|
14
|
|
|
|
|
15
|
|
|
protected array $existingUuids; |
|
16
|
|
|
|
|
17
|
|
|
protected Collection $mediaLibraryRequestItems; |
|
18
|
|
|
|
|
19
|
|
|
protected string $collectionName; |
|
20
|
|
|
|
|
21
|
|
|
public static function createForMediaLibraryRequestItems( |
|
22
|
|
|
Model $model, |
|
23
|
|
|
Collection $mediaLibraryRequestItems, |
|
24
|
|
|
string $collectionName |
|
25
|
|
|
): self { |
|
26
|
|
|
return new static($model, $mediaLibraryRequestItems, $collectionName); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function __construct(Model $model, Collection $mediaLibraryRequestItems, string $collectionName) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->model = $model; |
|
32
|
|
|
|
|
33
|
|
|
$this->existingUuids = $this->model->getMedia($collectionName)->pluck('uuid')->toArray(); |
|
34
|
|
|
|
|
35
|
|
|
$this->mediaLibraryRequestItems = $mediaLibraryRequestItems; |
|
36
|
|
|
|
|
37
|
|
|
$this->collectionName = $collectionName; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function updateExistingMedia(): self |
|
41
|
|
|
{ |
|
42
|
|
|
$this |
|
43
|
|
|
->existingMediaLibraryRequestItems() |
|
44
|
|
|
->each(function (MediaLibraryRequestItem $mediaResponseItem) { |
|
45
|
|
|
$this->handleExistingMediaLibraryRequestItem($mediaResponseItem); |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function deleteObsoleteMedia(): self |
|
52
|
|
|
{ |
|
53
|
|
|
$keepUuids = $this->mediaLibraryRequestItems->pluck('uuid')->toArray(); |
|
54
|
|
|
|
|
55
|
|
|
$this->model->getMedia($this->collectionName) |
|
56
|
|
|
->reject(fn (Media $media) => in_array($media->uuid, $keepUuids)) |
|
57
|
|
|
->each(fn (Media $media) => $media->delete()); |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getPendingMediaItems(): Collection |
|
63
|
|
|
{ |
|
64
|
|
|
return $this |
|
65
|
|
|
->newMediaLibraryRequestItems() |
|
66
|
|
|
->map(function (MediaLibraryRequestItem $item) { |
|
67
|
|
|
return new PendingMediaItem( |
|
68
|
|
|
$item->uuid, |
|
69
|
|
|
$item->name, |
|
70
|
|
|
$item->order, |
|
71
|
|
|
$item->customProperties, |
|
72
|
|
|
$item->customHeaders, |
|
73
|
|
|
$item->fileName, |
|
74
|
|
|
); |
|
75
|
|
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function existingMediaLibraryRequestItems(): Collection |
|
79
|
|
|
{ |
|
80
|
|
|
return $this |
|
81
|
|
|
->mediaLibraryRequestItems |
|
82
|
|
|
->filter(fn (MediaLibraryRequestItem $item) => in_array($item->uuid, $this->existingUuids)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function newMediaLibraryRequestItems(): Collection |
|
86
|
|
|
{ |
|
87
|
|
|
return $this |
|
88
|
|
|
->mediaLibraryRequestItems |
|
89
|
|
|
->reject(fn (MediaLibraryRequestItem $item) => in_array($item->uuid, $this->existingUuids)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function handleExistingMediaLibraryRequestItem(MediaLibraryRequestItem $mediaLibraryRequestItem): void |
|
93
|
|
|
{ |
|
94
|
|
|
$mediaModelClass = config('media-library.media_model'); |
|
95
|
|
|
|
|
96
|
|
|
$media = $mediaModelClass::findByUuid($mediaLibraryRequestItem->uuid); |
|
97
|
|
|
|
|
98
|
|
|
$media->update([ |
|
99
|
|
|
'name' => $mediaLibraryRequestItem->name, |
|
100
|
|
|
'custom_properties' => $mediaLibraryRequestItem->customProperties, |
|
101
|
|
|
'order_column' => $mediaLibraryRequestItem->order, |
|
102
|
|
|
]); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths