GetFilesAndImages::getFirstImageAttribute()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 4
eloc 7
c 3
b 1
f 1
nc 1
nop 0
dl 0
loc 15
rs 10
1
<?php
2
3
namespace Larrock\Core\Traits;
4
5
use Cache;
6
use Spatie\MediaLibrary\Models\Media;
7
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
8
9
trait GetFilesAndImages
10
{
11
    use HasMediaTrait;
12
13
    /**
14
     * @param Media|null $media
15
     * @throws \Spatie\Image\Exceptions\InvalidManipulation
16
     */
17
    public function registerMediaConversions(Media $media = null)
0 ignored issues
show
Unused Code introduced by
The parameter $media 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

17
    public function registerMediaConversions(/** @scrutinizer ignore-unused */ Media $media = null)

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...
18
    {
19
        $this->addMediaConversion('110x110')
20
            ->height(110)->width(110)
21
            ->performOnCollections('images');
22
23
        $this->addMediaConversion('140x140')
24
            ->height(140)->width(140)
25
            ->performOnCollections('images');
26
27
        if ($this->config->customMediaConversions) {
28
            foreach ($this->config->customMediaConversions as $conversion) {
29
                $explode = explode('x', $conversion);
30
                $this->addMediaConversion($conversion)
31
                    ->height($explode[0])->width($explode[1])
32
                    ->performOnCollections('images');
33
            }
34
        }
35
    }
36
37
    public function getFiles()
38
    {
39
        return $this->hasMany('Spatie\MediaLibrary\Models\Media', 'model_id', 'id')
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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
        return $this->/** @scrutinizer ignore-call */ hasMany('Spatie\MediaLibrary\Models\Media', 'model_id', 'id')
Loading history...
40
            ->where([['model_type', '=', $this->config->model], ['collection_name', '=', 'files']])->orderBy('order_column', 'DESC');
41
    }
42
43
    public function getImages()
44
    {
45
        return $this->hasMany('Spatie\MediaLibrary\Models\Media', 'model_id', 'id')
46
            ->where([['model_type', '=', $this->config->model], ['collection_name', '=', 'images']])->orderBy('order_column', 'DESC');
47
    }
48
49
    public function getFirstImage()
50
    {
51
        return $this->hasOne('Spatie\MediaLibrary\Models\Media', 'model_id', 'id')
0 ignored issues
show
Bug introduced by
It seems like hasOne() 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

51
        return $this->/** @scrutinizer ignore-call */ hasOne('Spatie\MediaLibrary\Models\Media', 'model_id', 'id')
Loading history...
52
            ->where([['model_type', '=', $this->config->model], ['collection_name', '=', 'images']])->orderBy('order_column', 'DESC');
53
    }
54
55
    public function getFirstImageAttribute()
56
    {
57
        $value = Cache::rememberForever(sha1('image_f_category'.$this->id.'_'.$this->config->model), function () {
58
            if ($get_image = $this->getMedia('images')->sortByDesc('order_column')->first()) {
59
                return $get_image->getUrl();
60
            }
61
62
            if (method_exists($this, 'getCategoryActive') && $this->getCategoryActive) {
63
                return $this->getCategoryActive->first()->first_image;
64
            }
65
66
            return '/_assets/_front/_images/empty_big.png';
67
        });
68
69
        return $value;
70
    }
71
72
    public function getFirstImage110Attribute()
73
    {
74
        $value = Cache::rememberForever(sha1('image_f110_category'.$this->id.'_'.$this->config->model), function () {
75
            if ($get_image = $this->getMedia('images')->sortByDesc('order_column')->first()) {
76
                return $get_image->getUrl('110x110');
77
            }
78
79
            if (method_exists($this, 'getCategoryActive') && $this->getCategoryActive) {
80
                return $this->getCategoryActive->first()->first_image_110;
81
            }
82
83
            return '/_assets/_front/_images/empty_big.png';
84
        });
85
86
        return $value;
87
    }
88
89
    public function getFirstImage140Attribute()
90
    {
91
        $value = Cache::rememberForever(sha1('image_f140_category'.$this->id.'_'.$this->config->model), function () {
92
            if ($get_image = $this->getMedia('images')->sortByDesc('order_column')->first()) {
93
                return $get_image->getUrl('140x140');
94
            }
95
96
            if (method_exists($this, 'getCategoryActive') && $this->getCategoryActive) {
97
                return $this->getCategoryActive->first()->first_image_140;
98
            }
99
100
            return '/_assets/_front/_images/empty_big.png';
101
        });
102
103
        return $value;
104
    }
105
}
106