Test Setup Failed
Push — dependabot/npm_and_yarn/larave... ( c86056...82b153 )
by
unknown
220:25 queued 213:07
created

FragmentField::getDuplicatableFields()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
ccs 4
cts 6
cp 0.6667
rs 9.9666
cc 3
nc 2
nop 0
crap 3.3332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Fragments;
6
7
use Thinktomorrow\Chief\Fields\Fields;
8
use Thinktomorrow\Chief\Fields\Types\AbstractField;
9
use Thinktomorrow\Chief\Fields\Types\Field;
10
use Thinktomorrow\Chief\Fields\Types\FieldType;
11
use Thinktomorrow\Chief\Fields\Types\MediaField;
12
13
class FragmentField extends AbstractField implements Field
14
{
15
    /** @var Fields */
16
    private $fields;
17
18
    /** @var null|string */
19
    private $fragmentLabel;
20
21 43
    /** @var int */
22
    private $max;
23 43
24 43
    public static function make(string $key, Fields $fields): Field
25
    {
26
        return (new static(new FieldType(FieldType::FRAGMENT), $key))
27 43
            ->fields($fields);
28
    }
29 43
30
    private function fields(Fields $fields)
31 43
    {
32
        $this->fields = $fields;
33
34
        return $this;
35
    }
36
37
    public function getFields(): Fields
38
    {
39
        return $this->fields;
40
    }
41
42
    /**
43 2
     * @param string|null $locale
44
     * @return Fields[]
45
     */
46 2
    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

46
    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...
47
    {
48
        // By default there is one empty Fragment provided to the user
49 2
        $fragments = [Fragment::empty($this->getKey())];
50 1
51
        // Model is auto-injected by Manager::editFields() method.
52
        if (($this->model)) {
53
            if (!method_exists($this->model, 'getFragments')) {
54 1
                throw new \RuntimeException(get_class($this->model) . ' is missing the ' . HasFragments::class . ' trait.');
55
            }
56
57
            if (count($modelFragments = $this->model->getFragments($this->getKey())) > 0) {
58
                $fragments = $modelFragments->map(function (FragmentModel $fragmentModel) {
59
                    return Fragment::fromModel($fragmentModel);
60
                })->all();
61 2
            }
62 2
        }
63 2
64
        foreach ($fragments as $k => $fragment) {
65 2
            $fragments[$k] = $fragments[$k]
66 2
                ->setModelIdInputName($this->name . '[' . $k . '][modelId]')
67
                ->setFields($this->fields->clone()->map(function (Field $field) use ($k, $fragment) {
68 2
                    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

68
                    return $field->/** @scrutinizer ignore-call */ name($this->name . '.' . $k . '.' . $field->getName())
Loading history...
69
                                 ->localizedFormat(':name.:locale')
70
                                 ->valueResolver(function ($model = null, $locale = null, $field) use ($fragment) {
71
                                     if (isset($field->value)) {
72 2
                                         return $field->value;
73
                                     }
74
75
                                     if ($field instanceof MediaField) {
76
                                         if (!$fragment->hasModelId()) {
77
                                             return [];
78
                                         }
79
80 2
                                         return $field->getMedia(FragmentModel::find($fragment->getModelId()), $locale);
81 2
                                     }
82 2
83
                                     return $fragment->getValue($field->getColumn(), $locale);
84
                                 });
85 2
                }));
86
        }
87
88
        return $fragments;
89
    }
90
91
    /**
92
     * A fragmentlabel is shown in admin as the label above each fragment.
93
     *
94
     * @param string $fragmentLabel
95
     * @return $this
96
     */
97
    public function fragmentLabel(string $fragmentLabel): self
98
    {
99
        $this->fragmentLabel = $fragmentLabel;
100
101 2
        return $this;
102
    }
103 2
104
    public function getFragmentLabel(): string
105
    {
106 2
        return $this->fragmentLabel ?? 'fragment';
107
    }
108 2
109
    public function getDuplicatableFields(): array
110
    {
111
        if (count($this->getFragments()) < 1) {
112
            return [];
113
        }
114
115 2
        // Take the fields from the first fragment as a starting point for duplication
116
        return array_map(function (\Thinktomorrow\Chief\Fields\Types\Field $field) {
117
            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

117
            return $field->/** @scrutinizer ignore-call */ valueResolver(function ($model = null, $locale = null, $field) {
Loading history...
118 2
                if ($field instanceof \Thinktomorrow\Chief\Fields\Types\MediaField) {
119 2
                    return [];
120 2
                }
121
                return null;
122
            })->render();
123
        }, $this->getFragments()[0]->getFields()->clone()->all());
124
    }
125
126
    public function max(int $max)
127
    {
128
        $this->max = $max;
129
130
        return $this;
131
    }
132
133
    public function getMax(): int
134
    {
135
        return $this->max ?: 50;
136
    }
137
}
138