Passed
Pull Request — develop (#155)
by Laurent
02:14
created

DoctrineFamilyLogFinder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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\Finders\Doctrine;
15
16
use Administration\Application\FamilyLog\ReadModel\FamilyLog as FamilyLogReadModel;
17
use Administration\Application\FamilyLog\ReadModel\FamilyLogs;
18
use Administration\Application\Protocol\Finders\FamilyLogFinderProtocol;
19
use Administration\Infrastructure\FamilyLog\Mapper\FamilyLogModelMapper;
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 DoctrineFamilyLogFinder implements FamilyLogFinderProtocol
24
{
25
    private 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 findBySlug(string $slug): FamilyLogReadModel
36
    {
37
        $result = $this->connection->createQueryBuilder()
38
            ->select('uuid', 'parent_id', 'label', 'slug', 'level', 'path')
39
            ->from('family_log')
40
            ->where('slug = :slug')
41
            ->setParameter('slug', $slug)
42
            ->execute()
43
            ->fetchAssociative()
44
        ;
45
46
        return (new FamilyLogModelMapper())->getReadModelFromDataArray($result);
47
    }
48
49
    /**
50
     * @throws \Doctrine\DBAL\Exception|Exception
51
     */
52
    public function findAll(): FamilyLogs
53
    {
54
        $query = <<<'SQL'
55
WITH RECURSIVE cte (uuid, parent_id, label, level, path, slug, dir) AS (
56
    SELECT uuid, parent_id, label, level, path, slug, CAST(null as CHAR(10)) as dir
57
    FROM family_log
58
    UNION
59
    SELECT f1.uuid, f1.parent_id, f1.label, f1.level, f1.path, f1.slug, IFNULL(f2.dir, 'down')
60
    FROM family_log f1
61
        INNER JOIN cte f2 ON f1.parent_id = f2.uuid AND IFNULL(f2.dir, 'down')='down'
62
    UNION
63
    SELECT f1.uuid, f1.parent_id, f1.label, f1.level, f1.path, f1.slug, IFNULL(f2.dir, 'up')
64
    FROM family_log f1
65
            INNER JOIN cte f2 ON f1.parent_id = f2.uuid AND IFNULL(f2.dir, 'up')='up'
66
)
67
SELECT DISTINCT uuid, parent_id, label, level, path, slug FROM cte
68
ORDER BY level DESC
69
SQL;
70
71
        $results = $this->connection->executeQuery($query)->fetchAllAssociative();
72
73
        return (new FamilyLogModelMapper())->getFamilyLogsFromArray($results);
74
    }
75
}
76