Passed
Push — ft/package ( 5ee474...180175 )
by Philippe
05:16 queued 12s
created

RelatedCollection::inflate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 3.072
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\Common\Relations;
4
5
use Illuminate\Support\Collection;
6
7
class RelatedCollection extends Collection
8
{
9 3
    public static function availableChildren(ActsAsParent $parent): self
0 ignored issues
show
Unused Code introduced by
The parameter $parent 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

9
    public static function availableChildren(/** @scrutinizer ignore-unused */ ActsAsParent $parent): self

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...
10
    {
11 3
        $available_children_types = config('thinktomorrow.chief.relations.children', []);
12
13 3
        $collection = new static();
14
15 3
        foreach ($available_children_types as $type) {
16 3
            $collection = $collection->merge((new $type)->all());
17
        }
18
19 3
        return $collection;
20
    }
21
    
22
    public static function relationIds(Collection $collection): Collection
23
    {
24
        return $collection->map(function ($entry) {
25
            return $entry->getRelationId();
26
        });
27
    }
28
29 2
    public function flattenForSelect()
30
    {
31
        return $this->map(function (ActsAsChild $child) {
32
            return [
33 1
                'id'    => $child->getRelationId(),
34 1
                'label' => $child->getRelationLabel(),
35 1
                'group' => $child->getRelationGroup(),
36
            ];
37 2
        });
38
    }
39
40 1
    public function flattenForGroupedSelect(): Collection
41
    {
42 1
        $grouped = [];
43
44
        $this->flattenForSelect()->each(function ($entry) use (&$grouped) {
45
            if (isset($grouped[$entry['group']])) {
46
                $grouped[$entry['group']]['values'][] = $entry;
47
            } else {
48
                $grouped[$entry['group']] = ['group' => $entry['group'], 'values' => [$entry]];
49
            }
50 1
        });
51
52
        // We remove the group key as we need to have non-assoc array for the multiselect options.
53 1
        return collect(array_values($grouped));
54
    }
55
56 2
    public static function inflate(array $relateds = []): self
57
    {
58 2
        if (count($relateds) == 1 && is_null(reset($relateds))) {
59
            $relateds = [];
60
        }
61
62
        return (new static($relateds))->map(function ($related) {
63 1
            list($type, $id) = explode('@', $related);
64 1
            return (new $type)->find($id);
65 2
        });
66
    }
67
}
68