Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — poc-backpack-components ( 0ddd2c...dbc69d )
by Pedro
26:57
created

BaseCollection::buildAttributes()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Components;
4
5
use Backpack\CRUD\app\Library\Components\Attributes\BaseAttribute;
6
use Backpack\CRUD\app\Library\Components\Interfaces\SmartAttributeInterface;
7
use Backpack\CRUD\app\Library\Components\Interfaces\SmartCollectionInterface;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Facades\Validator;
10
11
class BaseCollection implements SmartCollectionInterface
12
{
13
    private Collection $attributes;
14
15
    public function __construct(array|string $initAttributes,
16
                                array $componentAttributes = [],
17
                                array $rules = [],
18
                                array $defaults = [],
19
                                private array $blockedAttributes = []
20
                            ) {
21
        $this->attributes = $this->buildAttributes($initAttributes, $componentAttributes);
22
23
        $this->setAttributeDefaults($componentAttributes);
24
        
25
        $this->validate();
26
27
        $item = $this->getItemByName((string) $this->getAttributeValue('name'));
28
        if ($item) {
29
            $this->attributes = $this->buildAttributes((array)$item, $componentAttributes);
30
        } else {
31
            $this->addCollectionItem($this);
32
        }
33
    }
34
35
    private function buildAttributes(array|string $attributes, $componentAttributes)
36
    {
37
        if (is_string($attributes)) {
0 ignored issues
show
introduced by
The condition is_string($attributes) is always false.
Loading history...
38
            $attributes = ['name' => $attributes];
39
        }
40
41
        $attributeNames = collect($componentAttributes)->mapWithKeys(function ($attribute) {
42
            return [$attribute::getAttributeName() => $attribute];
43
        })->toArray();
44
45
        return collect($attributes)->mapWithKeys(function ($value, $key) use ($attributeNames) {
0 ignored issues
show
Bug introduced by
$attributes of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

45
        return collect(/** @scrutinizer ignore-type */ $attributes)->mapWithKeys(function ($value, $key) use ($attributeNames) {
Loading history...
46
            if (isset($attributeNames[$key])) {
47
                return [$key => $attributeNames[$key]::make($key, $value, $attributeNames[$key]::getValidationRules($this))];
48
            }
49
50
            return [$key => new BaseAttribute($key, $value)];
51
        });
52
    }
53
54
    public function addCollectionItem($attributes)
55
    {
56
        $collection = $this->getCollection();
57
        $collection[$attributes->getAttributeValue('name')] = $attributes->toArray();
58
        $this->saveCollection($collection);
59
    }
60
61
    public function getItems(): Collection
62
    {
63
        return collect($this->getCollection());
64
    }
65
66
    public function getAttributes(): Collection
67
    {
68
        return $this->attributes;
69
    }
70
71
    public function getItemByName(string $name)
72
    {
73
        return $this->getItems()->first(function ($item, $key) use ($name) {
74
            return $key === $name;
75
        });
76
    }
77
78
    public function hasAttribute(string $attribute): bool
79
    {
80
        return $this->attributes->has($attribute);
81
    }
82
83
    public function getAttributeValue($attribute, $default = null)
84
    {
85
        return $this->attributes->has($attribute) ? $this->attributes->get($attribute, $default)->value() : $default;
86
    }
87
88
    public function validate()
89
    {
90
        $attributes = $this->toArray();
91
92
        $rules = $this->getValidationRules();
93
94
        $validator = Validator::make($attributes, $rules)->stopOnFirstFailure();
95
96
        if ($validator->fails()) {
97
            throw new \Exception($validator->errors()->first());
98
        }
99
    }
100
101
    private function getValidationRules()
102
    {
103
        $rules = [];
104
        foreach($this->attributes as $attribute) {
105
            $rules[$attribute->name()] = $attribute->rules(); 
106
        }
107
        return $rules;
108
    }
109
110
    public function setAttribute($attribute, $value)
111
    {
112
        $item = $this->hasAttribute($attribute) ? $this->attributes->get($attribute)->setValue($value) : new BaseAttribute($attribute, $value);
113
        $this->updateItem($item);
114
    }
115
116
    private function updateItem(SmartAttributeInterface $item)
117
    {
118
        $this->attributes[$item->name()] = $item;
119
        $collection = $this->getCollection();
120
        $collection[$this->getAttributeValue('name')] = $this->toArray();
121
        $this->saveCollection($collection);
122
    }
123
124
    public function deleteItem(string $name)
125
    {
126
        $this->attributes->forget($name);
127
        $collection = $this->getCollection();
128
        $collection->forget($name);
129
        $this->saveCollection($collection);
130
    }
131
132
    public function toCollection(): Collection
133
    {
134
        return $this->attributes->mapWithKeys(function ($item, $key) {
135
            return [$key => $item->value()];
136
        });
137
    }
138
139
    public function toArray(): array
140
    {
141
        return $this->toCollection()->toArray();
142
    }
143
144
    private function setAttributeDefaults($componentAttributes)
145
    {
146
        foreach ($componentAttributes as $attribute) {
147
            if (! $this->hasAttribute($attribute::getAttributeName())) {
148
                $this->attributes[$attribute::getAttributeName()] = $attribute::make($attribute::getAttributeName(), $attribute::getDefault($this), $attribute::getValidationRules($this));
149
            }
150
        }
151
    }
152
153
    public static function attributes(): array
154
    {
155
        return [];
156
    }
157
158
    public function getCollection()
159
    {
160
        return collect();
161
    }
162
163
    public function saveCollection($collection)
164
    {
165
        return [];
166
    }
167
}
168