Passed
Push — ft/fragments ( 6fc2e4...3b32df )
by Ben
11:43
created

ManagesFragments   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 1
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A saveFragmentFields() 0 25 4
A saveFragmentImageFields() 0 3 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Fragments;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\Chief\Fields\Types\FileField;
7
use Thinktomorrow\Chief\Fields\Types\ImageField;
8
use Thinktomorrow\Chief\Media\Application\FileFieldHandler;
9
use Thinktomorrow\Chief\Media\Application\ImageFieldHandler;
10
11
trait ManagesFragments
12
{
13
    public function saveFragmentFields(FragmentField $fragmentField, Request $request)
14
    {
15
        $payload = $request->input($fragmentField->getDottedName(), []);
16
        $imagePayload = $request->input('images.'.$fragmentField->getDottedName(), []);
17
18
        // Compose Fragment instances for each payload entry
19
        $fragments = array_map(function($fragmentPayload) use($fragmentField){
20
            return Fragment::fromRequestPayload($fragmentField->getKey(), $fragmentPayload); // (new Fragment($field->getKey(), $fragmentPayload));
21
        }, $payload);
22
23
        // remove all dead fragments
24
        $this->existingModel()->removeAllFragments($fragmentField->getKey(), array_map(function (Fragment $fragment) {
0 ignored issues
show
Bug introduced by
It seems like existingModel() 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
        $this->/** @scrutinizer ignore-call */ 
25
               existingModel()->removeAllFragments($fragmentField->getKey(), array_map(function (Fragment $fragment) {
Loading history...
25
            return $fragment->hasModelId() ? $fragment->getModelId() : null;
26
        }, $fragments));
27
28
        // Save each fragment
29
        foreach($fragments as $i => $fragment) {
30
            $modelId = $this->existingModel()->saveFragment($fragment, $i);
31
32
            // Attach any asset
33
            if(isset($imagePayload[$i])) {
34
                $fieldKey = key($imagePayload[$i]);
35
                $imageField = $this->fields()->keyed($fragmentField->getKey())->first()->getFields()->keyed($fieldKey)->first();
0 ignored issues
show
Bug introduced by
It seems like fields() 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

35
                $imageField = $this->/** @scrutinizer ignore-call */ fields()->keyed($fragmentField->getKey())->first()->getFields()->keyed($fieldKey)->first();
Loading history...
36
37
                $this->saveFragmentImageFields($imageField, $imagePayload[$i][$fieldKey], FragmentModel::find($modelId), $request);
38
            }
39
        }
40
41
    }
42
43
    private function saveFragmentImageFields(ImageField $field, array $values, FragmentModel $model, Request $request)
44
    {
45
        app(ImageFieldHandler::class)->handle($model, $field, $values, $request);
46
    }
47
}
48