Passed
Push — master ( 00089d...cdf2c0 )
by Philippe
08:10 queued 12s
created

HasFragments::saveFragment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Thinktomorrow\Chief\Fragments;
4
5
use Illuminate\Support\Collection;
6
7
trait HasFragments
8
{
9
    public function getFragments(string $key): Collection
10
    {
11
        return $this->fragments()->where('key', $key)->orderBy('order', 'ASC')->get();
12
    }
13
14
    /**
15
     * Save a single fragment and return the record id
16
     *
17
     * @param Fragment $fragment
18
     * @return int
19
     */
20
    public function saveFragment(Fragment $fragment, int $order): int
21
    {
22
        $values = array_merge([
23
            'key' => $fragment->getKey(), 'order' => $order,
24
        ], $fragment->getValues());
25
26
        if ($fragment->hasModelId()) {
27
            $model =FragmentModel::find($fragment->getModelId());
28
            $model->update($values);
29
30
            return $model->id;
31
        }
32
33
        return $this->fragments()->create($values)->id;
34
    }
35
36
    public function removeAllFragments(string $key, array $excludedFragmentIds): void
37
    {
38
        $this->fragments()->where('key', $key)->whereNotIn('id', $excludedFragmentIds)->delete();
39
    }
40
41
    public function fragments()
42
    {
43
        return $this->morphMany(FragmentModel::class, 'owner');
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

43
        return $this->/** @scrutinizer ignore-call */ morphMany(FragmentModel::class, 'owner');
Loading history...
44
    }
45
}
46