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

GetSettingsHandler::__invoke()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
nc 4
nop 1
dl 0
loc 19
rs 9.6111
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\Settings\Handler;
15
16
use Administration\Application\Settings\ReadModel\Settings;
17
use Administration\Infrastructure\Persistence\DoctrineOrm\Repositories\DoctrineSettingsRepository;
18
use Administration\Infrastructure\Settings\Query\GetSettings;
19
use Core\Domain\Protocol\Common\Query\QueryHandlerProtocol;
20
use Doctrine\ORM\NonUniqueResultException;
21
use Doctrine\Persistence\ManagerRegistry;
22
23
class GetSettingsHandler implements QueryHandlerProtocol
24
{
25
    private ManagerRegistry $registry;
26
27
    public function __construct(ManagerRegistry $registry)
28
    {
29
        $this->registry = $registry;
30
    }
31
32
    /**
33
     * @throws NonUniqueResultException
34
     */
35
    public function __invoke(GetSettings $query): ?Settings
36
    {
37
        if (null === $query->currency() && null === $query->locale()) {
38
            return (new DoctrineSettingsRepository($this->registry))
39
                ->findOne()
40
            ;
41
        }
42
        if (null !== $query->locale()) {
43
            return (new DoctrineSettingsRepository($this->registry))
44
                ->findByLocale($query->locale())
45
            ;
46
        }
47
        if (null !== $query->currency()) {
48
            return (new DoctrineSettingsRepository($this->registry))
49
                ->findByCurrency($query->currency())
50
            ;
51
        }
52
53
        return null;
54
    }
55
}
56