Passed
Push — main ( 7dcca1...e70bce )
by Daniel
04:22
created

DatabaseValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 25
ccs 0
cts 14
cp 0
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Setup\Validator;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Psr\Container\ContainerInterface;
9
use Throwable;
10
use Uxmp\Core\Component\Config\ConfigProviderInterface;
11
use Uxmp\Core\Component\Setup\Validator\Exception\EnvironmentValidationException;
12
13
final readonly class DatabaseValidator implements ValidatorInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 13 at column 6
Loading history...
14
{
15
    public function __construct(
16
        private ContainerInterface $dic,
17
        private ConfigProviderInterface $configProvider,
18
    ) {
19
    }
20
21
    public function validate(): void
22
    {
23
        $dsn = $this->configProvider->getDatabaseDsn();
24
25
        if ($dsn === '') {
26
            throw new EnvironmentValidationException(
27
                'Database DSN is not set in config'
28
            );
29
        }
30
31
        try {
32
            $this->dic->get(EntityManagerInterface::class)->getConnection()->connect();
33
        } catch (Throwable $throwable) {
34
            throw new EnvironmentValidationException(
35
                'Database connection could not be established',
36
                0,
37
                $throwable
38
            );
39
        }
40
    }
41
}
42