MetaTableResolver::extractValueFromResource()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8.6298

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
ccs 11
cts 14
cp 0.7856
rs 8.4444
cc 8
nc 6
nop 2
crap 8.6298
1
<?php
2
3
namespace NovaThinKit\Nova\Flexible\Resolvers;
4
5
use Illuminate\Support\Collection;
6
use NovaFlexibleContent\Layouts\Collections\GroupsCollection;
7
use NovaFlexibleContent\Layouts\Collections\LayoutsCollection;
8
use NovaFlexibleContent\Value\Resolver;
9
10
class MetaTableResolver implements Resolver
11
{
12
    protected string $relationship;
13
    protected string $keyName;
14
    protected string $dataKeyName;
15
16
    /**
17
     * @param string $relationship
18
     */
19 5
    public function __construct(string $relationship, string $keyName = 'key', string $dataKeyName = 'data')
20
    {
21 5
        $this->relationship = $relationship;
22 5
        $this->keyName      = $keyName;
23 5
        $this->dataKeyName  = $dataKeyName;
24
    }
25
26
    /**
27
     * @inerhitDoc
28
     */
29 5
    public function get(mixed $resource, string $attribute, LayoutsCollection $groups): GroupsCollection
30
    {
31 5
        $meta = $resource->{$this->relationship}()->where($this->keyName, $attribute)->first();
32 5
        if ($meta) {
33 1
            $value = static::extractValueFromResource($meta, $this->dataKeyName);
34
35 1
            return GroupsCollection::make($value)->map(function (LayoutValue $item) use ($groups) {
0 ignored issues
show
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

35
            return GroupsCollection::make(/** @scrutinizer ignore-type */ $value)->map(function (LayoutValue $item) use ($groups) {
Loading history...
36 1
                $layout = $groups->find($item->layout);
0 ignored issues
show
Bug Best Practice introduced by
The property layout does not exist on NovaThinKit\Nova\Flexible\Resolvers\LayoutValue. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $item->layout can also be of type null; however, parameter $name of NovaFlexibleContent\Layo...youtsCollection::find() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

36
                $layout = $groups->find(/** @scrutinizer ignore-type */ $item->layout);
Loading history...
37
38 1
                if (!$layout) {
39
                    throw new \Exception("Layout [{$item->layout}] not found");
40
                }
41
42 1
                return $layout->duplicate($item->key, (array)$item->attributes)
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist on NovaThinKit\Nova\Flexible\Resolvers\LayoutValue. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property attributes does not exist on NovaThinKit\Nova\Flexible\Resolvers\LayoutValue. Since you implemented __get, consider adding a @property annotation.
Loading history...
43 1
                    ->setCollapsed((bool)$item->collapsed);
0 ignored issues
show
Bug Best Practice introduced by
The property collapsed does not exist on NovaThinKit\Nova\Flexible\Resolvers\LayoutValue. Since you implemented __get, consider adding a @property annotation.
Loading history...
44 1
            })->filter()->values();
45
        }
46
47 5
        return GroupsCollection::make([]);
0 ignored issues
show
Bug introduced by
array() 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

47
        return GroupsCollection::make(/** @scrutinizer ignore-type */ []);
Loading history...
48
    }
49
50
    /**
51
     * @param $resource
52
     * @param string $attribute
53
     * @return array
54
     */
55 1
    public static function extractValueFromResource($resource, string $attribute = 'data'): array
56
    {
57 1
        $value = data_get($resource, str_replace('->', '.', $attribute)) ?? [];
58
59 1
        if ($value instanceof Collection) {
60
            $value = $value->toArray();
61 1
        } elseif (is_string($value)) {
62 1
            $value = json_decode($value, true) ?? [];
63
        }
64
65
        // Fail silently in case data is invalid
66 1
        if (!is_array($value)) {
67
            return [];
68
        }
69
70 1
        return array_filter(array_map(function ($item) {
71 1
            $layoutValue = new LayoutValue(!is_array($item) ? (array)$item : $item);
72 1
            if ($layoutValue->layout && $layoutValue->key && is_array($layoutValue->attributes)) {
0 ignored issues
show
Bug Best Practice introduced by
The property layout does not exist on NovaThinKit\Nova\Flexible\Resolvers\LayoutValue. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property key does not exist on NovaThinKit\Nova\Flexible\Resolvers\LayoutValue. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property attributes does not exist on NovaThinKit\Nova\Flexible\Resolvers\LayoutValue. Since you implemented __get, consider adding a @property annotation.
Loading history...
73 1
                return $layoutValue;
74
            }
75
76
            return null;
77 1
        }, $value));
78
    }
79
80
    /**
81
     * @inerhitDoc
82
     */
83 1
    public function set(mixed $resource, string $attribute, GroupsCollection $groups): string
84
    {
85 1
        $resource->{$this->relationship}()->updateOrCreate(
86 1
            [$this->keyName => $attribute],
87 1
            [$this->dataKeyName => $groups->map(function ($group) {
88 1
                return [
89 1
                    'collapsed'  => $group->isCollapsed(),
90 1
                    'layout'     => $group->name(),
91 1
                    'key'        => $group->key(),
92 1
                    'attributes' => $group->getAttributes(),
93 1
                ];
94 1
            })]
95 1
        );
96
97 1
        return '';
98
    }
99
}
100