Passed
Pull Request — master (#654)
by
unknown
14:11
created

HandleJsonRepeaters::getJsonRepeater()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
c 2
b 0
f 0
nc 13
nop 3
dl 0
loc 54
ccs 0
cts 31
cp 0
crap 72
rs 8.1475

How to fix   Long Method   

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\Collection;
8
use Illuminate\Support\Str;
9
10
/**
11
 *
12
 * Save repeaters in a json column instead of a new model.
13
 *
14
 * This trait is not intended to replace main repeaters but to give a quick
15
 * and easy alternative for simple elements where creating a new table might be an overkill.
16
 *
17
 * Simply define an array with the repeater names on your repository:
18
 * protected $jsonRepeaters = [ 'REPEATER_NAME_1', 'REPEATER_NAME_2', ... ]
19
 *
20
 * Names must be the same as the ones you added in your `repeaters` attribute on `config\twill.php`
21
 *
22
 * Supported: Input, WYSIWYG, textarea, browsers.
23
 * Not supported: Medias, Files, repeaters.
24
 *
25
 */
26
27
trait HandleJsonRepeaters
28
{
29
30
    /**
31
     * @param \A17\Twill\Models\Model $object
32
     * @param array $fields
33
     * @return string[]
34
     */
35
    public function prepareFieldsBeforeSaveHandleJsonRepeaters($object, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $object 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

35
    public function prepareFieldsBeforeSaveHandleJsonRepeaters(/** @scrutinizer ignore-unused */ $object, $fields)

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...
36
    {
37
        foreach ($this->jsonRepeaters as $repeater) {
38
            if (isset($fields['repeaters'][$repeater])) {
39
                $fields[$repeater] = $fields['repeaters'][$repeater];
40
            }
41
        }
42
43
        return $fields;
44
    }
45
46
    /**
47
     * @param \A17\Twill\Models\Model $object
48
     * @param array $fields
49
     * @return string[]
50
     */
51
    public function getFormFieldsHandleJsonRepeaters($object, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $object 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

51
    public function getFormFieldsHandleJsonRepeaters(/** @scrutinizer ignore-unused */ $object, $fields)

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...
52
    {
53
54
        foreach($this->jsonRepeaters as $repeater) {
55
            if (isset($fields[$repeater]) && !empty($fields[$repeater])) {
56
                $fields = $this->getJsonRepeater($fields, $repeater, $fields[$repeater]);
57
            }
58
        }
59
60
        return $fields;
61
    }
62
63
    public function getJsonRepeater($fields, $repeaterName, $serializedData) {
64
        $repeatersFields = [];
65
        $repeatersBrowsers = [];
66
        $repeatersMedias = [];
67
        $repeatersConfig = config('twill.block_editor.repeaters');
68
69
70
        foreach($serializedData as $index => $repeaterItem) {
71
            $id = $repeaterItem['id'] ?? $index;
72
73
            $repeaters[] = [
74
                'id' => $id,
75
                'type' => $repeatersConfig[$repeaterName]['component'],
76
                'title' => $repeatersConfig[$repeaterName]['title'],
77
            ];
78
            
79
            if (isset($repeaterItem['medias'])) {
80
                if (config('twill.media_library.translated_form_fields', false)) {
81
                    Collection::make($repeaterItem['medias'])->each(function ($rolesWithMedias, $locale) use (&$repeatersMedias, $id) {
82
                        $repeatersMedias[] = Collection::make($rolesWithMedias)->mapWithKeys(function ($medias, $role) use ($locale, $id) {
83
                            return [
84
                                "blocks[$id][$role][$locale]" => $medias,
85
                            ];
86
                        })->toArray();
87
                    });
88
                } else {
89
                    foreach ($repeaterItem['medias'] as $key => $values) {
90
                        $repeatersMedias["blocks[$id][$key]"] = $values;
91
                    }
92
                }
93
            }
94
95
            if (isset($repeaterItem['browsers'])) {
96
                foreach ($repeaterItem['browsers'] as $key => $values) {
97
                    $repeatersBrowsers["blocks[$id][$key]"] = $values;
98
                }
99
            }
100
101
            $itemFields = Arr::except($repeaterItem, ['id', 'repeaters', 'files', 'medias', 'browsers', 'blocks']);
102
103
            foreach($itemFields as $index => $value) {
0 ignored issues
show
Comprehensibility Bug introduced by
$index is overwriting a variable from outer foreach loop.
Loading history...
104
                $repeatersFields[] = [
105
                    'name' => "blocks[$id][$index]",
106
                    'value' => $value
107
                ];
108
            }
109
        }
110
111
        $fields['repeaters'][$repeaterName] = $repeaters;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $repeaters seems to be defined by a foreach iteration on line 70. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
112
        $fields['repeaterFields'][$repeaterName] = $repeatersFields;
113
        $fields['repeaterBrowsers'][$repeaterName] = $repeatersBrowsers;
114
        $fields['repeaterMedias'][$repeaterName] = $repeatersMedias;
115
116
        return $fields;
117
    }
118
119
}
120