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

DoctrineFamilyLogRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 44
c 1
b 0
f 1
dl 0
loc 84
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findParent() 0 25 2
A getData() 0 13 2
A add() 0 9 1
A existWithLabel() 0 13 1
A __construct() 0 3 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, dir) AS (
58
    SELECT uuid, parent_id, label, slug, level, 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, 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 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) VALUES (:uuid, :parent_id, :label, :slug, :level)'
90
        );
91
        $statement->execute($data);
92
    }
93
94
    private function getData(FamilyLog $familyLog): array
95
    {
96
        $parent = null;
97
        if (null !== $familyLog->parent()) {
98
            $parent = $familyLog->parent()->uuid();
99
        }
100
101
        return [
102
            'uuid' => $familyLog->uuid(),
103
            'parent_id' => $parent,
104
            'label' => $familyLog->label(),
105
            'slug' => $familyLog->slug(),
106
            'level' => (int) $familyLog->level(),
107
        ];
108
    }
109
}
110