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

FamilyLogModelMapper::getFamilyLogsFromArray()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 29
rs 9.3888
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 Core\Domain\Common\Model\VO\NameField;
21
22
class FamilyLogModelMapper
23
{
24
    public function createParentTreeFromArray(array $familyLogs): FamilyLog
25
    {
26
        foreach ($familyLogs as $key => $familyLog) {
27
            if ('1' === $familyLog['level']) {
28
                ${'parent' . $key} = FamilyLog::create(
29
                    FamilyLogUuid::fromString($familyLog['uuid']),
30
                    NameField::fromString($familyLog['label']),
31
                    (int) $familyLog['level']
32
                );
33
                $parent = ${'parent' . $key};
34
            } else {
35
                ${'parent' . $key} = FamilyLog::create(
36
                    FamilyLogUuid::fromString($familyLog['uuid']),
37
                    NameField::fromString($familyLog['label']),
38
                    (int) $familyLog['level'],
39
                    ${'parent' . ($key - 1)}
40
                );
41
                unset(${'parent' . ($key - 1)});
42
                $parent = ${'parent' . $key};
43
            }
44
        }
45
46
        return $parent;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parent seems to be defined by a foreach iteration on line 26. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
47
    }
48
49
    public function getFamilyLogsFromArray(array $data): FamilyLogs
50
    {
51
        /** @var FamilyLog[] $familyLogs */
52
        $familyLogs = [];
53
54
        foreach ($data as $datum) {
55
            $familyLogs[$datum['uuid']] = FamilyLog::create(
56
                FamilyLogUuid::fromString($datum['uuid']),
57
                NameField::fromString($datum['label']),
58
                $datum['level']
59
            );
60
        }
61
62
        \usort($data, static function ($a, $b) {
63
            return \strcmp($a['level'], $b['level']);
64
        });
65
66
        foreach ($data as $datum) {
67
            \array_map(static function (FamilyLog $familyLog) use ($datum, $familyLogs): void {
68
                if (null !== $datum['parent_id'] && ($datum['uuid'] === $familyLog->uuid())) {
69
                    $familyLog->attributeParent($familyLogs[$datum['parent_id']]);
70
                }
71
            }, $familyLogs);
72
        }
73
74
        return new FamilyLogs(
75
            ...\array_map(function (FamilyLog $familyLog) {
76
                return $this->createReadModelFromDomain($familyLog);
77
            }, \array_values($familyLogs))
78
        );
79
    }
80
81
    public function getReadModelFromDataArray(array $data): FamilyLogReadModel
82
    {
83
        // @Todo: get parental line.
84
        $familyLog = FamilyLog::create(
85
            $data['name'],
86
            $data['parent_id'],
87
            $data['label'],
88
        );
89
90
        return $this->createReadModelFromDomain($familyLog);
91
    }
92
93
    public function createReadModelFromDomain(FamilyLog $familyLog): FamilyLogReadModel
94
    {
95
        return new FamilyLogReadModel(
96
            $familyLog->uuid(),
97
            $familyLog->label(),
98
            $familyLog->level(),
99
            $familyLog->parent(),
100
            $familyLog->children(),
101
            $familyLog->path(),
102
            $familyLog->slug()
103
        );
104
    }
105
}
106