PendingMediaItem::getCustomProperties()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Spatie\MediaLibraryPro\Dto;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
use Spatie\MediaLibraryPro\Models\TemporaryUpload;
9
10
class PendingMediaItem
11
{
12
    public TemporaryUpload $temporaryUpload;
13
    public string $name;
14
    public int $order;
15
    public array $customProperties;
16
    public ?string $fileName;
17
18
    public static function createFromArray(array $pendingMediaItems): Collection
19
    {
20
        return collect($pendingMediaItems)
21
            ->map(fn (array $uploadAttributes) => new static(
22
                $uploadAttributes['uuid'],
23
                $uploadAttributes['name'] ?? '',
24
                $uploadAttributes['order'] ?? 0,
25
                $uploadAttributes['custom_properties'] ?? [],
26
                $uploadAttributes['fileName'] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $uploadAttributes['fileName'] ?? null can also be of type null; however, parameter $customHeaders of Spatie\MediaLibraryPro\D...ediaItem::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

26
                /** @scrutinizer ignore-type */ $uploadAttributes['fileName'] ?? null,
Loading history...
27
            ));
28
    }
29
30
    public function __construct(
31
        string $uuid,
32
        string $name,
33
        int $order,
34
        array $customProperties,
35
        array $customHeaders,
0 ignored issues
show
Unused Code introduced by
The parameter $customHeaders is not used and could be removed. ( Ignorable by Annotation )

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

35
        /** @scrutinizer ignore-unused */ array $customHeaders,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
        string $fileName = null
37
    ) {
38
        $temporaryUploadModelClass = config('media-library.temporary_upload_model');
39
40
        if (! $temporaryUpload = $temporaryUploadModelClass::findByMediaUuidInCurrentSession($uuid)) {
41
            throw new Exception('invalid uuid');
42
        }
43
44
        $this->temporaryUpload = $temporaryUpload;
45
46
        $this->name = $name;
47
48
        $this->order = $order;
49
50
        $this->customProperties = $customProperties;
51
52
        $this->fileName = $fileName;
53
    }
54
55
    public function toArray(): array
56
    {
57
        $media = $this->temporaryUpload->getFirstMedia();
58
59
        return [
60
            'uuid' => $media->uuid,
61
            'name' => $this->name,
62
            'order' => $this->order,
63
            'custom_properties' => $this->customProperties,
64
            'size' => $media->size,
65
            'mime' => $media->mime,
66
        ];
67
    }
68
69
    public function getCustomProperties(array $customPropertyNames): array
70
    {
71
        if (! count($customPropertyNames)) {
72
            return $this->customProperties;
73
        }
74
75
        return collect($customPropertyNames)
76
            ->filter(fn (string $customProperty) => Arr::has($this->customProperties, $customProperty))
77
            ->mapWithKeys(fn ($name) => [$name => Arr::get($this->customProperties, $name)])
78
            ->toArray();
79
    }
80
}
81