Passed
Push — develop ( 6aee1f...eb8792 )
by Laurent
01:33
created

DoctrineSettingsFinder::__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\Protocol\Finders\SettingsFinderProtocol;
17
use Administration\Application\Settings\ReadModel\Settings as SettingsReadModel;
18
use Administration\Domain\Settings\Model\VO\Currency;
19
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...
20
use Doctrine\DBAL\Exception;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\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...
21
22
class DoctrineSettingsFinder implements SettingsFinderProtocol
23
{
24
    private Connection $connection;
25
26
    public function __construct(Connection $connection)
27
    {
28
        $this->connection = $connection;
29
    }
30
31
    /**
32
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
33
     */
34
    public function findOne(): ?SettingsReadModel
35
    {
36
        $query = <<<'SQL'
37
SELECT
38
    settings.uuid as uuid,
39
    settings.currency as currency,
40
    settings.locale as locale
41
FROM settings
42
SQL;
43
        $result = $this->connection->executeQuery($query)->fetchAssociative();
44
45
        if (false !== $result) {
46
            return $this->createSettings($result);
47
        }
48
49
        return null;
50
    }
51
52
    /**
53
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
54
     */
55
    public function findByLocale(string $locale): ?SettingsReadModel
56
    {
57
        $query = <<<'SQL'
58
SELECT
59
    settings.uuid as uuid,
60
    settings.currency as currency,
61
    settings.locale as locale
62
FROM settings
63
WHERE locale = :locale
64
SQL;
65
        $result = $this->connection->executeQuery($query, ['locale' => $locale])->fetchAssociative();
66
67
        if (false !== $result) {
68
            return $this->createSettings($result);
69
        }
70
71
        return null;
72
    }
73
74
    /**
75
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
76
     */
77
    public function findByCurrency(string $currency): ?SettingsReadModel
78
    {
79
        $query = <<<'SQL'
80
SELECT
81
    settings.uuid as uuid,
82
    settings.currency as currency,
83
    settings.locale as locale
84
FROM settings
85
WHERE currency = :currency
86
SQL;
87
        $result = $this->connection->executeQuery($query, ['currency' => $currency])->fetchAssociative();
88
89
        if (false !== $result) {
90
            return $this->createSettings($result);
91
        }
92
93
        return null;
94
    }
95
96
    private function createSettings(array $result): SettingsReadModel
97
    {
98
        $symbol = Currency::fromString($result['currency']);
99
100
        return new SettingsReadModel(
101
            $result['currency'],
102
            $result['locale'],
103
            $symbol->symbol(),
104
            $result['uuid']
105
        );
106
    }
107
}
108