Passed
Push — 0.6 ( a96f06...3caceb )
by Philippe
04:03
created

AssetTrait::assetRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
use Thinktomorrow\AssetLibrary\Models\Asset;
9
10
trait AssetTrait
11
{
12
    use HasMediaTrait;
13
14 51
    public static function bootAssetTrait()
15
    {
16
        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

16
        static::/** @scrutinizer ignore-call */ 
17
                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...
17
            $model->assetRelation->each(function ($asset) use ($model) {
18 1
                $model->assetRelation()->updateExistingPivot($asset->id, ['unused'=> true]);
19 1
            });
20 51
        });
21 51
    }
22
23 51
    public function assetRelation(): MorphToMany
24
    {
25 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

25
        return $this->/** @scrutinizer ignore-call */ morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale', 'order')->orderBy('order');
Loading history...
26
    }
27
28 34
    public function asset(string $type, ?string $locale = null): ?Asset
29
    {
30 34
        if ($this->assetRelation->first() === null || $this->assetRelation->first()->pivot === null) {
31 3
            return null;
32
        }
33
34 32
        $assets = $this->assetRelation->where('pivot.type', $type);
35
36 32
        if ($locale && $assets->count() > 1) {
37 3
            $assets = $assets->where('pivot.locale', $locale);
38
        }
39
40 32
        return $assets->first();
41
    }
42
43 17
    public function assets(string $type = '', ?string $locale = null): Collection
44
    {
45 17
        $assets = $this->assetRelation;
46
47 17
        if ($type) {
48 16
            $assets = $assets->where('pivot.type', $type);
49
        }
50
51 17
        if ($locale) {
52 1
            $assets = $assets->where('pivot.locale', $locale);
53
        }
54
55 17
        return $assets->sortBy('pivot.order');
56
    }
57
}
58