Passed
Push — ft/fragments ( b30041...2bbc06 )
by Ben
07:30
created

FragmentField   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 36
c 2
b 0
f 0
dl 0
loc 83
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fields() 0 5 1
A getFields() 0 3 1
A make() 0 4 1
B getFragments() 0 43 8
A getDuplicatableFields() 0 11 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Thinktomorrow\Chief\Fragments;
5
6
use Thinktomorrow\Chief\Fields\Fields;
7
use Thinktomorrow\Chief\Fields\Types\AbstractField;
8
use Thinktomorrow\Chief\Fields\Types\Field;
9
use Thinktomorrow\Chief\Fields\Types\FieldType;
10
use Thinktomorrow\Chief\Fields\Types\MediaField;
11
12
class FragmentField extends AbstractField implements Field
13
{
14
    /** @var Fields */
15
    private $fields;
16
17
    public static function make(string $key, Fields $fields): Field
18
    {
19
        return (new static(new FieldType(FieldType::FRAGMENT), $key))
20
            ->fields($fields);
21
    }
22
23
    private function fields(Fields $fields)
24
    {
25
        $this->fields = $fields;
26
27
        return $this;
28
    }
29
30
    public function getFields(): Fields
31
    {
32
        return $this->fields;
33
    }
34
35
    /**
36
     * @param string|null $locale
37
     * @return Fields[]
38
     */
39
    public function getFragments(?string $locale = null): array
0 ignored issues
show
Unused Code introduced by
The parameter $locale is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

39
    public function getFragments(/** @scrutinizer ignore-unused */ ?string $locale = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        // By default there is one empty Fragment provided to the user
42
        $fragments = [Fragment::empty($this->getKey())];
43
44
        // Model is auto-injected by Manager::editFields() method.
45
        if(($this->model)) {
46
47
            if(!method_exists($this->model, 'getFragments')) {
48
                throw new \RuntimeException(get_class($this->model) . ' is missing the ' . HasFragments::class . ' trait.');
49
            }
50
51
            if(count($modelFragments = $this->model->getFragments($this->getKey())) > 0) {
52
                $fragments = $modelFragments->map(function (FragmentModel $fragmentModel) {
53
                    return Fragment::fromModel($fragmentModel);
54
                })->all();
55
            }
56
        }
57
58
        foreach($fragments as $k => $fragment) {
59
            $fragments[$k] = $fragments[$k]
60
                ->setModelIdInputName($this->name.'[' . $k . '][modelId]')
61
                ->setFields($this->fields->clone()->map(function(Field $field) use($k, $fragment){
62
                    return $field->name($this->name.'.' . $k . '.' . $field->getName())
0 ignored issues
show
Bug introduced by
The method name() does not exist on Thinktomorrow\Chief\Fields\Types\Field. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Fields\Types\Field. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
                    return $field->/** @scrutinizer ignore-call */ name($this->name.'.' . $k . '.' . $field->getName())
Loading history...
63
                                 ->localizedFormat(':name.:locale')
64
                                 ->valueResolver(function($model = null, $locale = null, $field) use($fragment){
65
66
                                     if(isset($field->value)) return $field->value;
67
68
                                     if($field instanceof MediaField){
69
                                         if(!$fragment->hasModelId()){
70
                                             return [];
71
                                         }
72
73
                                         return $field->getMedia(FragmentModel::find($fragment->getModelId()), $locale);
74
                                     }
75
76
                                     return $fragment->getValue($field->getColumn(), $locale);
77
                                 });
78
                }));
79
        }
80
81
        return $fragments;
82
    }
83
84
    public function getDuplicatableFields(): array
85
    {
86
        if(count($this->getFragments()) < 1) return [];
87
88
        // Take the fields from the first fragment as a starting point for duplication
89
        return array_map(function(\Thinktomorrow\Chief\Fields\Types\Field $field){
90
            return $field->valueResolver(function($model = null, $locale = null, $field){
0 ignored issues
show
Bug introduced by
The method valueResolver() does not exist on Thinktomorrow\Chief\Fields\Types\Field. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Fields\Types\Field. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
            return $field->/** @scrutinizer ignore-call */ valueResolver(function($model = null, $locale = null, $field){
Loading history...
91
                if($field instanceof \Thinktomorrow\Chief\Fields\Types\MediaField) { return []; }
92
                return null;
93
            })->render();
94
        }, $this->getFragments()[0]->getFields()->clone()->all());
95
    }
96
}
97