Passed
Push — main ( 66769c...c749bb )
by Yaroslav
19:17
created

DefaultResolver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 89
ccs 34
cts 40
cp 0.85
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 18 2
A __construct() 0 4 1
A extractValueFromResource() 0 11 3
A get() 0 30 6
1
<?php
2
3
namespace NovaFlexibleContent\Value;
4
5
use Illuminate\Support\Collection;
6
use Laravel\Nova\Makeable;
7
use NovaFlexibleContent\Layouts\Collections\GroupsCollection;
8
use NovaFlexibleContent\Layouts\Collections\LayoutsCollection;
9
use NovaFlexibleContent\Layouts\Layout;
10
11
class DefaultResolver implements Resolver
12
{
13
    use Makeable;
14
15
    protected ?\Closure $set = null;
16
    protected ?\Closure $get = null;
17
18 15
    public function __construct(?\Closure $set = null, ?\Closure $get = null)
19
    {
20 15
        $this->set = $set;
21 15
        $this->get = $get;
22
    }
23
24
    /**
25
     * @inerhitDoc
26
     */
27 4
    public function set(mixed $resource, string $attribute, GroupsCollection $groups): string
28
    {
29 4
        $value = $groups->map(function (Layout $group) {
30 4
            return [
31 4
                'layout'     => $group->name(),
32 4
                'key'        => $group->key(),
33 4
                'collapsed'  => $group->isCollapsed(),
34 4
                'attributes' => $group->getAttributes(),
35 4
            ];
36 4
        });
37
38 4
        if ($this->set) {
39
            call_user_func($this->set, $resource, $value, $attribute, $groups);
40
        } else {
41 4
            $resource->$attribute = $value;
42
        }
43
44 4
        return $value;
45
    }
46
47
    /**
48
     * @inerhitDoc
49
     */
50 13
    public function get(mixed $resource, string $attribute, LayoutsCollection $groups): GroupsCollection
51
    {
52 13
        if ($this->get) {
53
            $value = call_user_func($this->get, $resource, $attribute, $groups);
54
        } else {
55 13
            $value = $this->extractValueFromResource($resource, $attribute);
56
        }
57
58
        // Fail silently in case data is invalid
59 13
        if (!is_array($value)) {
60
            $value = [];
61
        }
62
63
        // Force transform arrays to objects
64 13
        $value = array_map(function ($item) {
65 7
            return is_array($item) ? (object)$item : $item;
66 13
        }, $value);
67
68 13
        return GroupsCollection::make($value)->map(function ($item) use ($groups, $attribute) {
0 ignored issues
show
Unused Code introduced by
The import $attribute is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
Bug introduced by
$value of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make(). ( Ignorable by Annotation )

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

68
        return GroupsCollection::make(/** @scrutinizer ignore-type */ $value)->map(function ($item) use ($groups, $attribute) {
Loading history...
69 7
            if ($item instanceof Layout) {
70
                return $item;
71
            }
72
73 7
            if ($layout = $groups->find($item->layout)) {
74 7
                return $layout->duplicate($item->key, (array)$item->attributes)
75 7
                    ->setCollapsed((bool)($item->collapsed ?? false));
76
            }
77
78
            return null;
79 13
        })->filter()->values();
80
    }
81
82
    /**
83
     * Find the attribute's value in the given resource
84
     *
85
     * @param mixed $resource
86
     * @param string $attribute
87
     * @return array
88
     */
89 13
    protected function extractValueFromResource($resource, $attribute)
90
    {
91 13
        $value = data_get($resource, str_replace('->', '.', $attribute)) ?? [];
92
93 13
        if ($value instanceof Collection) {
94
            $value = $value->all();
95 13
        } elseif (is_string($value)) {
96 7
            $value = json_decode($value) ?? [];
97
        }
98
99 13
        return $value;
100
    }
101
}
102