Passed
Pull Request — develop (#152)
by Laurent
01:25
created

DoctrineSettingsRepository::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
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\Persistence\DoctrineOrm\Repositories;
15
16
use Administration\Domain\Protocol\Repository\SettingsRepositoryProtocol;
17
use Administration\Domain\Settings\Model\Settings;
18
use Administration\Infrastructure\Settings\Mapper\SettingsModelMapper;
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 DoctrineSettingsRepository implements SettingsRepositoryProtocol
23
{
24
    protected Connection $connection;
25
26
    public function __construct(Connection $connection)
27
    {
28
        $this->connection = $connection;
29
    }
30
31
    /**
32
     * @throws Exception
33
     */
34
    final public function add(Settings $settings): void
35
    {
36
        $data = (new SettingsModelMapper())->getDataFromSettings($settings);
37
38
        $query = $this->connection->createQueryBuilder()
39
            ->insert('settings')
40
            ->values(['uuid' => '?', 'currency' => '?', 'locale' => '?'])
41
            ->setParameters([$data['uuid'], $data['currency'], $data['locale']])
42
        ;
43
        $query->execute();
44
    }
45
46
    /**
47
     * @throws Exception
48
     */
49
    final public function update(Settings $settings): void
50
    {
51
        $data = (new SettingsModelMapper())->getDataFromSettings($settings);
52
53
        $query = $this->connection->createQueryBuilder()
54
            ->update('settings')
55
            ->set('currency', '?')
56
            ->set('locale', '?')
57
            ->where('uuid = ?')
58
            ->setParameters([0 => $data['currency'], 1 => $data['locale'], 2 => $settings->uuid()])
59
        ;
60
        $query->execute();
61
    }
62
63
    /**
64
     * @throws Exception
65
     */
66
    final public function findOneByUuid(string $uuid): ?Settings
67
    {
68
        $query = $this->connection->createQueryBuilder()
69
            ->select('uuid', 'currency', 'locale')
70
            ->from('settings')
71
            ->where('uuid = ?')
72
            ->setParameter(0, $uuid)
73
        ;
74
        $result = $query->execute()->fetchAssociative();
75
76
        return (new SettingsModelMapper())->getDomainModelFromArray($result);
77
    }
78
79
    /**
80
     * @throws Exception
81
     */
82
    final public function exists(): bool
83
    {
84
        $query = $this->connection->createQueryBuilder()
85
            ->select('uuid')
86
            ->from('settings')
87
        ;
88
89
        $statement = $query->execute()->fetchAssociative();
90
91
        return false !== $statement;
92
    }
93
}
94