Passed
Push — 0.6 ( fa70f4...7db85c )
by Philippe
06:36
created

AssetTrait::asset()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 11
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 24
ccs 12
cts 12
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Traits;
4
5
use Thinktomorrow\AssetLibrary\Models\Asset;
6
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
7
use Illuminate\Database\Eloquent\Relations\MorphToMany;
8
9
trait AssetTrait
10
{
11
    use HasMediaTrait;
12
13 51
    public static function bootAssetTrait()
14
    {
15
        static::deleted(function ($model) {
0 ignored issues
show
Bug introduced by
The method deleted() does not exist on Thinktomorrow\AssetLibrary\Traits\AssetTrait. Did you maybe mean deleteMedia()? ( Ignorable by Annotation )

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

15
        static::/** @scrutinizer ignore-call */ 
16
                deleted(function ($model) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
16
            $model->assetRelation->each(function ($asset) use ($model) {
17 1
                $model->assetRelation()->updateExistingPivot($asset->id, ['unused'=> true]);
18 1
            });
19 51
        });
20 51
    }
21
22
    /**
23
     * @return mixed
24
     */
25 51
    public function assetRelation(): MorphToMany
26
    {
27 51
        return $this->morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale', 'order')->orderBy('order');
0 ignored issues
show
Bug introduced by
It seems like morphToMany() 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

27
        return $this->/** @scrutinizer ignore-call */ morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale', 'order')->orderBy('order');
Loading history...
28
    }
29
30 39
    public function asset(string $type = '', ?string $locale = null): ?Asset
31
    {
32 39
        $this->load('assetRelation');
0 ignored issues
show
Bug introduced by
It seems like load() 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

32
        $this->/** @scrutinizer ignore-call */ 
33
               load('assetRelation');
Loading history...
33
34 39
        if ($this->assetRelation->first() === null || $this->assetRelation->first()->pivot === null) {
35 3
            return null;
36
        }
37
38 38
        $assets = $this->assetRelation;
39
40 38
        if($type != '')
41
        {
42 12
            $assets = $this->assetRelation->where('pivot.type', $type);
43
        }
44
45 38
        if ($locale && $assets->count() > 1) {
46 3
            $assets = $assets->where('pivot.locale', $locale);
47
        }
48
49 38
        if ($assets->isEmpty()) {
50 1
            return null;
51
        }
52
53 38
        return $assets->first();
54
    }
55
56 14
    public function assets(string $type = '', string $locale = null)
57
    {
58 14
        $this->load('assetRelation');
59
60 14
        $assets = $this->assetRelation;
61
62 14
        if ($type) {
63 6
            $assets = $assets->where('pivot.type', $type);
64
        }
65
66 14
        if ($locale) {
67
            $assets = $assets->where('pivot.locale', $locale);
68
        }
69
70 14
        return $assets->sortBy('pivot.order');
71
    }
72
73
    // /**
74
    //  * @param string $type
75
    //  * @param string|null $locale
76
    //  * @return string
77
    //  */
78
    // public function getFilename($type = '', $locale = null): string
79
    // {
80
    //     return basename($this->getFileUrl($type, '', $locale));
81
    // }
82
83 28
    public function getFileUrl($type = '', $size = '', $locale = null): ?string
84
    {
85 28
       return optional($this->asset($type, $locale))->url($size);
86
    }
87
88
    /**
89
     * @param string|null $locale
90
     * @return string
91
     */
92
    private function normalizeLocaleString($locale = null): string
93
    {
94
        $locale = $locale ?? config('app.fallback_locale');
95
96
        return $locale;
97
    }
98
}
99