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

HandleFieldsGroups   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 21
c 4
b 0
f 0
dl 0
loc 74
ccs 13
cts 26
cp 0.5
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormFieldsHandleFieldsGroups() 0 14 4
A hydrateHandleFieldsGroups() 0 9 3
A prepareFieldsBeforeSaveHandleFieldsGroups() 0 3 1
A prepareFieldsBeforeCreateHandleFieldsGroups() 0 3 1
A handleFieldsGroups() 0 14 3
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