Passed
Push — main ( b3cf5c...f07e9b )
by Yaroslav
18:34
created

DefaultResolver::extractValueFromResource()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.1502

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
nc 6
nop 2
dl 0
loc 18
ccs 9
cts 11
cp 0.8182
crap 5.1502
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
namespace NovaFlexibleContent\Value;
4
5
use Illuminate\Support\Collection;
6
use NovaFlexibleContent\Layouts\Collections\GroupsCollection;
7
use NovaFlexibleContent\Layouts\Collections\LayoutsCollection;
8
use NovaFlexibleContent\Layouts\Layout;
9
10
class DefaultResolver implements Resolver
11
{
12
13
    /**
14
     * @inerhitDoc
15
     */
16 4
    public function set(mixed $resource, string $attribute, GroupsCollection $groups): string
17
    {
18 4
        return $resource->$attribute = $groups->map(function (Layout $group) {
19 4
            return [
20 4
                'layout'     => $group->name(),
21 4
                'key'        => $group->key(),
22 4
                'collapsed'  => $group->isCollapsed(),
23 4
                'attributes' => $group->getAttributes(),
24 4
            ];
25 4
        });
26
    }
27
28
    /**
29
     * @inerhitDoc
30
     */
31 13
    public function get(mixed $resource, string $attribute, LayoutsCollection $groups): GroupsCollection
32
    {
33 13
        return GroupsCollection::make(
34 13
            $this->extractValueFromResource($resource, $attribute)
0 ignored issues
show
Bug introduced by
$this->extractValueFromR...($resource, $attribute) 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

34
            /** @scrutinizer ignore-type */ $this->extractValueFromResource($resource, $attribute)
Loading history...
35 13
        )->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...
36 7
            if($item instanceof Layout) {
37
                return $item;
38
            }
39
40 7
            if ($layout = $groups->find($item->layout)) {
41 7
                return $layout->duplicate($item->key, (array)$item->attributes)
42 7
                    ->setCollapsed((bool)($item->collapsed ?? false));
43
            }
44
45
            return null;
46 13
        })->filter()->values();
47
    }
48
49
    /**
50
     * Find the attribute's value in the given resource
51
     *
52
     * @param mixed $resource
53
     * @param string $attribute
54
     * @return array
55
     */
56 13
    protected function extractValueFromResource($resource, $attribute)
57
    {
58 13
        $value = data_get($resource, str_replace('->', '.', $attribute)) ?? [];
59
60 13
        if ($value instanceof Collection) {
61
            $value = $value->all();
62 13
        } elseif (is_string($value)) {
63 7
            $value = json_decode($value) ?? [];
64
        }
65
66
        // Fail silently in case data is invalid
67 13
        if (!is_array($value)) {
68
            return [];
69
        }
70
71 13
        return array_map(function ($item) {
72 7
            return is_array($item) ? (object)$item : $item;
73 13
        }, $value);
74
    }
75
}
76