Passed
Push — dependabot/composer/orchestra/... ( 137c82...0b1a28 )
by
unknown
04:18
created

AssetTrait::bootAssetTrait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary;
4
5
use Illuminate\Database\Eloquent\Relations\MorphToMany;
6
use Illuminate\Support\Collection;
7
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
8
9
trait AssetTrait
10
{
11
    use HasMediaTrait;
12
13 53
    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\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 53
        });
20 53
    }
21
22 53
    public function assetRelation(): MorphToMany
23
    {
24 53
        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

24
        return $this->/** @scrutinizer ignore-call */ morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale', 'order')->orderBy('order');
Loading history...
25
    }
26
27 36
    public function asset(string $type, ?string $locale = null): ?Asset
28
    {
29 36
        return $this->assets($type, $locale)->first();
30
    }
31
32 46
    public function assets(?string $type = null, ?string $locale = null): Collection
33
    {
34 46
        $assets = $this->assetRelation;
35
36 46
        if ($type) {
37 46
            $assets = $assets->where('pivot.type', $type);
38
        }
39
40 46
        $locale = $locale ?? app()->getLocale();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

40
        $locale = $locale ?? app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
41
42
        $results = $assets->filter(function($asset) use($locale){
43 44
            return $asset->pivot->locale == $locale;
44 46
        });
45
46 46
        if($this->getUseAssetFallbackLocale() && $locale != $this->getAssetFallbackLocale() && $results->isEmpty()) {
47
            $results = $assets->filter(function($asset){
48 2
                return $asset->pivot->locale == $this->getAssetFallbackLocale();
49 2
            });
50
        }
51 46
        return $results->sortBy('pivot.order');
52
    }
53
54 46
    protected function getUseAssetFallbackLocale(): bool
55
    {
56 46
        return $this->useAssetFallbackLocale ?? config('thinktomorrow.assetlibrary.use_fallback_locale', false);
57
    }
58
59 2
    protected function getAssetFallbackLocale(): string
60
    {
61 2
        return $this->assetFallbackLocale ?? config('thinktomorrow.assetlibrary.fallback_locale');
62
    }
63
}
64