Passed
Push — orm-management ( 19e760...de38f8 )
by Laurent
01:44
created

DoctrineSettingsRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\Application\Settings\ReadModel\Settings as SettingsReadModel;
17
use Administration\Domain\Protocol\Repository\SettingsRepositoryProtocol;
18
use Administration\Domain\Settings\Model\Settings as SettingsModel;
19
use Administration\Domain\Settings\Model\VO\Currency;
20
use Administration\Infrastructure\Persistence\DoctrineOrm\Entities\Settings;
21
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
0 ignored issues
show
Bug introduced by
The type Doctrine\Bundle\Doctrine...ServiceEntityRepository 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
use Doctrine\ORM\NonUniqueResultException;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\NonUniqueResultException 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...
23
use Doctrine\ORM\OptimisticLockException;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\OptimisticLockException 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...
24
use Doctrine\ORM\ORMException;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\ORMException 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...
25
use Doctrine\Persistence\ManagerRegistry;
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\ManagerRegistry 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...
26
27
class DoctrineSettingsRepository extends ServiceEntityRepository implements SettingsRepositoryProtocol
28
{
29
    public function __construct(ManagerRegistry $registry)
30
    {
31
        parent::__construct($registry, Settings::class);
32
    }
33
34
    /**
35
     * @throws ORMException
36
     * @throws OptimisticLockException
37
     */
38
    final public function save(SettingsModel $settings): void
39
    {
40
        $settingsEntity = Settings::fromModel($settings);
41
        $this->getEntityManager()->persist($settingsEntity);
42
        $this->getEntityManager()->flush();
43
    }
44
45
    /**
46
     * @throws ORMException
47
     */
48
    final public function remove(SettingsModel $settings): void
49
    {
50
        $this->getEntityManager()->remove($settings);
51
    }
52
53
    /**
54
     * @throws NonUniqueResultException
55
     */
56
    final public function findOneByUuid(string $uuid): ?SettingsModel
57
    {
58
        return $this->createQueryBuilder('ds')
59
            ->where('ds.uuid = :uuid')
60
            ->setParameter('uuid', $uuid)
61
            ->getQuery()
62
            ->getOneOrNullResult()
63
            ;
64
    }
65
66
    /**
67
     * @throws NonUniqueResultException
68
     */
69
    final public function findByLocale(string $locale): ?SettingsReadModel
70
    {
71
        $statement = $this->createQueryBuilder('ds')
72
            ->where('ds.locale = :locale')
73
            ->setParameter('locale', $locale)
74
            ->getQuery()
75
            ->getOneOrNullResult()
76
            ;
77
        if (null !== $statement) {
78
            \assert($statement instanceof Settings);
79
80
            return $this->createSettings($statement);
81
        }
82
83
        return null;
84
    }
85
86
    /**
87
     * @throws NonUniqueResultException
88
     */
89
    final public function findByCurrency(string $currency): ?SettingsReadModel
90
    {
91
        $statement = $this->createQueryBuilder('ds')
92
            ->where('ds.currency = :currency')
93
            ->setParameter('currency', $currency)
94
            ->getQuery()
95
            ->getOneOrNullResult()
96
        ;
97
        if (null !== $statement) {
98
            \assert($statement instanceof Settings);
99
100
            return $this->createSettings($statement);
101
        }
102
103
        return null;
104
    }
105
106
    /**
107
     * @throws NonUniqueResultException
108
     */
109
    final public function settingsExist(): bool
110
    {
111
        $statement = $this->createQueryBuilder('ds')
112
            ->getQuery()
113
            ->getOneOrNullResult()
114
        ;
115
116
        return null !== $statement;
117
    }
118
119
    /**
120
     * @throws NonUniqueResultException
121
     */
122
    final public function findOne(): ?SettingsReadModel
123
    {
124
        $statement = $this->createQueryBuilder('ds')
125
            ->getQuery()
126
            ->getOneOrNullResult()
127
        ;
128
        if (null !== $statement) {
129
            \assert($statement instanceof Settings);
130
131
            return $this->createSettings($statement);
132
        }
133
134
        return null;
135
    }
136
137
    private function createSettings(Settings $statement): SettingsReadModel
138
    {
139
        $symbol = Currency::fromString($statement->getCurrency());
140
141
        return new SettingsReadModel(
142
            $statement->getCurrency(),
143
            $statement->getLocale(),
144
            $symbol->symbol(),
145
            $statement->getUuid()
146
        );
147
    }
148
}
149