|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibraryPro; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Spatie\MediaLibrary\MediaCollections\FileAdderFactory; |
|
|
|
|
|
|
8
|
|
|
use Spatie\MediaLibraryPro\Dto\MediaLibraryRequestItem; |
|
9
|
|
|
use Spatie\MediaLibraryPro\Dto\PendingMediaItem; |
|
10
|
|
|
|
|
11
|
|
|
class PendingMediaLibraryRequestHandler |
|
12
|
|
|
{ |
|
13
|
|
|
protected Collection $mediaLibraryRequestItems; |
|
14
|
|
|
|
|
15
|
|
|
protected Model $model; |
|
16
|
|
|
|
|
17
|
|
|
protected bool $preserveExisting; |
|
18
|
|
|
|
|
19
|
|
|
protected ?array $processCustomProperties = null; |
|
20
|
|
|
|
|
21
|
|
|
protected ?array $customHeaders = null; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(array $mediaLibraryRequestItems, Model $model, bool $preserveExisting) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->mediaLibraryRequestItems = collect($mediaLibraryRequestItems) |
|
26
|
|
|
->map(fn (array $properties) => MediaLibraryRequestItem::fromArray($properties)); |
|
27
|
|
|
|
|
28
|
|
|
$this->model = $model; |
|
29
|
|
|
|
|
30
|
|
|
$this->preserveExisting = $preserveExisting; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function usingName($mediaName): self |
|
34
|
|
|
{ |
|
35
|
|
|
if (is_string($mediaName)) { |
|
36
|
|
|
return $this->usingName(fn () => $mediaName); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$callable = $mediaName; |
|
40
|
|
|
|
|
41
|
|
|
$this->mediaLibraryRequestItems->each(function (MediaLibraryRequestItem $mediaLibraryRequestItem) use ($callable) { |
|
42
|
|
|
$name = $callable($mediaLibraryRequestItem); |
|
43
|
|
|
|
|
44
|
|
|
$mediaLibraryRequestItem->name = $name; |
|
45
|
|
|
}); |
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function usingFileName($fileName): self |
|
51
|
|
|
{ |
|
52
|
|
|
if (is_string($fileName)) { |
|
53
|
|
|
return $this->usingFileName(fn () => $fileName); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$callable = $fileName; |
|
57
|
|
|
|
|
58
|
|
|
$this->mediaLibraryRequestItems->each(function (MediaLibraryRequestItem $mediaLibraryRequestItem) use ($callable) { |
|
59
|
|
|
$fileName = $callable($mediaLibraryRequestItem); |
|
60
|
|
|
|
|
61
|
|
|
$mediaLibraryRequestItem->fileName = $fileName; |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function withCustomProperties(...$customPropertyNames): self |
|
68
|
|
|
{ |
|
69
|
|
|
$this->processCustomProperties = $customPropertyNames; |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function addCustomHeaders(array $customHeaders): self |
|
75
|
|
|
{ |
|
76
|
|
|
$this->customHeaders = $customHeaders; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function toMediaLibrary(string $collectionName = 'default', string $diskName = ''): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->toMediaCollection($collectionName, $diskName); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function toMediaCollection(string $collectionName = 'default', string $diskName = ''): void |
|
87
|
|
|
{ |
|
88
|
|
|
$mediaLibraryRequestHandler = MediaLibraryRequestHandler::createForMediaLibraryRequestItems($this->model, $this->mediaLibraryRequestItems, $collectionName) |
|
89
|
|
|
->updateExistingMedia(); |
|
90
|
|
|
|
|
91
|
|
|
if (! $this->preserveExisting) { |
|
92
|
|
|
$mediaLibraryRequestHandler->deleteObsoleteMedia(); |
|
93
|
|
|
} |
|
94
|
|
|
$mediaLibraryRequestHandler |
|
95
|
|
|
->getPendingMediaItems() |
|
96
|
|
|
->each(function (PendingMediaItem $pendingMedia) use ($diskName, $collectionName) { |
|
97
|
|
|
$fileAdder = app(FileAdderFactory::class)->createForPendingMedia($this->model, $pendingMedia); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
if (! is_null($this->processCustomProperties)) { |
|
100
|
|
|
$fileAdder->withCustomProperties($pendingMedia->getCustomProperties($this->processCustomProperties)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (! is_null($this->customHeaders)) { |
|
104
|
|
|
$fileAdder = $fileAdder->addCustomHeaders($this->customHeaders); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (! is_null($pendingMedia->fileName)) { |
|
108
|
|
|
$fileAdder->setFileName($pendingMedia->fileName); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$fileAdder->toMediaCollection($collectionName, $diskName); |
|
112
|
|
|
}); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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