Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

PresentSections::addModelToCollection()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.288

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 22
ccs 8
cts 10
cp 0.8
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 2
crap 6.288
1
<?php
2
3
namespace Thinktomorrow\Chief\PageBuilder;
4
5
use Illuminate\Support\Collection;
6
use Thinktomorrow\Chief\Concerns\Viewable\ViewableContract;
7
use Thinktomorrow\Chief\Management\ManagedModel;
8
use Thinktomorrow\Chief\Modules\Module;
9
use Thinktomorrow\Chief\Relations\ActsAsChild;
10
use Thinktomorrow\Chief\Relations\ActsAsParent;
11
use Thinktomorrow\Chief\Sets\Set;
12
use Thinktomorrow\Chief\Sets\StoredSetReference;
13
14
class PresentSections
15
{
16
    /** @var ActsAsParent */
17
    private $parent;
18
19
    /**
20
     * Original collection of children
21
     * @var array
22
     */
23
    private $children;
24
25
    /**
26
     * Resulting sets
27
     * @var array
28
     */
29
    private $sets;
30
31
    /**
32
     * Keep track of current pageset index key
33
     * @var string
34
     */
35
    private $current_index = null;
36
37
    /**
38
     * Keep track of current pageset type. This is mainly to bundle
39
     * individual selected pages together.
40
     * @var string
41
     */
42
    private $current_type = null;
43
44
    private $withSnippets = false;
45
46 10
    public function __construct()
47
    {
48 10
        $this->sets = collect([]);
0 ignored issues
show
Documentation Bug introduced by
It seems like collect(array()) of type Illuminate\Support\Collection is incompatible with the declared type array of property $sets.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49 10
    }
50
51
    // pages can be adopted as children individually or as a pageset. They are presented in one module file
52
    // with the collection of all pages combined. But only if they are sorted right after each other
53 10
    public function __invoke(ActsAsParent $parent, Collection $children): Collection
54
    {
55 10
        $this->parent = $parent;
56 10
        $this->children = $children;
0 ignored issues
show
Documentation Bug introduced by
It seems like $children of type Illuminate\Support\Collection is incompatible with the declared type array of property $children.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
58 10
        $this->withSnippets = $parent->withSnippets;
0 ignored issues
show
Bug introduced by
Accessing withSnippets on the interface Thinktomorrow\Chief\Relations\ActsAsParent suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
59
60 10
        return $this->toCollection();
61
    }
62
63 10
    public function toCollection(): Collection
64
    {
65 10
        foreach ($this->children as $i => $child) {
66 10
            if ($child instanceof StoredSetReference) {
67 1
                $this->addSetToCollection($i, $child->toSet());
68 1
                continue;
69
            }
70
71
            // A module is something we will add as is, without combining them together
72 9
            if ($child instanceof Module) {
73 7
                $this->sets[$i] = $child;
74 7
                $this->current_type = null;
75 7
                continue;
76
            }
77
78 5
            $this->addModelToCollection($i, $child);
79
        }
80
81
        return $this->sets->values()->map(function (ViewableContract $child) {
82 10
            return ($this->withSnippets && method_exists($child, 'withSnippets'))
83 7
                ? $child->withSnippets()->setViewParent($this->parent)->renderView()
84 10
                : $child->setViewParent($this->parent)->renderView();
85 10
        });
86
    }
87
88 1
    private function addSetToCollection($index, Set $set)
89
    {
90 1
        $this->sets[$index] = $set;
91 1
        $this->current_type = null;
92 1
    }
93
94 5
    private function addModelToCollection($index, ActsAsChild $model)
95
    {
96
        // Only published pages you fool!
97
        // TODO: check for assistant instead of method existence
98 5
        if (method_exists($model, 'isPublished') && ! $model->isPublished()) {
99
            return;
100
        }
101
102 5
        $key = $model->flatReferenceGroup();
103
104
        // Set the current collection to the model key identifier: for pages this is the collection key, for
105
        // other managed models this is the registered key.
106 5
        if ($this->current_type == null || $this->current_type != $key) {
107 5
            $this->current_type = $key;
108 5
            $this->current_index = $index;
109
        }
110
        // If current pageset index is null, let's make sure it is set to the current index
111 3
        elseif (is_null($this->current_index)) {
0 ignored issues
show
introduced by
The condition is_null($this->current_index) is always false.
Loading history...
112
            $this->current_index = $index;
113
        }
114
115 5
        $this->pushToSet($model, $key);
116 5
    }
117
118 5
    private function pushToSet($model, string $setKey)
119
    {
120 5
        if (!isset($this->sets[$this->current_index])) {
121 5
            $this->sets[$this->current_index] = new Set([], $setKey);
122
        }
123
124 5
        $this->sets[$this->current_index]->push($model);
125 5
    }
126
}
127