Passed
Push — 1.2 ( 2544d0...f6cea4 )
by Quentin
07:12
created

HasRelated::saveRelated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 22
rs 9.7
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
use A17\Twill\Models\RelatedItem;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Collection;
9
10
trait HasRelated
11
{
12
    protected $relatedCache;
13
14
    public function relatedItems()
15
    {
16
        return $this->morphMany(RelatedItem::class, 'subject')->orderBy('position');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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

16
        return $this->/** @scrutinizer ignore-call */ morphMany(RelatedItem::class, 'subject')->orderBy('position');
Loading history...
17
    }
18
19
    public function getRelated($browser_name)
20
    {
21
        if (!isset($this->relatedCache[$browser_name]) || $this->relatedCache[$browser_name] === null) {
22
            $this->loadRelated($browser_name);
23
        }
24
25
        return $this->relatedCache[$browser_name];
26
    }
27
28
    public function loadRelated($browser_name)
29
    {
30
        if (!isset($this->relatedItems)) {
0 ignored issues
show
Bug introduced by
The property relatedItems does not exist on A17\Twill\Models\Behaviors\HasRelated. Did you mean relatedCache?
Loading history...
31
            $this->load('relatedItems');
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

31
            $this->/** @scrutinizer ignore-call */ 
32
                   load('relatedItems');
Loading history...
32
        }
33
34
        return $this->relatedCache[$browser_name] = $this->relatedItems
35
            ->where('browser_name', $browser_name)
36
            ->map(function ($item) {
37
                return $item->related;
38
            });
39
    }
40
41
    public function saveRelated($items, $browser_name)
42
    {
43
        RelatedItem::where([
44
            'browser_name' => $browser_name,
45
            'subject_id' => $this->getKey(),
0 ignored issues
show
Bug introduced by
It seems like getKey() 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

45
            'subject_id' => $this->/** @scrutinizer ignore-call */ getKey(),
Loading history...
46
            'subject_type' => $this->getMorphClass(),
0 ignored issues
show
Bug introduced by
It seems like getMorphClass() 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

46
            'subject_type' => $this->/** @scrutinizer ignore-call */ getMorphClass(),
Loading history...
47
        ])->delete();
48
49
        $position = 1;
50
51
        Collection::make($items)->map(function ($item) {
52
            return Arr::only($item, ['endpointType', 'id']);
53
        })->each(function ($values) use ($browser_name, &$position) {
54
            RelatedItem::create([
55
                'subject_id' => $this->getKey(),
56
                'subject_type' => $this->getMorphClass(),
57
                'related_id' => $values['id'],
58
                'related_type' => $values['endpointType'],
59
                'browser_name' => $browser_name,
60
                'position' => $position,
61
            ]);
62
            $position++;
63
        });
64
    }
65
}
66