Passed
Push — 1.2 ( 392f29...968e7d )
by Quentin
08:39 queued 03:08
created

HandleRepeaters::getFormFieldsForRepeater()   F

Complexity

Conditions 16
Paths 776

Size

Total Lines 93
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 16
eloc 56
c 6
b 0
f 0
nc 776
nop 5
dl 0
loc 93
rs 1.7111

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace A17\Twill\Repositories\Behaviors;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use Illuminate\Support\Collection;
9
10
trait HandleRepeaters
11
{
12
    /**
13
     * @param \A17\Twill\Models\Model $object
14
     * @param array $fields
15
     * @return void
16
     */
17
    public function afterSaveHandleRepeaters($object, $fields)
18
    {
19
        if (property_exists($this, 'repeaters')) {
20
            foreach ($this->repeaters as $module) {
21
                if (is_string($module)) {
22
                    $model = Str::studly(Str::singular($module));
23
                    $repeaterName = Str::singular($module);
24
                    $this->updateRepeater($object, $fields, $module, $model, $repeaterName);
25
                } elseif (is_array($module)) {
26
                    $relation = !empty($module['relation']) ? $module['relation'] : key($module);
27
                    $model = isset($module['model']) ? $module['model'] : Str::studly(Str::singular(key($module)));
28
                    $repeaterName = !empty($module['repeaterName']) ? $module['repeaterName'] : Str::singular(key($module));
29
                    $this->updateRepeater($object, $fields, $relation, $model, $repeaterName);
30
                }
31
            }
32
        }
33
    }
34
35
    /**
36
     * @param \A17\Twill\Models\Model $object
37
     * @param array $fields
38
     * @return array
39
     */
40
    public function getFormFieldsHandleRepeaters($object, $fields)
41
    {
42
        if (property_exists($this, 'repeaters')) {
43
            foreach ($this->repeaters as $module) {
44
                if (is_string($module)) {
45
                    $model = Str::studly(Str::singular($module));
46
                    $repeaterName = Str::singular($module);
47
                    $fields = $this->getFormFieldsForRepeater($object, $fields, $module, $model, $repeaterName);
48
                } elseif (is_array($module)) {
49
                    $model = isset($module['model']) ? $module['model'] : Str::studly(Str::singular(key($module)));
50
                    $relation = !empty($module['relation']) ? $module['relation'] : key($module);
51
                    $repeaterName = !empty($module['repeaterName']) ? $module['repeaterName'] : Str::singular(key($module));
52
                    $fields = $this->getFormFieldsForRepeater($object, $fields, $relation, $model, $repeaterName);
53
                }
54
            }
55
        }
56
        
57
        return $fields;
58
    } 
59
60
    public function updateRepeaterMany($object, $fields, $relation, $keepExisting = true, $model = null)
61
    {
62
        $relationFields = $fields['repeaters'][$relation] ?? [];
63
        $relationRepository = $this->getModelRepository($relation, $model);
0 ignored issues
show
Bug introduced by
It seems like getModelRepository() 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

63
        /** @scrutinizer ignore-call */ 
64
        $relationRepository = $this->getModelRepository($relation, $model);
Loading history...
64
65
        if (!$keepExisting) {
66
            $object->$relation()->each(function ($repeaterElement) {
67
                $repeaterElement->forceDelete();
68
            });
69
        }
70
71
        foreach ($relationFields as $relationField) {
72
            $newRelation = $relationRepository->create($relationField);
73
            $object->$relation()->attach($newRelation->id);
74
        }
75
    }
76
77
    public function updateRepeater($object, $fields, $relation, $model = null, $repeaterName = null)
78
    {
79
        if (!$repeaterName) {
80
            $repeaterName = $relation;
81
        }
82
83
        $relationFields = $fields['repeaters'][$repeaterName] ?? [];
84
85
        $relationRepository = $this->getModelRepository($relation, $model);
86
87
        // if no relation field submitted, soft deletes all associated rows
88
        if (!$relationFields) {
89
            $relationRepository->updateBasic(null, [
90
                'deleted_at' => Carbon::now(),
91
            ], [
92
                $this->model->getForeignKey() => $object->id,
93
            ]);
94
        }
95
96
        // keep a list of updated and new rows to delete (soft delete?) old rows that were deleted from the frontend
97
        $currentIdList = [];
98
99
        foreach ($relationFields as $index => $relationField) {
100
            $relationField['position'] = $index + 1;
101
            if (isset($relationField['id']) && starts_with($relationField['id'], $relation)) {
0 ignored issues
show
Deprecated Code introduced by
The function starts_with() has been deprecated: Str::startsWith() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

101
            if (isset($relationField['id']) && /** @scrutinizer ignore-deprecated */ starts_with($relationField['id'], $relation)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
102
                // row already exists, let's update
103
                $id = str_replace($relation . '-', '', $relationField['id']);
104
                $relationRepository->update($id, $relationField);
105
                $currentIdList[] = $id;
106
            } else {
107
                // new row, let's attach to our object and create
108
                $relationField[$this->model->getForeignKey()] = $object->id;
109
                unset($relationField['id']);
110
                $newRelation = $relationRepository->create($relationField);
111
                $currentIdList[] = $newRelation['id'];
112
            }
113
        }
114
115
        foreach ($object->$relation->pluck('id') as $id) {
116
            if (!in_array($id, $currentIdList)) {
117
                $relationRepository->updateBasic(null, [
118
                    'deleted_at' => Carbon::now(),
119
                ], [
120
                    'id' => $id,
121
                ]);
122
            }
123
        }
124
    }
125
126
    public function getFormFieldsForRepeater($object, $fields, $relation, $model = null, $repeaterName = null)
127
    {
128
        if (!$repeaterName) {
129
            $repeaterName = $relation;
130
        }
131
132
        $repeaters = [];
133
        $repeatersFields = [];
134
        $repeatersBrowsers = [];
135
        $repeatersMedias = [];
136
        $repeatersFiles = [];
137
        $relationRepository = $this->getModelRepository($relation, $model);
138
        $repeatersConfig = config('twill.block_editor.repeaters');
139
140
        foreach ($object->$relation as $relationItem) {
141
            $repeaters[] = [
142
                'id' => $relation . '-' . $relationItem->id,
143
                'type' => $repeatersConfig[$repeaterName]['component'],
144
                'title' => $repeatersConfig[$repeaterName]['title'],
145
            ];
146
147
            $relatedItemFormFields = $relationRepository->getFormFields($relationItem);
148
            $translatedFields = [];
149
150
            if (isset($relatedItemFormFields['translations'])) {
151
                foreach ($relatedItemFormFields['translations'] as $key => $values) {
152
                    $repeatersFields[] = [
153
                        'name' => "blocks[$relation-$relationItem->id][$key]",
154
                        'value' => $values,
155
                    ];
156
157
                    $translatedFields[] = $key;
158
                }
159
            }
160
161
            if (isset($relatedItemFormFields['medias'])) {
162
                if (config('twill.media_library.translated_form_fields', false)) {
163
                    Collection::make($relatedItemFormFields['medias'])->each(function ($rolesWithMedias, $locale) use (&$repeatersMedias, $relation, $relationItem) {
164
                        $repeatersMedias[] = Collection::make($rolesWithMedias)->mapWithKeys(function ($medias, $role) use ($locale, $relation, $relationItem) {
165
                            return [
166
                                "blocks[$relation-$relationItem->id][$role][$locale]" => $medias,
167
                            ];
168
                        })->toArray();
169
                    });
170
                } else {
171
                    foreach ($relatedItemFormFields['medias'] as $key => $values) {
172
                        $repeatersMedias["blocks[$relation-$relationItem->id][$key]"] = $values;
173
                    }
174
                }
175
            }
176
177
            if (isset($relatedItemFormFields['files'])) {
178
                Collection::make($relatedItemFormFields['files'])->each(function ($rolesWithFiles, $locale) use (&$repeatersFiles, $relation, $relationItem) {
179
                    $repeatersFiles[] = Collection::make($rolesWithFiles)->mapWithKeys(function ($files, $role) use ($locale, $relation, $relationItem) {
180
                        return [
181
                            "blocks[$relation-$relationItem->id][$role][$locale]" => $files,
182
                        ];
183
                    })->toArray();
184
                });
185
            }
186
187
            if (isset($relatedItemFormFields['browsers'])) {
188
                foreach ($relatedItemFormFields['browsers'] as $key => $values) {
189
                    $repeatersBrowsers["blocks[$relation-$relationItem->id][$key]"] = $values;
190
                }
191
            }
192
193
            $itemFields = method_exists($relationItem, 'toRepeaterArray') ? $relationItem->toRepeaterArray() : Arr::except($relationItem->attributesToArray(), $translatedFields);
194
195
            foreach ($itemFields as $key => $value) {
196
                $repeatersFields[] = [
197
                    'name' => "blocks[$relation-$relationItem->id][$key]",
198
                    'value' => $value,
199
                ];
200
            }
201
202
        }
203
204
        if (!empty($repeatersMedias) && config('twill.media_library.translated_form_fields', false)) {
205
            $repeatersMedias = call_user_func_array('array_merge', $repeatersMedias);
206
        }
207
208
        if (!empty($repeatersFiles)) {
209
            $repeatersFiles = call_user_func_array('array_merge', $repeatersFiles);
210
        }
211
212
        $fields['repeaters'][$repeaterName] = $repeaters;
213
        $fields['repeaterFields'][$repeaterName] = $repeatersFields;
214
        $fields['repeaterMedias'][$repeaterName] = $repeatersMedias;
215
        $fields['repeaterFiles'][$repeaterName] = $repeatersFiles;
216
        $fields['repeaterBrowsers'][$repeaterName] = $repeatersBrowsers;
217
218
        return $fields;
219
    }
220
}
221