Passed
Pull Request — 2.x (#727)
by Bekzat
16:53
created

HandleFieldsGroups::handleFieldsGroups()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.667

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 3
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 3
cts 9
cp 0.3333
crap 5.667
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 $object
12
     * @param array $fields
13
     * @return \A17\Twill\Models\Model
14
     */
15 3
    public function hydrateHandleFieldsGroups($object, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $fields 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 hydrateHandleFieldsGroups($object, /** @scrutinizer ignore-unused */ $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 3
        foreach ($this->fieldsGroups as $group => $groupFields) {
18
            if ($object->$group) {
19
                $object->$group = json_encode($object->$group);
20
            }
21
        }
22
23 3
        return $object;
24
    }
25
    
26
    /**
27
     * @param \A17\Twill\Models\Model|null $object
28
     * @param array $fields
29
     * @return array
30
     */
31 31
    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

31
    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...
32
    {
33 31
        return $this->handleFieldsGroups($fields);
34
    }
35
36
    /**
37
     * @param \A17\Twill\Models\Model|null $object
38
     * @param array $fields
39
     * @return array
40
     */
41 26
    public function prepareFieldsBeforeCreateHandleFieldsGroups($fields)
42
    {
43 26
        return $this->handleFieldsGroups($fields);
44
    }
45
46
    /**
47
     * @param \A17\Twill\Models\Model $object
48
     * @param array $fields
49
     * @return array
50
     */
51 6
    public function getFormFieldsHandleFieldsGroups($object, $fields)
52
    {
53 6
        foreach ($this->fieldsGroups as $group => $groupFields) {
54
            if ($object->$group) {
55
                $decoded_fields = json_decode($object->$group, true) ?? [];
56
                // In case that some field read the value through $item->$name
57
                foreach($decoded_fields as $field_name => $field_value) {
58
                    $object->setAttribute($field_name, $field_value);
59
                }
60
                $fields = array_merge($fields, $decoded_fields);
61
            }
62
        }
63
64 6
        return $fields;
65
    }
66
67 32
    protected function handleFieldsGroups($fields) {
68 32
        foreach ($this->fieldsGroups as $group => $groupFields) {
69
            $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

69
            $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...
70
                return !empty($value);
71
            });
72
            
73
            if (empty($fields[$group])) {
74
                $fields[$group] = null;
75
            }
76
            
77
            Arr::forget($fields, $groupFields);
78
        }
79
80 32
        return $fields;
81
    }
82
}
83