MediaResource::collection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Owowagency\LaravelMedia\Resources;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Resources\Json\JsonResource;
7
use Spatie\MediaLibrary\MediaCollections\Models\Media;
8
9
class MediaResource extends JsonResource
10
{
11
    /**
12
     * Force the resource to use its own array structure instead of possible
13
     * exceptions.
14
     *
15
     * @var bool
16
     */
17
    public $forceMediaResource = false;
18
19
    /**
20
     * Create a new resource instance.
21
     *
22
     * @param  \Spatie\MediaLibrary\MediaCollections\Models\Media  $resource
23
     * @param  bool  $forceMediaResource
24
     */
25
    public function __construct(Media $resource, bool $forceMediaResource = false)
26
    {
27
        parent::__construct($resource);
28
29
        $this->forceMediaResource = $forceMediaResource;
30
    }
31
32
    /**
33
     * Transform the resource into an array.
34
     *
35
     * @param  \Illuminate\Http\Request  $request
36
     * @return array
37
     */
38
    public function toArray($request): array
39
    {
40
        if (! $this->forceMediaResource && $this->isImage()) {
41
            return (new MediaImageResource($this->resource))->toArray($request);
42
        }
43
44
        return [
45
            'id' => $this->resource->id,
46
            'file_name' => $this->resource->file_name,
47
            'size' => $this->resource->size,
48
            'mime_type' => $this->resource->mime_type,
49
            'url' => $this->getUrl(),
50
        ];
51
    }
52
53
    /**
54
     * Determines if the resource is an image.
55
     *
56
     * @return bool
57
     */
58
    public function isImage(): bool
59
    {
60
        return Str::contains($this->resource->mime_type, 'image');
61
    }
62
63
    /**
64
     * Returns the url. The method of retrieving it varies on the disk it is
65
     * stored on.
66
     *
67
     * @param  string  $conversion
68
     * @return string
69
     */
70
    public function getUrl(string $conversion = ''): string
71
    {
72
        $createTempUrl = config('laravel-media.temporary_urls', false);
73
74
        if (filter_var($createTempUrl, FILTER_VALIDATE_BOOLEAN)) {
75
            $expiration = now()->addMinutes(
76
                config('laravel-media.sign_expiration', 60)
77
            );
78
79
            return $this->resource->getTemporaryUrl($expiration, $conversion);
80
        }
81
82
        return $this->resource->getFullUrl($conversion);
83
    }
84
85
    /**
86
     * Create new anonymous resource collection.
87
     *
88
     * @param  mixed  $resource
89
     * @param  bool  $forceMediaResource
90
     * @return \Owowagency\LaravelMedia\Resources\MediaResourceCollection
91
     */
92
    public static function collection($resource, bool $forceMediaResource = false): MediaResourceCollection
93
    {
94
        return new MediaResourceCollection($resource, $forceMediaResource);
95
    }
96
}
97