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

DoctrineFamilyLogRepository::findChildren()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 24
rs 9.6666
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\Persistence\DoctrineOrm\Repositories;
15
16
use Administration\Domain\FamilyLog\Model\FamilyLog;
17
use Administration\Domain\Protocol\Repository\FamilyLogRepositoryProtocol;
18
use Administration\Infrastructure\FamilyLog\Mapper\FamilyLogModelMapper;
19
use Administration\Infrastructure\Finders\Exceptions\FamilyLogNotFound;
20
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Doctrine\DBAL\Driver\Exception;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Driver\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
class DoctrineFamilyLogRepository implements FamilyLogRepositoryProtocol
24
{
25
    protected Connection $connection;
26
27
    public function __construct(Connection $connection)
28
    {
29
        $this->connection = $connection;
30
    }
31
32
    /**
33
     * @throws \Doctrine\DBAL\Exception
34
     */
35
    public function existWithLabel(string $label, ?string $parentUuid): bool
36
    {
37
        $statement = $this->connection->createQueryBuilder()
38
            ->select('label')
39
            ->from('family_log')
40
            ->where('label = :label')
41
            ->andWhere('parent_id = :parent_id')
42
            ->setParameters(['label' => $label, 'parent_id' => $parentUuid])
43
            ->execute()
44
            ->fetchOne()
45
        ;
46
47
        return false !== $statement;
48
    }
49
50
    /**
51
     * @throws \Doctrine\DBAL\Exception|Exception
52
     */
53
    public function findParent(string $uuid): FamilyLog
54
    {
55
        $parent = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $parent is dead and can be removed.
Loading history...
56
        $query = <<<'SQL'
57
WITH RECURSIVE cte (uuid, parent_id, label, level, path, slug, dir) AS (
58
    SELECT uuid, parent_id, label, level, path, slug, CAST(null as CHAR(10)) as dir
59
    FROM family_log
60
    WHERE uuid = :uuid
61
    UNION
62
    SELECT f1.uuid, f1.parent_id, f1.label, f1.level, f1.path, f1.slug, IFNULL(f2.dir, 'up')
63
    FROM family_log f1
64
        INNER JOIN cte f2 ON f2.parent_id = f1.uuid AND IFNULL(f2.dir, 'up')='up'
65
)
66
SELECT DISTINCT uuid, parent_id, label, level, path, slug FROM cte
67
ORDER BY level
68
SQL;
69
70
        // Get parents
71
        $result = $this->connection->executeQuery($query, ['uuid' => $uuid])->fetchAllAssociative();
72
73
        if ([] === $result) {
74
            throw new FamilyLogNotFound();
75
        }
76
77
        return (new FamilyLogModelMapper())->createParentTreeFromArray($result);
78
    }
79
80
    /**
81
     * @throws \Doctrine\DBAL\Exception|Exception
82
     */
83
    public function findChildren(string $uuid): FamilyLog
84
    {
85
        $children = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $children is dead and can be removed.
Loading history...
86
        $query = <<<'SQL'
87
WITH RECURSIVE cte (uuid, parent_id, label, level, path, slug, dir) AS (
88
    SELECT uuid, parent_id, label, level, path, slug, CAST(null as CHAR(10)) as dir
89
    FROM family_log
90
    WHERE uuid = :uuid
91
    UNION
92
    SELECT f1.uuid, f1.parent_id, f1.label, f1.level, f1.path, f1.slug, IFNULL(f2.dir, 'down')
93
    FROM family_log f1
94
        INNER JOIN cte f2 ON f1.parent_id = f2.uuid AND IFNULL(f2.dir, 'down')='down'
95
)
96
SELECT DISTINCT uuid, parent_id, label, level, path, slug FROM cte
97
ORDER BY level
98
SQL;
99
        // Get children
100
        $result = $this->connection->executeQuery($query, ['uuid' => $uuid])->fetchAllAssociative();
101
102
        if ([] === $result) {
103
            throw new FamilyLogNotFound();
104
        }
105
106
        return (new FamilyLogModelMapper())->createChildrenTreeFromArray($result, $uuid);
107
    }
108
109
    /**
110
     * @throws \Doctrine\DBAL\Exception|Exception
111
     */
112
    public function add(FamilyLog $familyLog): void
113
    {
114
        $data = $this->getData($familyLog);
115
116
        $statement = $this->connection->prepare(
117
            'INSERT INTO family_log
118
(uuid, parent_id, label, slug, level, path) VALUES (:uuid, :parent_id, :label, :slug, :level, :path)'
119
        );
120
        $statement->execute($data);
121
    }
122
123
    /**
124
     * @throws \Doctrine\DBAL\Exception|Exception
125
     */
126
    public function update(FamilyLog $familyLog): void
127
    {
128
        $data = $this->getData($familyLog);
129
130
        $statement = $this->connection->prepare(
131
            'UPDATE family_log SET
132
uuid = :uuid, parent_id = :parent_id, label = :label, slug = :slug, level = :level, path = :path
133
WHERE uuid = :uuid'
134
        );
135
        $statement->execute($data);
136
    }
137
138
    /**
139
     * @throws \Doctrine\DBAL\Exception|Exception
140
     */
141
    public function delete(string $familyLogUuid): void
142
    {
143
        $statement = $this->connection->prepare('DELETE FROM family_log WHERE uuid = :uuid');
144
        $statement->execute(['uuid' => $familyLogUuid]);
145
    }
146
147
    private function getData(FamilyLog $familyLog): array
148
    {
149
        $parent = null;
150
        if (null !== $familyLog->parent()) {
151
            $parent = $familyLog->parent()->uuid();
152
        }
153
154
        return [
155
            'uuid' => $familyLog->uuid(),
156
            'parent_id' => $parent,
157
            'label' => $familyLog->label(),
158
            'slug' => $familyLog->slug(),
159
            'level' => (int) $familyLog->level(),
160
            'path' => $familyLog->path(),
161
        ];
162
    }
163
}
164