Passed
Push — develop ( 0d66a7...37c69c )
by Laurent
05:34 queued 02:39
created

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