toMediaCollection()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 26
rs 9.4555
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;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\Medi...ctions\FileAdderFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method createForPendingMedia() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
                $fileAdder = app(FileAdderFactory::class)->/** @scrutinizer ignore-call */ createForPendingMedia($this->model, $pendingMedia);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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