Passed
Push — feature-family-log ( 455fa6...893bac )
by Laurent
01:43
created

DoctrineFamilyLogRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 51
c 1
b 0
f 1
dl 0
loc 100
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findParent() 0 25 2
A add() 0 9 1
A existWithLabel() 0 13 1
A __construct() 0 3 1
A getData() 0 14 2
A update() 0 10 1
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, slug, level, path, dir) AS (
58
    SELECT uuid, parent_id, label, slug, level, path, 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.slug, f1.level, f1.path, 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, slug, level, path 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 add(FamilyLog $familyLog): void
84
    {
85
        $data = $this->getData($familyLog);
86
87
        $statement = $this->connection->prepare(
88
            'INSERT INTO family_log
89
(uuid, parent_id, label, slug, level, path) VALUES (:uuid, :parent_id, :label, :slug, :level, :path)'
90
        );
91
        $statement->execute($data);
92
    }
93
94
    /**
95
     * @throws \Doctrine\DBAL\Exception|Exception
96
     */
97
    public function update(FamilyLog $familyLog): void
98
    {
99
        $data = $this->getData($familyLog);
100
101
        $statement = $this->connection->prepare(
102
            'UPDATE family_log SET
103
uuid = :uuid, parent_id = :parent_id, label = :label, slug = :slug, level = :level, path = :path
104
WHERE uuid = :uuid'
105
        );
106
        $statement->execute($data);
107
    }
108
109
    private function getData(FamilyLog $familyLog): array
110
    {
111
        $parent = null;
112
        if (null !== $familyLog->parent()) {
113
            $parent = $familyLog->parent()->uuid();
114
        }
115
116
        return [
117
            'uuid' => $familyLog->uuid(),
118
            'parent_id' => $parent,
119
            'label' => $familyLog->label(),
120
            'slug' => $familyLog->slug(),
121
            'level' => (int) $familyLog->level(),
122
            'path' => $familyLog->path(),
123
        ];
124
    }
125
}
126