Passed
Push — ft/fragments ( 6fc2e4...3b32df )
by Ben
11:43
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 saveFragments(string $key, array $fragments): void
42
//    {
43
//        // TEST
44
//        $id = 6;
45
//        $fragments = array_map(function (Fragment $fragment) use (&$id) {
46
//            return $fragment->modelId($id++);
47
//        }, $fragments);
48
//        //END TEST
49
//
50
//        // Reset all fragments in db that are no longer present
51
//        $this->removeDeadFragments($key, $fragments);
52
//
53
//        // order is set by order of array
54
//        $i = 0;
55
//
56
//        array_map(function (Fragment $fragment) use (&$i) {
57
//
58
//            // Fragment already exists
59
//            if ($fragment->hasModelId())
60
//            {
61
//                FragmentModel::find($fragment->getModelId())->update(array_merge([
62
//                    'key'   => $fragment->getKey(),
63
//                    'order' => $i++,
64
//                ], $fragment->getValues()));
65
//            } else
66
//            {
67
//                $this->fragments()->create(array_merge([
68
//                    'key'   => $fragment->getKey(),
69
//                    'order' => $i++,
70
//                ], $fragment->getValues()));
71
//            }
72
//
73
//        }, $fragments);
74
//    }
75
76
    protected function fragments()
77
    {
78
        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

78
        return $this->/** @scrutinizer ignore-call */ morphMany(FragmentModel::class, 'owner');
Loading history...
79
    }
80
}
81