WithMedia::clearMedia()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Spatie\MediaLibraryPro\Http\Livewire\Concerns;
4
5
/** @mixin \Livewire\Component */
6
trait WithMedia
7
{
8
    public function getMediaComponentNames(): array
9
    {
10
        return $this->mediaComponentNames ?? [];
11
    }
12
13
    public function mountWithMedia(): void
14
    {
15
        foreach ($this->getMediaComponentNames() as $mediaComponentName) {
16
            $this->$mediaComponentName = null;
17
        }
18
    }
19
20
    public function hydrateWithMedia()
21
    {
22
        foreach ($this->getMediaComponentNames() as $mediaComponent) {
23
            $this->listeners["$mediaComponent:mediaChanged"] = 'onMediaChanged';
0 ignored issues
show
Bug Best Practice introduced by
The property listeners does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
        }
25
    }
26
27
    public function onMediaChanged($name, $media): void
28
    {
29
        $media = $this->makeSureCustomPropertiesUseRightCasing($media);
30
31
        $this->$name = $media;
32
    }
33
34
    public function renderingWithMedia(): void
35
    {
36
        $errorBag = $this->getErrorBag();
0 ignored issues
show
Bug introduced by
It seems like getErrorBag() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        /** @scrutinizer ignore-call */ 
37
        $errorBag = $this->getErrorBag();
Loading history...
37
38
        foreach ($this->getMediaComponentNames() as $mediaComponentName) {
39
            $this->emit("$mediaComponentName:mediaComponentValidationErrors", $mediaComponentName, $errorBag->toArray());
0 ignored issues
show
Bug introduced by
It seems like emit() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

39
            $this->/** @scrutinizer ignore-call */ 
40
                   emit("$mediaComponentName:mediaComponentValidationErrors", $mediaComponentName, $errorBag->toArray());
Loading history...
40
        }
41
    }
42
43
    public function clearMedia($mediaComponentNames = null)
44
    {
45
        if (is_null($mediaComponentNames)) {
46
            $mediaComponentNames = $this->getMediaComponentNames();
47
        }
48
49
        if (is_string($mediaComponentNames)) {
50
            $mediaComponentNames = [$mediaComponentNames];
51
        }
52
53
        foreach ($mediaComponentNames as $mediaComponentName) {
54
            $this->emit("$mediaComponentName:clearMedia", $mediaComponentName);
55
56
            $this->$mediaComponentName = [];
57
        }
58
    }
59
60
    protected function makeSureCustomPropertiesUseRightCasing(array $media): array
61
    {
62
        $media = collect($media)
63
            ->map(function (array $mediaItemAttributes) {
64
                if (! isset($mediaItemAttributes['custom_properties']) && isset($mediaItemAttributes['customProperties'])) {
65
                    $mediaItemAttributes['custom_properties'] = $mediaItemAttributes['customProperties'];
66
                    unset($mediaItemAttributes['customProperties']);
67
                }
68
69
                return $mediaItemAttributes;
70
            })
71
            ->toArray();
72
73
        return $media;
74
    }
75
}
76