Passed
Push — main ( 764702...024f60 )
by Daniel
04:19
created

DatabaseValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 8
cp 0
rs 9.9332
cc 3
nc 3
nop 0
crap 12
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\Setup\Validator\Exception\EnvironmentValidationException;
11
12
final class DatabaseValidator implements ValidatorInterface
13
{
14
    public function __construct(
15
        private ContainerInterface $dic,
16
    ) {
17
    }
18
19
    public function validate(): void
20
    {
21
        $dsn = $_ENV['DATABASE_DSN'] ?? '';
22
23
        if ($dsn === '') {
24
            throw new EnvironmentValidationException(
25
                'Database DSN is not set in config'
26
            );
27
        }
28
29
        try {
30
            $this->dic->get(EntityManagerInterface::class)->getConnection()->connect();
31
        } catch (Throwable $e) {
32
            throw new EnvironmentValidationException(
33
                'Database connection could not be established',
34
                0,
35
                $e
36
            );
37
        }
38
    }
39
}
40