Passed
Push — feature-family-log ( 90a5a1...8af55d )
by Laurent
06:55
created

FamilyLog::attributeParent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Administration\Domain\FamilyLog\Model;
15
16
use Administration\Domain\FamilyLog\Model\VO\FamilyLogUuid;
17
use Core\Domain\Common\Model\VO\NameField;
18
19
final class FamilyLog
20
{
21
    private string $uuid;
22
    private string $label;
23
    private int $level;
24
    private ?FamilyLog $parent = null;
25
    /**
26
     * @var FamilyLog[]|null
27
     */
28
    private ?array $children = null;
29
    private string $slug;
30
    private string $path;
31
32
    public function __construct(FamilyLogUuid $uuid, NameField $label, int $level, ?self $parent = null)
33
    {
34
        $this->uuid = $uuid->toString();
35
        $this->label = $label->getValue();
36
        $this->path = $label->slugify();
37
        $this->slug = $label->slugify();
38
        $this->level = $level;
39
        if (null !== $parent) {
40
            $this->attributeParent($parent);
41
        }
42
    }
43
44
    public static function create(FamilyLogUuid $uuid, NameField $name, int $level, ?self $parent = null): self
45
    {
46
        return new self($uuid, $name, $level, $parent);
47
    }
48
49
    public function uuid(): string
50
    {
51
        return $this->uuid;
52
    }
53
54
    public function label(): string
55
    {
56
        return $this->label;
57
    }
58
59
    public function level(): int
60
    {
61
        return $this->level;
62
    }
63
64
    public function parent(): ?self
65
    {
66
        return $this->parent;
67
    }
68
69
    public function children(): ?array
70
    {
71
        return $this->children;
72
    }
73
74
    public function path(): string
75
    {
76
        return $this->path;
77
    }
78
79
    public function slug(): string
80
    {
81
        return $this->slug;
82
    }
83
84
    public function parseTree(): array
85
    {
86
        $arrayChildren = [];
87
        if (null === $this->children) {
88
            return [$this->label => $arrayChildren];
89
        }
90
91
        foreach ($this->children as $child) {
92
            if (null !== $this->hasChildren($child)) {
93
                $arrayChildren[$child->label] = $this->hasChildren($child);
94
            } else {
95
                $arrayChildren[] = $child->label;
96
            }
97
        }
98
99
        return [$this->label => $arrayChildren];
100
    }
101
102
    public function attributeParent(?self $parent): void
103
    {
104
        $this->parent = $parent;
105
        if (null !== $parent) {
106
            $this->parent->addChild($this);
0 ignored issues
show
Bug introduced by
The method addChild() does not exist on null. ( Ignorable by Annotation )

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

106
            $this->parent->/** @scrutinizer ignore-call */ 
107
                           addChild($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
            $this->path = $parent->path() . '/' . $this->slug();
108
        }
109
    }
110
111
    private function hasChildren(self $familyLog): ?array
112
    {
113
        if (null !== $familyLog->children) {
114
            return \array_map(static function (self $child) {
115
                return $child->label;
116
            }, $familyLog->children);
117
        }
118
119
        return null;
120
    }
121
122
    private function addChild(self $child): void
123
    {
124
        $this->children[] = $child;
125
    }
126
}
127