HasMediaExtended::getLastMedia()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Traits;
4
5
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\HasMedia\HasMediaTrait 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...
6
use Spatie\MediaLibrary\Models\Media;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\Models\Media 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...
7
8
/**
9
 * HasMediaExtended Trait.
10
 */
11
trait HasMediaExtended
12
{
13
    use HasMediaTrait;
14
15
    /**
16
     * Get the last media item of a media collection.
17
     *
18
     * @return Media|null
19
     */
20
    public function getLastMedia(string $collectionName = 'default', array $filters = [])
21
    {
22
        $media = $this->getMedia($collectionName, $filters);
23
24
        return $media->sortByDesc('created_at')->first();
25
    }
26
27
    /*
28
     * Get the url of the image for the given conversionName
29
     * for first media for the given collectionName.
30
     * If no profile is given, return the source's url.
31
     */
32
    public function getLastMediaUrl(string $collectionName = 'default', string $conversionName = ''): string
33
    {
34
        $media = $this->getLastMedia($collectionName);
35
36
        if (! $media) {
37
            return '';
38
        }
39
40
        return $media->getUrl($conversionName);
41
    }
42
43
    /**
44
     * Register Media Conversion.
45
     *
46
     * @param \Spatie\MediaLibrary\Models\Media $media
47
     */
48
    public function registerMediaConversions(Media $media = null)
49
    {
50
        $dimensions = $this->getMediaDimensions();
51
52
        foreach ($dimensions as $dimension => $collection) {
53
            $this->addMediaConversion($collection)
54
                ->width($dimension)
55
                ->height($dimension)
56
                ->performOnCollections($collection);
57
        }
58
    }
59
60
    /**
61
     * List of dimension and it's collection name.
62
     */
63
    public function getMediaDimensions(): array
64
    {
65
        return [
66
            24  => 'avatar',
67
            32  => 'avatar',
68
            64  => 'avatar',
69
            128 => 'avatar',
70
            256 => 'avatar',
71
            512 => 'avatar',
72
        ];
73
    }
74
}
75