Passed
Push — feature-family-log ( 893bac...9622c6 )
by Laurent
01:33
created

FamilyLogModelMapper::getParent()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
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\Infrastructure\FamilyLog\Mapper;
15
16
use Administration\Application\FamilyLog\ReadModel\FamilyLog as FamilyLogReadModel;
17
use Administration\Application\FamilyLog\ReadModel\FamilyLogs;
18
use Administration\Domain\FamilyLog\Model\FamilyLog;
19
use Administration\Domain\FamilyLog\Model\VO\FamilyLogUuid;
20
use Administration\Infrastructure\Finders\Exceptions\FamilyLogNotFound;
21
use Core\Domain\Common\Model\VO\NameField;
22
23
class FamilyLogModelMapper
24
{
25
    public function createParentTreeFromArray(array $familyLogs): FamilyLog
26
    {
27
        foreach ($familyLogs as $key => $familyLog) {
28
            if (0 === $key) {
29
                ${'parent' . $key} = FamilyLog::create(
30
                    FamilyLogUuid::fromString($familyLog['uuid']),
31
                    NameField::fromString($familyLog['label']),
32
                    (int) $familyLog['level']
33
                );
34
                $parent = ${'parent' . $key};
35
            } else {
36
                ${'parent' . $key} = FamilyLog::create(
37
                    FamilyLogUuid::fromString($familyLog['uuid']),
38
                    NameField::fromString($familyLog['label']),
39
                    (int) $familyLog['level'],
40
                    ${'parent' . ($key - 1)}
41
                );
42
                unset(${'parent' . ($key - 1)});
43
                $parent = ${'parent' . $key};
44
            }
45
        }
46
47
        return $parent;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parent seems to be defined by a foreach iteration on line 27. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
48
    }
49
50
    public function createChildrenTreeFromArray(array $result, string $uuid): FamilyLog
51
    {
52
        $familyLog = $this->createParentTreeFromArray($result);
53
54
        if ($uuid === $familyLog->uuid()) {
55
            return $familyLog;
56
        }
57
        if (null === $familyLog->parent()) {
58
            throw new FamilyLogNotFound();
59
        }
60
61
        return $this->getParent($familyLog, $uuid);
62
    }
63
64
    public function getFamilyLogsFromArray(array $data): FamilyLogs
65
    {
66
        /** @var FamilyLog[] $familyLogs */
67
        $familyLogs = [];
68
69
        foreach ($data as $datum) {
70
            $familyLogs[$datum['uuid']] = FamilyLog::create(
71
                FamilyLogUuid::fromString($datum['uuid']),
72
                NameField::fromString($datum['label']),
73
                (int) $datum['level'],
74
                null,
75
                $datum['path']
76
            );
77
        }
78
79
        \usort($data, static function ($a, $b) {
80
            return \strcmp($a['level'], $b['level']);
81
        });
82
83
        foreach ($data as $datum) {
84
            \array_map(static function (FamilyLog $familyLog) use ($datum, $familyLogs): void {
85
                if (null !== $datum['parent_id'] && ($datum['uuid'] === $familyLog->uuid())) {
86
                    $familyLog->attributeParent($familyLogs[$datum['parent_id']]);
87
                }
88
            }, $familyLogs);
89
        }
90
91
        return new FamilyLogs(
92
            ...\array_map(function (FamilyLog $familyLog) {
93
                return $this->createReadModelFromDomain($familyLog);
94
            }, \array_values($familyLogs))
95
        );
96
    }
97
98
    public function getReadModelFromDataArray(array $data): FamilyLogReadModel
99
    {
100
        $familyLog = FamilyLog::create(
101
            $data['uuid'],
102
            $data['label'],
103
            $data['level'],
104
            $data['parent_id'],
105
            $data['path']
106
        );
107
108
        return $this->createReadModelFromDomain($familyLog);
109
    }
110
111
    public function createReadModelFromDomain(FamilyLog $familyLog): FamilyLogReadModel
112
    {
113
        return new FamilyLogReadModel(
114
            $familyLog->uuid(),
115
            $familyLog->label(),
116
            $familyLog->level(),
117
            $familyLog->parent(),
118
            $familyLog->children(),
119
            $familyLog->path(),
120
            $familyLog->slug()
121
        );
122
    }
123
124
    private function getParent(FamilyLog $familyLog, string $uuid): FamilyLog
125
    {
126
        if ((null !== $familyLog->parent()) && ($uuid === $familyLog->parent()->uuid())) {
127
            return $familyLog->parent();
128
        }
129
130
        return $this->getParent($familyLog->parent(), $uuid);
131
    }
132
}
133