Passed
Push — master ( 5f2d64...84176e )
by Quentin
05:55
created

getFormFieldsHandleFieldsGroups()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 7.9062

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 14
ccs 3
cts 8
cp 0.375
crap 7.9062
rs 10
1
<?php
2
3
namespace A17\Twill\Repositories\Behaviors;
4
5
use Illuminate\Support\Arr;
6
7
trait HandleFieldsGroups
8
{
9
10
    /**
11
     * @param \A17\Twill\Models\Model|null $object
12
     * @param array $fields
13
     * @return array
14
     */
15 27
    public function prepareFieldsBeforeSaveHandleFieldsGroups($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

15
    public function prepareFieldsBeforeSaveHandleFieldsGroups(/** @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...
16
    {
17 27
        return $this->handleFieldsGroups($fields);
18
    }
19
20
    /**
21
     * @param \A17\Twill\Models\Model|null $object
22
     * @param array $fields
23
     * @return array
24
     */
25 22
    public function prepareFieldsBeforeCreateHandleFieldsGroups($fields)
26
    {
27 22
        return $this->handleFieldsGroups($fields);
28
    }
29
30
    /**
31
     * @param \A17\Twill\Models\Model $object
32
     * @param array $fields
33
     * @return array
34
     */
35 5
    public function getFormFieldsHandleFieldsGroups($object, $fields)
36
    {
37 5
        foreach ($this->fieldsGroups as $group => $groupFields) {
38
            if ($object->$group) {
39
                $decoded_fields = json_decode($object->$group, true) ?? [];
40
                // In case that some field read the value through $item->$name
41
                foreach($decoded_fields as $field_name => $field_value) {
42
                    $object->setAttribute($field_name, $field_value);
43
                }
44
                $fields = array_merge($fields, $decoded_fields);
45
            }
46
        }
47
48 5
        return $fields;
49
    }
50
51 28
    protected function handleFieldsGroups($fields) {
52 28
        foreach ($this->fieldsGroups as $group => $groupFields) {
53
            $fields[$group] = Arr::where(Arr::only($fields, $groupFields), function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key 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

53
            $fields[$group] = Arr::where(Arr::only($fields, $groupFields), function ($value, /** @scrutinizer ignore-unused */ $key) {

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...
54
                return !empty($value);
55
            });
56
            
57
            if (empty($fields[$group])) {
58
                $fields[$group] = null;
59
            }
60
            
61
            Arr::forget($fields, $groupFields);
62
        }
63
64 28
        return $fields;
65
    }
66
}
67